I'm working on REST
api using NodeJS
. For authentication I decided to use Passport
. I want truly RESTful api. So it means I have to use tokens instead of sessions.
I want to let users login using username and password, or using social networks like Facebook, Google and Twitter.
I make my own OAuth2.0
server for issuing Access
and Refresh tokens
using oauth2orize
module. So now I can register new user and then issue them tokens.
I followed this tutorial:
http://aleksandrov.ws/2013/09/12/restful-api-with-nodejs-plus-mongodb/
Verifying user for route:
// 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 })
}
);
All this works quite well. Unfortunately I don't know how to implement social authentication.
I was reading this tutorial:
http://scotch.io/tutorials/javascript/easy-node-authentication-facebook
But in this tutorial they are not making a truly RESTful api. I already implemented user schema according this tutorial where tokens for local user are stored in separated models.
// 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
}
});
But now, how can I verify user?
passport.authenticate('bearer', { session: false }),
this is verifying only bearer token against to my db, but how can I verify social tokens? Am I missing something?