Search code examples
couchdbcloudant

Creating view to check a document fields for specific values (For a simple login)


I'm very new to cloudant , so pardon me for this question. I am creating a simple mobile game login system which only checks for username(email) and password.

I have several simple docs that are in this format

{
    "_id": "xxx",
    "_rev": "xxx",
    "password": "3O+k+O8bxsxu0KUlSBUiww==", --encrypted by application beforehand
    "type": "User",
    "email": "[email protected]"
}

Right now I can't seem to get the correct 'Formula' for creating this view (map function) whereby I would do a network request and pass it both the email and password. If there is a doc that matches the email, then check the doc.password against the passed value. If it matches, the function should return a simple "YES".

For now my map function is as follows, but this just returns all the docs .

function(doc) {
    if (doc.email){
        index("password", doc.password, { store : true });
        if (doc.password){ 
            emit("YES");
        }
    }
}

It may be my request format is also wrong. Right now it is as follows. Values are not real, only for format checking

https:/etcetc/_design/app/_view/viewCheckLogin?q=email:"[email protected]"&password:"asd"

Solution

  • It looks like you have misunderstood how views are supposed to work. In general you cannot perform logic to return a different result based on the request. Query parameters in a view request can only be used to limit the result set of view entries returned or to return grouped information from the reduce function.

    To determine if there is a match for a given username and password you could emit those values as keys and then query for them. This would return the view entry for those keys or an empty list if there was no match. However I'd be very cautious about the security here. Anyone with access to the view would be able to see all the view entries, i.e. all the usernames and passwords.