Last active
January 17, 2019 22:20
-
-
Save FermiDirak/cedef9d4d7e6c7bd500d4ce915940ce8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const passport = require('passport'); | |
const LocalStrategy = require('passport-local').Strategy; | |
const passportJWT = require('passport-jwt'); | |
const JWTStrategy = passportJWT.Strategy; | |
const bcrypt = require('bcrypt'); | |
const { secret } = require('./keys'); | |
const UserModel = require('./models/user'); | |
passport.use(new LocalStrategy({ | |
usernameField: username, | |
passwordField: password, | |
}, async (username, password, done) => { | |
try { | |
const userDocument = await UserModel.findOne({username: username}).exec(); | |
const passwordsMatch = await bcrypt.compare(password, userDocument.passwordHash); | |
if (passwordsMatch) { | |
return done(null, userDocument); | |
} else { | |
return done('Incorrect Username / Password'); | |
} | |
} catch (error) { | |
done(error); | |
} | |
})); | |
passport.use(new JWTStrategy({ | |
jwtFromRequest: req => req.cookies.jwt, | |
secretOrKey: secret, | |
}, | |
(jwtPayload, done) => { | |
if (Date.now() > jwtPayload.expires) { | |
return done('jwt expired'); | |
} | |
return done(null, jwtPayload); | |
} | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment