首页 > 解决方案 > 使用 Node.js Passport 进行动态重定向

问题描述

我还没有找到这个话题的最新答案;我希望我没有被卡住,因为我不理解旧的......

如果用户之前访问过给定页面,我想在登录后将用户重定向到不同的页面。

If thisVariable is true,
    Log the user in, 
    And redirect to /page1/thisVariable
Else,
    Log the user in
    And redirect to /page2.

使用 passeport,这是我原来的基本登录路线:

app.post('/login', passport.authenticate("local", { 
        successRedirect : '/',
        failureRedirect : '/login',
        failureFlash: "Please double check your credentials ❌"
    }), (req, res)=>{
        req.flash("success", "You're logged in ✅");
    }
);

我最好的尝试调整它是这段代码,它不起作用:

app.post('/login', (req, res)=>{

    console.log("VARIABLE=> ", req.body.variable) //(My variable does show up here)

    if(req.body.variable){
        passport.authenticate("local", {
            successRedirect : '/page1/'+req.body.variable,
            failureRedirect : '/login',
            failureFlash: "Please double check your credentials ❌"
    })
    req.flash("success", "You're logged in ✅");
    
    } else {
        passport.authenticate("local", {
            successRedirect : '/',
            failureRedirect : '/login',
            failureFlash: "Please double check your credentials ❌"
    })
    req.flash("success", "You're logged in ✅");
    }
});

该代码不会引发任何错误,请求仍处于待处理状态。

有什么办法可以把它改正吗?非常感谢 !

标签: node.jsauthenticationredirectpassport.js

解决方案


推荐阅读