jaredhanson / passport-facebook

Facebook authentication strategy for Passport and Node.js.
https://www.passportjs.org/packages/passport-facebook/?utm_source=github&utm_medium=referral&utm_campaign=passport-facebook&utm_content=about
MIT License
1.29k stars 446 forks source link

Can not login after changing username #254

Closed dariatsenter closed 5 years ago

dariatsenter commented 5 years ago

Hi there! Hoping to get some help as I found zero information on how to fix this issue.

In my application, after the user signs up through passport-facebook I allow him to change his username. However, if he does so, then after logging out he is never let back in through facebook login.

I'm not changing facebookId, just the username, but somehow that breaks the login flow..

Here is my facebook strategy `passport.use(new FacebookStrategy({ clientID: process.env['FACEBOOK_APPID' + env], clientSecret: process.env['FACEBOOK_APPSECRET' + env], callbackURL: process.env[ 'WEBSITE_' + env] + "auth/facebook/callback", profileFields: ['id', 'emails'] }, function(accessToken, refreshToken, profile, done) { User.findOne({ username : profile.id}, function(err, user) { if (err) { return done(err); } if (!user){ const newUser = new User({ username: profile.emails[0].value.split('@')[0], facebookId: profile.id, email: profile.emails[0].value, numberOfLogs: 0 });

            newUser.save(function(err) {

                if(err) {
                    console.log(err);
                } else {
                    done(null, newUser);
                }
            });
        }else{
            //if a user already exists too
            done(null, user);
        }
    });
}

));`

My authentication app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email'], failureRedirect: '/', successRedirect: '/addlog'}));

And that's a sample user in the database after changing the username: { "_id": { "$oid": "5ba2ae50e484f5000494b62e" }, "username": "hi_hello", "facebookId": "101191784185559", "email": "daria.tsenter@gmail.com", "numberOfLogs": 0, "__v": 0 }

Thank you very much!

dariatsenter commented 5 years ago

Solved this one!

Turns out the problem was in that in Strategy I identified the users by username, whereas meant to by email.