简体   繁体   English

使用Passport,Node.js的用户组

[英]User groups with Passport, Node.js

Looking for some pointers here. 在这里寻找一些指针。

I want to create a page with a login form as the landing page and when you log in you will be taken to your own dashboard. 我想创建一个带有登录表单的页面作为登录页面,登录时将带到您自己的仪表板。 However if you log in with an as a user with admin rights they will be taken to an admin dashboard. 但是,如果您以具有管理员权限的用户身份登录,他们将被带到管理控制台。

What is the best way to achieve that? 做到这一点的最佳方法是什么? Just have some middleware that checks if they are an admin? 只是有一些中间件检查他们是否是管理员?

Thanks Miles 感谢迈尔斯

login.js file : login.js文件:

var findUser = function(req, res, next) {
    var user = User.find({email:req.body.email},function(err,user){
     if (!user) {
        res.locals.error = 'No such account exists.'
        return res.render('frontend/login')
    }
    next()
    });
}

var authenticate = function(req, res, next) {

  passport.authenticate('local', function(err, user, info) {
            if (err || !user) {
                console.log('ATUH ERROR');
                            console.log(err , user,info)
                res.locals.error = 'Invalid login credentials provided.'
                return res.render('frontend/login')
            }
            req.logIn(user, function(err2) {
                if (err2) {
                    console.log('LOGIN TIME ERROR')
                    res.locals.error = 'Internal system error while logging in.'
                    return res.render('frontend/login')
                }
                console.log('user redirect');
                if(user.role=='user'){
                  res.redirect('/user/dashboard');
                }

                if(user.role=='admin'){
                  res.redirect('/admin/dashboard');
                }

            })
  })(req, res, next)
}


module.exports = [findUser, authenticate]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM