Javascript 谷歌+;登录-仅登录用户可访问的页面
我决定在我的页面上使用社交媒体的好处,目前我正在实施Google+登录 我的网站上的一个页面应该只允许登录用户访问(向页面添加内容)。我通过JavaScript将用户登录到网站Javascript 谷歌+;登录-仅登录用户可访问的页面,javascript,python,google-app-engine,google-plus,google-login,Javascript,Python,Google App Engine,Google Plus,Google Login,我决定在我的页面上使用社交媒体的好处,目前我正在实施Google+登录 我的网站上的一个页面应该只允许登录用户访问(向页面添加内容)。我通过JavaScript将用户登录到网站 我知道javascript是在客户端执行的,但我好奇的是,是否可以仅使用javascript来限制对特定页面的访问。这里有一种方法可以做到这一点,其中包括注释: var authenticate = function(req, success, failure) { // Use the Google stra
我知道javascript是在客户端执行的,但我好奇的是,是否可以仅使用javascript来限制对特定页面的访问。这里有一种方法可以做到这一点,其中包括注释:
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();
});
}
}
}
);
};
One idea is to wrap the above code in some more middleware, to make it easier to read:
// 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);
信用证:您无法仅使用客户端javascript执行可靠的访问控制 这是因为javascript是在用户的浏览器上执行的,用户将能够绕过您在那里设置的任何访问控制规则 在Python代码中,必须在服务器端执行访问控制
通常,人们也会在客户端执行某种访问控制检查,不是为了阻止访问,而是为了隐藏/禁用用户无法使用的按钮。最好在代码的来源添加信用。哎呀,错过了。更新答案。正如我所看到的,答案使用了Node.js,我在项目中没有使用,我希望避免使用它(我以前从未使用过Node.js)。