aboutsummaryrefslogtreecommitdiff
path: root/server/config/passport.js
blob: 02ad4f6a3e30e57b17cfe246852b070a9e543d8a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const passport = require('passport');
const jwtStrategy = require('passport-jwt').Strategy;
const extractJwt = require('passport-jwt').ExtractJwt;

const {masterKey} = require("../config/env");
const User = require('../models/User');

const options = {
  jwtFromRequest: extractJwt.fromAuthHeaderAsBearerToken(),
  secretOrKey: masterKey
};

const jwtStrategyCallback = (jwtPayload, done) => {
  // Try to find user
  User.findOne({_id: jwtPayload.sub}, (err, user) => {
    // Database error
    if (err)
      return done(err, false);

    // No user found
    if (!user)
      return done(null, false);

    // Successful login
    return done(null, user);
  })
};

passport.use(new jwtStrategy(options, jwtStrategyCallback));