我正在使用NodeJS
的REST
API。为了验证,我决定使用Passport
。我想要真正的RESTful api。所以这意味着我必须使用令牌而不是会话。使用Passport和OAuth2 +社交网络的NodeJS REST身份验证
我想让用户使用用户名和密码登录,或使用Facebook,Google和Twitter等社交网络。
我制作我自己的OAuth2.0
服务器用于发行Access
和Refresh tokens
使用oauth2orize
模块。所以现在我可以注册新用户,然后发布它们的令牌。 我跟着这个教程:
http://aleksandrov.ws/2013/09/12/restful-api-with-nodejs-plus-mongodb/
验证用户的路线:
// api ------------------------------------------------------------------------------------
app.get('/api/userInfo',
passport.authenticate('bearer', { session: false }),
function(req, res) {
// req.authInfo is set using the `info` argument supplied by
// `BearerStrategy`. It is typically used to indicate scope of the token,
// and used in access control checks. For illustrative purposes, this
// example simply returns the scope in the response.
res.json({ user_id: req.user.userId, name: req.user.username, scope: req.authInfo.scope })
}
);
这一切都工作得很好。不幸的是我不知道如何实现社交认证。
我阅读本教程:
http://scotch.io/tutorials/javascript/easy-node-authentication-facebook
但在本教程中他们没有做一个真正的RESTful API。根据本教程,我已经实现了用户模式,其中本地用户的令牌存储在分离的模型中。
// define the schema for our user model
var userSchema = mongoose.Schema({
local: {
username: {
type: String,
unique: true,
required: true
},
hashedPassword: {
type: String,
required: true
},
created: {
type: Date,
default: Date.now
}
},
facebook: {
id: String,
token: String,
email: String,
name: String
},
twitter: {
id: String,
token: String,
displayName: String,
username: String
},
google: {
id: String,
token: String,
email: String,
name: String
}
});
但现在,我该如何验证用户?
passport.authenticate('bearer', { session: false }),
这是验证只有持票人令牌反对我的数据库,但我如何验证社交令牌?我错过了什么吗?
你能详细说明你最终如何解决这个问题吗?你还可以留下你的Twitter手柄进一步通信。谢谢:) – 2015-04-15 21:29:32
而不是aleksandrov教程,为什么不能使用passport-oauth? http://passportjs.org/guide/oauth/ – 2015-04-16 00:49:59
也为什么为你自己的服务器实现oAuth?为什么不使用护照本地,然后通过载体策略发布令牌? – 2015-04-16 12:16:31