passport js authentication from different schemas

39 views
Skip to first unread message

Sachin Rajput

unread,
Jan 7, 2015, 8:07:02 AM1/7/15
to nod...@googlegroups.com
Hi,

I am using passport js bearer strategy for session authentication.

passport.use(new BearerStrategy(
  function(token, done) {
    Student.findOne({ token: token }, function (err, student) {
      if (err) { return done(err); }
      if (!student) { return done(null, false); }
return done(null, student, { scope: 'all' });
}); } ));

which get fire whenever a request comes having '/secure' prefix.

router.all('/secure/*',passport.authenticate('bearer', { session: false }),function(req,res,next){
	next();
});

but this authentication is done only for students. Suppose if there is teachers also so how will I do the same?

Adrien Risser

unread,
Jan 7, 2015, 10:28:30 AM1/7/15
to nod...@googlegroups.com
Hi,

You might need concept of a "User" instead of just Student / Teacher.

passport.use(new BearerStrategy(
  function(token, done) {
    User.findOne({ token: token }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
return done(null, user, { scope: 'all' });
}); } ));

Linking which user has which role is then up to you.

You could add a "type" or "role" property to your User obejcts or create a join table a la SQL

Cheers,

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/635ae1b8-aefb-4a29-8116-06c75ecfa240%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Adrien Risser,
Freelance Node.js Consultant
Reply all
Reply to author
Forward
0 new messages