当我测试以查看与护照 facebook 策略关联的电子邮件地址是否已存在于数据库中时,我试图显示一条错误消息。
这是我的代码:
// 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);
});
}
})
}
})
})
);
如果电子邮件已经存在,如何在登录页面上向用户显示错误消息?