Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

set everyauth.user in findOrCreateUser #116

Closed
andrewtennison opened this Issue Nov 23, 2011 · 5 comments

Comments

Projects
None yet
5 participants

Login works fine, but to get everyauth.user it seems I have to use everyauth.everymodule.findUserById() which then either looks at a local object (as in your example) or makes another call to the server.

During my .findOrCreateUser() my API (true API on remote server) returns the full user object, can I not just set everyauth.user here rather than store the user data in an object to lookup or make another call to the api?

Owner

bnoguchi commented Nov 26, 2011

findOrCreateUser is only invoked right after a 3rd party oauth/oauth2/etc directly calls back to your app. You will want access to everyauth.user in more scenarios than this (i.e., in views that occur many requests after the 3rd party oauth callback that never involve the 3rd party callback again). This is why everyauth.user is set via the user fetched via findUserById. In other words, you don't want to be setting everyauth.user inside findOrCreateUser because it is always set for you anyways as a result of invoking findUserById.

In order to configure findOrCreateUser and findUserById to work with your api, you would do something like the following:

everyauth.everymodule
  .findUserById( function (id, callback) {
    yourApi.fetchUserById(id, function (err, user) {
      if (err) return callback(err);
      callback(null, user);
    });
    // or more succinctly, if your api sends a user to the callback with function signature function (err, user):
    // yourApi.fetchUserById(id, callback);
  });

everyauth.facebook
  .findOrCreateUser( function (session, accessToken, accessTokenExtra, fbUserMetadata) {
    var userPromise = this.Promise();
    // Try to find a matching user, first
    yourApi.fetchOneUser( { fbId: fbUserMetadata.id }, function (err, user) {
      if (err) return userPromise.fail(err);
      if (user) return userPromise.fulfill(user);
      // else create a new user
      yourApi.createNewUserFromFbMetadata(fbUserMetadata, function (err, user) {
        if (err) return userPromise.fail(err);
        return userPromise.fulfill(user);
      });
    });
    return userPromise;
  });

Hope that clarifies things.

If you think I mis-interpreted what I meant by the issue here, please provide a more detailed explanation with desired code.

@bnoguchi bnoguchi closed this Nov 26, 2011

thanks for clarifying, that makes sense.

Thank you! I just wonder how I return a user after fetching from the database. (I didn't know I can use promise like that.)

contactm commented Aug 2, 2012

thank you. was wondering about this... now more clear.

I attempted to do the above using MongoDB

everyauth.everymodule
  .findUserById( function (id, callback) {
    users.findOne(id, function (err, user) {
      if (err) return callback(err);
      callback(null, user);
    });
    // or more succinctly, if your api sends a user to the callback with function signature function (err, user):
    // yourApi.fetchUserById(id, callback);
        console.log('foobar');
  });


everyauth
    .facebook
    .appId('id')
    .appSecret('secret')
    .findOrCreateUser( function (session, accessToken, accessTokenExtra, fbUserMetadata) {
        var promise = this.Promise();
        users.findOne({id:fbUserMetadata.id},function(err,user){
            if (user == null) {
                console.log('new user!');
                users.insert(fbUserMetadata,function(err,ok){
                    if (err){
                        console.log(new Error(err.message));
                    }
                });
                promise.fulfill(user);
            } else if (user) {
                console.log('old user!');
                if (user.blacklisted === true) return promise.fail('denied!');
                promise.fulfill(user);
            }
        });

        return promise;
    })
    .scope('publish_actions')//figure out how to use Open Graph here
    .sendResponse(function(res,data){
        var session = data.session;
        var redirectTo = session.redirectTo;
        delete session.redirectTo;
        //res.redirect(redirectTo);
        res.redirect('/play')
    });

but then everyauth goes into an endless redirect loop... what's going on?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment