簡體   English   中英

頁面未使用 express-session 和通行證 nodejs 正確重定向

[英]Page is not properly redirecting with express-session and passport nodejs

我正在使用本地護照和快速會話,但我的問題是當我輸入地址時,關閉會話后,我遇到瀏覽器“頁面未正確重定向”的問題,所以我想做的是重定向到地址/登錄名而不是顯示該問題

我會留下我的代碼

路由.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
        });
    }

    });

應用程序.js

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

 }));


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

更新解決了

驗證我做了我在 app.get call 中調用的函數

函數 isLoggedIn (req, res, next) {

if (req.isAuthenticated()) 返回 next();

Res.redirect('/登錄'); }

嘗試使用return res.redirect而不是res.redirect並告訴我會發生什么。 (渲染相同)

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

在護照中,有一個內置函數,req.isAuthenticated()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM