I am trying to display an error message when I test to see if the email address associated to the passport facebook strategy already exists in the database.
Here is my code:
// callback route for facebook to redirect to
router.get('/facebook/redirect', passport.authenticate('facebook'), (req, res) => {
res.redirect('/profile/');
});
// Facebook Strategy
passport.use(
new FacebookStrategy({
clientID: keys.facebook.clientID,
clientSecret: keys.facebook.clientSecret,
callbackURL: '/auth/facebook/redirect',
profileFields: ['id', 'email', 'displayName', 'photos']
}, (err, accessToken, refreshToken, profile, done) => {
User.findOne({ email: profile.emails[0].value }).then((response) => {
if(response) {
console.log('Email already exists.'); // This is where I get my response
} else {
User.findOne({
facebookID: profile.id
}).then((currentUser) => {
if(currentUser) {
done(null, currentUser);
} else {
new User({
name: profile.displayName,
username: profile.displayName + ' Facebook',
facebookID: profile.id,
email: profile.emails[0].value,
avatar: profile.photos ? profile.photos[0].value : '/img/faces/unknown-user-pic.jpg'
}).save().then((newUser) => {
done(null, newUser);
});
}
})
}
})
})
);
How do I display an error flash message to the user on the login page if the email already exists?