Does passport.authenticated provide a callback
zacharynevin opened this issue · 4 comments
Earlier in my application, I was simply using session based authentication with passport. For this, passport provides a method called isAuthenticated().
However, now I don't have isAuthenticated() because I'm using token based authentication. Before, I had a middleware function called ensureAuth that would check if the user is authentication by the isAuthenticated function. I don't really want to split the ensureAuth function into two - one for the passport.authorize() function and one for the small step I intend to do afterwards before moving onto the next middleware. Let me explain.
For example, my current code is like this:
ensureAuth: function(req, res, next) {
// authenticate using the token authentication strategy
passport.authenticate('bearer', { session: false })
// user object stored in req.user
// set req.user.model as the single table inheritance model of the user (e.g. A or B)
// (I used to do rails and some conventions stuck)
var UserModel = app.get('Model.'+_str.capitalize(req.user.get('role')))
req.user.role = new UserModel({ id: req.user.id })
next();
}
and I want to do something more like this, because the example above doesn't work:
ensureAuth: function(req, res, next) {
// authenticate using the token authentication strategy
passport.authenticate('bearer', { session: false }, function() {
// user object stored in req.user
// set req.user.model as the single table inheritance model of the user (e.g. A or B)
var UserModel = app.get('Model.'+_str.capitalize(req.user.get('role')))
req.user.role = new UserModel({ id: req.user.id })
next();
})
}
So I guess my question is: does passport.authenticated() provide a callback? I couldn't find mention of it in the documentation.
Sorry if I sound like an idiot :(
I think I found it on StackOverflow. Sorry for wasting your time!
passport.authenticate('bearer', { session: false }, function(err, user, info) {
})
Actually, I'm reopening the issue as this didn't work.
Sorry again! I found a solution using passReqToCallback.
@zacharynevin - Can you provide more details on your solution?