1

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?

1 Answers1

0

I have managed to find a solution. I added a failure redirect to my facebook callback:

// callback route for facebook to redirect to
router.get('/facebook/redirect', passport.authenticate('facebook', {
    successRedirect: '/profile/',
    failureRedirect: '/auth/login', 
    failureFlash: true
}), (req, res) => {
res.redirect('/profile/');
});

And then returned null, false and the error message to be displayed:

passport.use(
    new FacebookStrategy({
        clientID: keys.facebook.clientID,
        clientSecret: keys.facebook.clientSecret,
        callbackURL: '/auth/facebook/redirect',
        profileFields: ['id', 'email', 'displayName', 'picture.type(large)']
    }, (err, accessToken, refreshToken, profile, done) => {
        User.findOne({
            facebookID: profile.id
        }).then((currentUser) => {
            if (currentUser) {
                done(null, currentUser);
            } else {
                User.findOne({ email: profile.emails[0].value }).then((response) => {
                    if (response) {
                        done(null, false, {message: "Email already exists"}); // <-- Where I return the callback
                    } 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);
                        });
                    }
                })
            }
        })
    })
);

I hope this helps anyone else who is having the same problem :)