【发布时间】:2014-12-04 19:26:22
【问题描述】:
我正在使用NodeJS 开发REST api。对于身份验证,我决定使用Passport。我想要真正的 RESTful api。所以这意味着我必须使用令牌而不是会话。
我想让用户使用用户名和密码登录,或者使用 Facebook、Google 和 Twitter 等社交网络。
我制作了自己的OAuth2.0 服务器,用于使用oauth2orize 模块发布Access 和Refresh tokens。所以现在我可以注册新用户,然后给他们发放令牌。
我遵循了本教程:
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 句柄以便进一步通信。谢谢:)
-
而不是aleksandrov教程,为什么不能使用passport-oauth? passportjs.org/guide/oauth
-
为什么还要为自己的服务器实施 oAuth?为什么不使用本地护照,然后通过不记名策略发行令牌?
标签: node.js express passport.js access-token facebook-access-token