我有一个与Phonegap
服务器通信的Nodejs
应用程序。
同样对于Facebook\Twitter
登录我正在使用Hello.js
库(顺便说一句,这很容易使用)。
不幸的是这个库只进行客户端登录(身份验证),因此在服务器端我不知道用户是否有效(已经被Facebook \ Twitter嵌入)。
编辑:
用户登录后(客户端),Hello.js
提供用户凭据,具有Facebook唯一用户ID,但我不知道如何将其安全地传递到服务器,或者通常是一个好主意将它用作我的数据库中的用户ID。
我正在寻找一个简单的例子来检查服务器端登录的有效性。
感谢。
答案 0 :(得分:2)
如果您使用的是https,那么将ID发送到您的服务器就可以了。您可以做的只是检查您的数据库中是否已存在该唯一ID,并返回该用户数据(如果需要)或创建新帐户。
我还建议在服务器端创建一个JWT(JSON Web Token)并将其发送回应用程序以存储在本地存储中,并用于验证将来对节点服务器的所有请求。如果在所有路由中使用jwt.verify方法作为中间件,则可以非常轻松地实现该方法。
这是一个简单的例子:
var jwt = require('jsonwebtoken');
var jwtValidation = function(req, res, next) {
var token = req.body.jwt_token;
if (token) {
jwt.verify(token, 'yourSecretKeyHere', function(err, decoded) {
if (err) {
// Error when checking JWT - redirect to unauthorized
res.redirect('/unauthorized');
} else if (decoded.id) {
// Token that was passed in has been decoded
// Check your DB for the decoded.id
// Complete any other needed tasks then call next();
next();
} else {
// Something else went wrong - redirect to unauthorized
res.redirect('/unauthorized');
}
});
} else {
// No token present - redirect to unauthorized
res.redirect('/unauthorized');
}
};
module.exports = jwtValidation;
答案 1 :(得分:1)
这是我想的主要想法:
在Phonegap应用程序中,用户登录后,将调用此函数:
hello.on('auth.login', function(r){
var token = r.authResponse.access_token;
}
现在,您只能将token
发送到服务器,服务器将直接从Facebook
获取用户凭据。
例如,在Facebook
中,请调用此usr: