I'm trying to set up facebook authentication using passport from node.js. When I run the app and go to /auth/facebook, I go to a facebook page to give permission for facebook to return my email.Here's my code
Front-end.tsx
<a href='http://localhost:3001/auth/facebook'>
<FacebookButton>Signup with Facebook</FacebookButton>
</a>
Backend.ts
const { id, secret }: FACEBOOK_APP_SECRET = config.get('Facebook');
const FacebookStrategy = passportFacebook.Strategy;
passport.use(
new FacebookStrategy(
{
clientID: id,
clientSecret: secret,
callbackURL: 'http://localhost:3001/auth/facebook/callback',
profileFields: ['id', 'displayName', 'link', 'email'],
},
function (accessToken, refreshToken, profile, cb, done) {
console.log(profile);
console.log(profile.email);
userModel.findOne({ facebook: profile.id }, (err: NativeError, existingUser: User) => {
if (err) {
console.log(err);
return done(err);
}
if (existingUser) {
console.log(existingUser);
return done(undefined, existingUser);
} else {
userModel.findOne({ email: profile._json.email }, (err: NativeError, existingEmailUser: User) => {
if (err) {
console.log(err);
return done(err);
}
if (existingEmailUser) {
done(err);
} else {
const user: any = new userModel();
user.email = profile._json.email;
user.facebook = profile.id;
user.save((err: Error) => {
done(err, user);
});
}
});
}
});
},
),
);
Route.ts
this.router.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'public_profile'] }));
this.router.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }), function (req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
So right now when I print out the profile , it will return the followings
{
access_token: 'xxxxxxxxxxxxxxxxxxx',
token_type: 'bearer',
expires_in: 5181888
}
and with profile.email ,it will only return undefined
I search for a while , and other people are wrong at they did not attach { scope: ['email', 'public_profile'] } at their code which I did, so I don't know what is the problem here, why can't I return email in the profile ?