使用passport.js登录后如何“重定向"以做出反应? [英] how to 'redirect' with react after login with passport.js?

查看:100
本文介绍了使用passport.js登录后如何“重定向"以做出反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手回答问题:我正在将它与passport.js一起使用并进行快递.我已成功登录该应用程序.但是我不知道如何进行重定向.

Newbie react question: I’m using it with passport.js and express. I’m successfully logging in to the application. But I don't know how to do a redirect.

router.post('/login',
  passport.authenticate('local'),
  function(req, res) {
    // If this function gets called, authentication was successful.
    res.redirect('/upload'); //<—— what I’d like to do is some kind of redirect here
});

任何帮助表示赞赏.

推荐答案

在您的回调路由上执行该操作.

Do that on your callback route.

router.get('/facebook', passport.authenticate('facebook', { scope: ['public_profile', 'email'] }));

router.get('/facebook/callback',
    passport.authenticate('facebook', { failureRedirect: "/" }), function (req, res) {
        if (req.user || req.session.user)
            return res.redirect('/' + req.user._id || req.session.user._id);
        return res.redirect('/login');
    });

在这里,我已经使用passport.js进行Facebook登录.在/facebook/callback中,我已重定向到/login,在/login中,我具有检查会话是否已设置的功能.

Here, I have used passport.js for Facebook Login. and in /facebook/callback I have redirected to /login and in /login I have a function to check if the session is set.

if (Helper.isLoggedIn(req)) {
    res.redirect('/');
    return;
}

Helper.jsisLoggedIn的定义是

function isLoggedIn(req) {
    if (req.session && req.session.user_email && req.session.user_email != null) {
        if (!req.user || req.session.user == undefined || req.session.user == null) {
            loadUserFrom(req);
        }
        return true;
    }
    return false;
}

这篇关于使用passport.js登录后如何“重定向"以做出反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆