bnoguchi/everyauth — Issue #188: Promise returned in findOrCreateUser seems to lead to user undefined 
Author: sapien99
Created: 2012-02-23 15:05:26+00:00
State: open

Hi guys,

i have to admit that i am fairly new to everyauth, but i really like it. Unfortunately i ran into an issue i am not able to solve on my own. I am using twitterauth for delegated authentication, the target is to map an authenticated twitter user to its "local" user. To achive this, i am executing a list function in couchdb, which returns a JSON object representing the internal user.

This is what such an object looks like:
{"id":"myuserid","name":"myusername","provider":"twitter","twitterid":"mytwitterid"}

For the sample i just use an object to store the users between the delegation part and the further processing. This is done accessing an URL /ilogin/ which will contain the code for the internal authentication and authorization.

As i need to asynchronously fetch the information from couchdb, i return a promise in findOrCreateUser, but it seems (either bug or feature) the redirect to /ilogin/ happens before the fetch really returns the user - and so req.user returns unknown. The everyauth-traces show quite good what happens:

[2012-02-23 15:59:37.449] [INFO] couchauth - initializing configuration for twitter auth-delegation
[2012-02-23 15:59:46.171] [INFO] console - starting step - getRequestToken
[2012-02-23 15:59:47.421] [INFO] console - ...finished step
[2012-02-23 15:59:47.422] [INFO] console - starting step - storeRequestToken
[2012-02-23 15:59:47.422] [INFO] console - ...finished step
[2012-02-23 15:59:47.422] [INFO] console - starting step - redirectToProviderAuth
[2012-02-23 15:59:47.423] [INFO] console - ...finished step
[2012-02-23 15:59:48.966] [INFO] console - starting step - extractTokenAndVerifier
[2012-02-23 15:59:48.967] [INFO] console - ...finished step
[2012-02-23 15:59:48.967] [INFO] console - starting step - getSession
[2012-02-23 15:59:48.967] [INFO] console - ...finished step
[2012-02-23 15:59:48.967] [INFO] console - starting step - rememberTokenSecret
[2012-02-23 15:59:48.967] [INFO] console - ...finished step
[2012-02-23 15:59:48.967] [INFO] console - starting step - getAccessToken
[2012-02-23 15:59:50.155] [INFO] console - ...finished step
[2012-02-23 15:59:50.155] [INFO] console - starting step - fetchOAuthUser
[2012-02-23 15:59:51.189] [INFO] console - ...finished step
[2012-02-23 15:59:51.189] [INFO] console - starting step - findOrCreateUser
[2012-02-23 15:59:51.189] [INFO] couchauth - Finding correct user for twitter user
[2012-02-23 15:59:51.190] [INFO] couchauth - fetching http://localhost:5984/bla/_design/bla/_list/users
[2012-02-23 15:59:51.203] [INFO] console - ...finished step
[2012-02-23 15:59:51.203] [INFO] console - starting step - compileAuth
[2012-02-23 15:59:51.204] [INFO] console - ...finished step
[2012-02-23 15:59:51.204] [INFO] console - starting step - addToSession
[2012-02-23 15:59:51.205] [INFO] console - ...finished step
[2012-02-23 15:59:51.205] [INFO] console - starting step - sendResponse
[2012-02-23 15:59:51.206] [INFO] console - ...finished step
[2012-02-23 15:59:51.211] [TRACE] couchauth - entered /clogin/
[2012-02-23 15:59:51.212] [ERROR] couchauth - clogin got user undefined
[2012-02-23 15:59:51.215] [DEBUG] couchauth - promise of user fulfilled:{"id":"myuserid","name":"myusername","provider":"twitter","twitterid":"mytwitterid"}

Any hint how to prevent this? what am i doing wrong?

I'll also post the code of my little sample

Thanks a lot for your help
Michael