【问题标题】:Page is not properly redirecting with express-session and passport nodejs页面未使用 express-session 和护照 nodejs 正确重定向
【发布时间】:2017-11-02 17:37:05
【问题描述】:

我正在使用 passport-local 和 express-session 但我的问题是当我输入地址时,在我关闭会话后,我遇到了浏览器“页面未正确重定向”的问题,所以我想要做什么被重定向到地址/登录而不是显示那个问题

我会留下我的代码

Routes.js

app.get('/index', isLoggedIn, function(req, res) {
    console.log(req.session);
        if (req.session == null  || req.session == undefined)
        {
             res.redirect('/login');
        }
        else
        {
        res.render('index.ejs', {
            user : req.user // get the user out of session and pass to template
        });
    }

    });

app.js

 app.use(session({
     secret  : 'asjdknas',

 }));


 app.use(passport.initialize());
 app.use(passport.session());
 app.use(flash());

更新已解决

验证我做了我在 app.get 调用中调用的函数

函数 isLoggedIn (req, res, next) {

如果(req.isAuthenticated()) 返回下一个();

Res.redirect('/login'); }

【问题讨论】:

    标签: javascript node.js session express


    【解决方案1】:

    在passport中,有一个内置函数req.isAuthenticated()

    【讨论】:

      【解决方案2】:

      尝试使用return res.redirect 而不是res.redirect 并告诉我会发生什么。 (渲染相同)

      另外,console.log(req.session) 给你什么?

      【讨论】:

      • 与返回相同的发生 = (,console.log 在使用会话 Session { Cookie: {Path: '/', _expires: null, OriginalMaxAge: null, HttpOnly: true} ,护照:{用户:1}}
      • 这意味着req.session 不是nullundefined,因此您没有输入重定向条件。但我猜你之前尝试过并成功进入条件?您应该在 res.redirect 之前添加一个 console.log('Hello') 以检查您是否确实在其中
      • 489/5000 我刚查了一下,问题是它没有进入,“页面没有正确重定向”的错误,控制台只返回这个 GET / index 302 12,686 ms - 56 GET /索引 302 2,300 ms - 56 GET / 索引 302 1,232 ms - 56 GET / 索引 302 0.906 ms - 56 GET / 索引 302 0.729 ms - 56 GET / 索引 302 0.551 ms - 56 GET / 索引 302 0.795 ms - 56 GET / 索引 302 1,430 毫秒 - 56 GET / 索引 302 1,189 毫秒 - 56 GET / 索引 302 2.079 毫秒 - 56 GET / 索引 302 0.706 毫秒 - 56 GET / 索引 302 0.664 毫秒 - 56
      • 非常感谢解决方法是将 si 放在函数 islogged 中
      • 你是怎么做到的?我是新来的:(如果你解释,我会做的
      猜你喜欢
      • 1970-01-01
      • 2017-01-18
      • 2011-08-11
      • 2018-06-08
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多