Passport facebook登录名:facebook无法打开,isAuthenticated()为false

时间:2019-05-02 23:52:17

标签: node.js express session passport.js passport-facebook

理想情况下,主页上有一个通用的登录按钮,该按钮会重定向到多个登录按钮,其中一个用于Facebook。用户单击该按钮,如果通过身份验证,则将其重定向到/my-reptiles/

但是,实际上发生的是,当用户单击“使用Facebook登录”时,他们并没有收到来自Facebook的确认提示,而是立即从{{1}中的ensureAuthenticated()方法重定向回到首页}}。在私人浏览中运行时,系统会要求我先登录到Facebook,然后再进行重定向,但我没有被要求批准使用Facebook作为登录方法。这正常吗?

即使my-reptiles.js失败,仍会为用户创建本地mongodb条目。

在Facebook开发者控制台上,我将应用程序域设置为isAuthenticated(),并将站点URL设置为localhost

这是控制台输出:

http://localhost:3000/

这是网络日志:

Network log

my-reptiles.js

GET /auth/facebook 302 4.309 ms - 0
GET /auth/facebook/callback?code=LONG_CODE_HERE 302 215.462 ms - 68
Unauthenticated request!
GET /my-reptiles 302 0.879 ms - 46
GET / 304 14.854 ms - -

app.js

function ensureAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return next();
    } else {
        console.log("Unauthenticated request!");
        res.redirect('/');
    }
}

router.get('/', ensureAuthenticated, function(req, res, next) {
    //Do stuff
});

module.exports = router;

1 个答案:

答案 0 :(得分:0)

问题是,根据this answer,我应该在路线之后添加会话和护照,而应该在之前添加会话和护照。

//Routes WERE here

const cookieExpirationDate = new Date();
const cookieExpirationDays = 365;
cookieExpirationDate.setDate(cookieExpirationDate.getDate() + cookieExpirationDays);

app.use(session({
    secret: 'sekret',
    saveUninitialized: true,
    resave: true
}));
app.use(passport.initialize());
app.use(passport.session());

//Routes SHOULD BE here
相关问题