1

当我测试以查看与护照 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);
                        });
                    }
                })
            }
        })
    })
);

如果电子邮件已经存在,如何在登录页面上向用户显示错误消息?

4

1 回答 1

0

我设法找到了解决方案。我在我的 facebook 回调中添加了失败重定向:

// 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/');
});

然后返回 null、false 和要显示的错误信息:

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);
                        });
                    }
                })
            }
        })
    })
);

我希望这可以帮助遇到同样问题的其他人:)

于 2018-03-16T00:59:58.823 回答