Passport strategy, please, help me understand something!

Passport strategy, please, help me understand something!
0.0 0

#1

Hi! I have a question regarding passport strategies.
I read many many topics about because the app I’m working on right now is my very first one full stack, and I needed to understand how it works and how to solve the numerous issues I encountered :smiley:
My authentication strategies now work fine but I have a question because I don’t understand something:
I eventually had a problem(facebook and twitter only, regarding local auth, I hav 2 middlewares before login, including one for user db creation): I needed to "login " once to create user, then had a failureRedirect because user was saved but not present in req, then I had to login again and user was found in db and logged in.
The issue came from the way I created my user:

// if no user found in db:
        else {
           const twitterUser = new Auth({
                'twitter.id': profile.id,
                'twitter.token': accessToken,
                'twitter.name': profile.displayName,
           }).save(((err) => {
                if(err) {
                    console.log('err: ' + err);
                    done(err);
                }
                done(null, twitterUser);
            }));  
        }
    });
  }
));

then i eventually changed it by doing so

 else {
            const facebookUser = new Auth();
            facebookUser.facebook.id = profile.id;
            facebookUser.facebook.token = accessToken;
            facebookUser.facebook.name = profile.displayName;
            facebookUser.save(((err) // blablabla

and it worked just fine this way, req.user ok and user saved and logged in at once.
I must admit I am exhausted but still, I don’t understand why? what is the big deal?

thank you for your help :slight_smile:


#2

I’m no expert on passport, but in terms of JS, I see a problem.

           const twitterUser = new Auth({
                'twitter.id': profile.id,
                'twitter.token': accessToken,
                'twitter.name': profile.displayName,
           })

is not the same as

            const facebookUser = new Auth();
            facebookUser.facebook.id = profile.id;
            facebookUser.facebook.token = accessToken;
            facebookUser.facebook.name = profile.displayName;

even if we ignore the that one is twitter and one is facebook. Consider the following code.

let obj1 = {
  "test.a": 'apple'
}
let obj2 = { test: {} }
obj2.test = { a: 'aardvark' }

console.log('obj1', JSON.stringify(obj1))
// obj1 {"test.a":"apple"}
console.log('obj2', JSON.stringify(obj2))
// obj2 {"test":{"a":"aardvark"}}

The second case is what we expected, nested objects. But in the first case, look closely - it is a single object, is a property called “test.a”. That is a big difference.


#3

Hi @dariaL

If there is a delay, I suspect you are accidentally letting a function in the form of a closure or similar.

I haven’t worked yet on this but can you check the following part of your code and see why it is between parentheses?