aboutsummaryrefslogtreecommitdiffstats
path: root/passport.js
blob: c85c9a52beb4a1471b4f21fed99ab21b76aa6945 (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
30
31
32
33
const passport = require('passport')
const jwt = require('passport-jwt')
const dotenv = require('dotenv')
dotenv.config()

require('dotenv').config()

const cookieExtractor = function(req) {
  let token = null
  if (req && req.cookies) {
    token = req.cookies['token']
  }
  return token
}

passport.use(new jwt.Strategy({
    jwtFromRequest: jwt.ExtractJwt.fromExtractors([cookieExtractor]),
    secretOrKey: process.env.SECRET
  }, async function (payload, next) {
    const db = await require('./db')
    db.User.findOne({
      where: {
        username: payload.username
      } 
    }).then((user) => {
      next(null, user)
    }).catch((err) => {
      next(null, false)
    })
  })
)

module.exports = passport