I am trying to use everyauth within express to authenticate users to a website that allows adding text commentary to images. I am currently using the twitter oauth api to authenticate my users.
Now I’m trying to established a custom user database that allows me to keep multiple ways of logging in connected to a single entry in a CouchDB. I am running into trouble when i want to add a new user to the DB when he initially connects via twitter. The code of my findUserById reads as following:
everyauth.everymodule.findUserById (userId, callback) ->
users.findById userId, (err,res) ->
if res.id
# id returned, so there is an entry in the DB
callback null, res
else
# no entry in the DB, add a new one
users.saveById JSON.stringify(userId), {"name":"test"}, (saveErr, saveRes) ->
callback null, saveRes
everyauth.twitter
.consumerKey(config.twitterConsumerKey)
.consumerSecret(config.twitterConsumerSecret)
.findOrCreateUser((session, token, secret, user) ->
promise = @.Promise().fulfill user
).redirectPath '/'
findById and saveById are methods of a cradle object that query the DB, userId is provided by everyauth, as it seems.
My problem lies in the {"name":"test"} part. This is the location where I want to add the data from the req.session.auth.twitter object to the DB. How can I access those values from within the findUserByIdfunction?
Is that even possible? Is there a different approach? I am looking at the findOrCreateUser function, but I have no idea how to change that without crashing my app – i do not yet grasp the concept of the Promise.
After fiddling around further, I have found a solution to the problem i posed. To access the full user data that is transmitted with the OAuth request with respect to the Promise-structure of everyauth, the everyauth.twitter call should look like this:
The
findByTwitterIdcall is a function that queries the CouchDB for a view looking up documents that contain atwitter.idfield.The
req.userobject is populated by this: