到目前为止,我一直在使用客户端身份验证。我刚刚设置了服务器端 everyauth,它运行得很好,但是当页面加载时,如何执行类似于 FB.getLoginStatus
(我在客户端执行的操作)?
如果用户已经登录,我不想让他们再次经历这个过程。
谢谢您的解释!
到目前为止,我一直在使用客户端身份验证。我刚刚设置了服务器端 everyauth,它运行得很好,但是当页面加载时,如何执行类似于 FB.getLoginStatus
(我在客户端执行的操作)?
如果用户已经登录,我不想让他们再次经历这个过程。
谢谢您的解释!
从文档中了解到:
everyauth还在ServerRequest实例req上提供了方便的方法。只要有权访问req的任何范围,您就可以获得以下方便的getter和方法:
req.loggedIn
- 一个布尔getter,告诉您请求是否由已登录用户发起
req.user
- 与会话相关联的User文档
req.logout()
- 清除身份验证数据的sesion
在Express视图中:
If you are using express, everyauth comes with some useful dynamic helpers. To enable them:
var express = require('express') , everyauth = require('everyauth') , app = express.createServer(); everyauth.helpExpress(app);
Then, from within your views, you will have access to the following helpers methods attached to the helper, everyauth:
everyauth.loggedIn
var userlogin = function () {
var req = this.request
,res = this.response
,login = req.body
,username = login.username
,password = login.password;
if(username === 'tester' && password === '12345'){
req.session.user_id = xxxxxx;
res.redirect('/');
}else{
res.redirect('/login');
}
}
var checkAuth = function(fn_auth, fn_unauth){
var req = this.request
,res = this.response
,unauthCallback;
if(req.session.user_id){
fn_auth(); //execute fn_auth if user is logged in. callback can be set to redirect to root path '/'
}else{
fn_unauth(); //execute fn_unauth calback which bring user to login path '/login'
}
}
}
if (!everyauth.loggedIn) // <-----
a(href="/your_url_here/") log in with facebook
else
p Welcome <username>
app.get('/', function(req, res){ if(req.loggedIn){res.send('you're logged in')} });
- fancy