好的,所以使用passport.js工作,並且運行良好,從我所見過的。但是,我不確定如何正確排除某些用戶。如果應用程序的目的是限制訪問,而不僅僅是爲用戶提供登錄方法,那麼我如何通過passport.js限制登錄?目前,用戶只需訪問/login
並使用其Google帳戶登錄,即可訪問內部信息。限制登錄訪問 - Passport.js,谷歌身份驗證
3
A
回答
9
這裏有一種方法可以做到這一點,並帶有評論。更主要的是從作者理解這一頁: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 === "[email protected]") {
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);
(這一切都假定我們正在使用快遞。)
0
試試這個。
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);
}
*
相關問題
- 1. Passport.js用戶登錄和身份驗證
- 2. 谷歌登錄身份驗證失敗
- 3. Firebase登錄身份驗證與谷歌
- 4. 谷歌身份驗證登錄
- 5. 谷歌身份驗證C#
- 6. ExpressJs + Passport.js + MySQL身份驗證
- 7. Passport.js可選身份驗證
- 8. 谷歌應用程序身份驗證
- 9. 身份驗證firebase谷歌
- 10. Passport.js驗證用戶身份的問題
- 11. 谷歌閱讀器身份驗證
- 12. rhomobile的谷歌身份驗證
- 13. 谷歌身份驗證使用Xamarin.Auth
- 14. Symfony2.1 - HWIOAuthBundle和谷歌oauth身份驗證
- 15. 谷歌登錄身份驗證不起作用
- 16. 狀態碼12501使用谷歌登錄進行身份驗證
- 17. 使用谷歌賬號登錄 - 身份驗證
- 18. 無法解析符號'身份驗證'谷歌登錄集成
- 19. 身份驗證登錄
- 20. Symfony2登錄身份驗證
- 21. 登錄身份驗證
- 22. ASP.Net登錄身份驗證
- 23. NT身份驗證登錄
- 24. Facebook身份驗證登錄
- 25. 登錄身份驗證Asp.net
- 26. 登錄身份驗證
- 27. Google身份驗證 - 登錄
- 28. Facebook登錄身份驗證
- 29. PHP登錄身份驗證
- 30. Symfony2登錄身份驗證
哇,我很抱歉。我從未接受過這個。 – skeggse