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 findUserById
function?
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.