如果用户经过身份验证,就阻止他们再次登录 Passportjs?

Ani*_*ngh 2 mongoose node.js passport-local passport.js

我正在使用 PassportJS,注册和登录功能运行得非常顺利。

我使用 PassportJS 面临的唯一问题(我也在使用会话),即使用户已经登录,他们也可以再次返回到注册/登录 url 并进行注册和/或重新登录!

这很诱惑我。如果有人有修复/建议,请把它写下来。

更新 - 1

我的一瞥routes.js:(使用 PassportJS 和 connet-ensure-login。

app.get('*', function(req, res, next) {
    if (req.url.indexOf('/users/login') < 0 &&
        req.url.indexOf('/users/signup') < 0) {
        req.session.returnTo = null;
    }
    next();
});

// =====================================
// HOME PAGE (with login links) ========
// =====================================
app.get('/', sabSettings, function(req, res) {
    Setting.findOne(function(err, setting) {
        if (err)
            throw err;
        // console.log(setting);
        res.render('index', { title: 'eduBird | Reach the glory', setting: req.setting }); // load the index file
    });
});

// =====================================
// LOGIN ===============================
// =====================================
// show the login form
app.get('/login', sabSettings, function(req, res) {

    // render the page and pass in any flash data if it exists
    res.render('login', {
        message: req.flash('loginMessage'),
        errors: req.flash('error'),
        title: 'Login | eduBird',
        setting: req.setting
    });
});

// process the login form

app.post('/login', passport.authenticate('local-login', {
    successReturnToOrRedirect: '/loggedin',
    failureRedirect: '/login',
    failureFlash: true
}));

// =====================================
// SIGNUP ==============================
// =====================================
// show the signup form
app.get('/signup', sabSettings, function(req, res) {

    // render the page and pass in any flash data if it exists
    process.nextTick(function() {
        res.render('signup', {
            message: req.flash('signupMessage'),
            errors: req.flash('error'),
            title: 'Register | eduBird',
            setting: req.setting
        });
    });
});

// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
    successReturnToOrRedirect: '/profile/welcome',
    failureRedirect: '/signup',
    failureFlash: true
}));
Run Code Online (Sandbox Code Playgroud)

小智 6

您尚未创建任何类型的访问控制,但不用担心,我们将首先了解 Passport 的工作原理并使用它来解决问题。

  1. 当用户提交登录表单时,会向我们指定的路径发出 POST 请求,从而导致执行 Passport.authenticate。
  2. 该路由的身份验证中间件配置为处理本地策略,passport 将调用本地策略的实现。
  3. 如果与数据库交互时发生错误,我们会调用done(err)。否则,如果找不到用户或密码不匹配,我们将调用done(null, false)。如果成功,我们调用done(null, user)。

调用done将使我们返回到passport.authenticate,并执行相应的重定向。

此时,如果登录成功,用户对象(来自 did(null, user))将附加到请求中,您可以通过req.user访问用户对象。

主要思想是,如果用户对象未附加到请求,则意味着用户未登录,因此我们可以使用 req.user 控制登录用户的应用程序行为。例如:

// If the user object does not exist it means the user is not logged in
    if (!req.user) {
        res.render('signin');
    } else {
// If the user object exists, the user is logged in and if they try to log in we redirect them to the home page
        return res.redirect('/');
    }
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助。