限制登录访问 - Passport.js,谷歌认证(Restricting Login Acces

2019-08-03 10:14发布

好了,所以使用passport.js的作品,并且效果很好,从我所看到的。 但是,我不知道如何正确地排除某些用户。 如果应用程序的目的是限制访问,而不是仅仅向用户提供用于登录的方法,我怎么能限制通过passport.js登录? 既然这样,用户就可以只需访问/login ,并与他们的谷歌帐户登录,从而获得进入内部。

Answer 1:

这里是要做到这一点,在整个意见的一种方式。 更主要的是从作者理解这一页: http://passportjs.org/guide/authenticate/ ,我解释在这个例子中多一点...

这可能是更容易阅读底部到顶部:

var authenticate = function(req, success, failure) {

    // Use the Google strategy with passport.js, but with a custom callback.
    // passport.authenticate returns Connect middleware that we will use below.
    //
    // For reference: http://passportjs.org/guide/authenticate/
    return passport.authenticate('google', 
        // This is the 'custom callback' part
        function (err, user, info) {

            if (err) { 
                failure(err);
            }
            else if (!user) { 
                failure("Invalid login data");
            }
            else {
                // Here, you can do what you want to control 
                // access. For example, you asked to deny users 
                // with a specific email address:
                if (user.emails[0].value === "no@emails.com") {
                    failure("User not allowed");
                }
                else {
                    // req.login is added by the passport.initialize() 
                    // middleware to manage login state. We need 
                    // to call it directly, as we're overriding
                    // the default passport behavior.
                    req.login(user, function(err) {
                        if (err) { 
                            failure(err);
                        }
                        success();
                    });
                }
            }
        }
    );
};

一种想法是包裹在上面的代码中的一些中间件,以使其更易于阅读:

// This defines what we send back to clients that want to authenticate
// with the system.
var authMiddleware = function(req, res, next) {

    var success = function() {
        res.send(200, "Login successul");
    };

    var failure = function(error) {
        console.log(error);
        res.send(401, "Unauthorized"); 
    };

    var middleware = authenticate(req, success, failure);
    middleware(req, res, next);
};


// GET /auth/google/return
//   Use custom middleware to handle the return from Google.
//   The first /auth/google call can remain the same.
app.get('/auth/google/return', authMiddleware);

(这一切都假定我们正在使用快递)。



Answer 2:

试试这个。

googleLogin: function(req, res) {
        passport.authenticate('google', { failureRedirect: '/login', scope: ['https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email'] }, function(err, user) {
          req.logIn(user, function(err) {
            if (err) {
              console.log(err);
              res.view('500');
              return;
            }
            var usrEmail = user['email'];
                 if(usrEmail.indexOf("@something.com") !== -1)
                 {
                 console.log('successful');
                 res.redirect('/');
                 return;
                 }
                 else
                 {
                 console.log('Invalid access');
                 req.logout();
                 res.view('403');
                 return;
                 }

          });
        })(req, res);
      }

*



文章来源: Restricting Login Access - Passport.js, Google Authentication