我正在使用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