Last active
December 17, 2018 22:52
-
-
Save olegpolyakov/0c8e2995511c5045f7966cf81092a079 to your computer and use it in GitHub Desktop.
Passport JWT Strategy
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 JwtStrategy = require('passport-jwt').Strategy; | |
const ExtractJwt = require('passport-jwt').ExtractJwt; | |
const User = require('../../shared/models/data').User; | |
const errorHandler = require('../../shared/utils').errorHandler; | |
const options = { | |
secretOrKey: cfg.jwtSecret, | |
jwtFromRequest: ExtractJwt.fromAuthHeader() | |
}; | |
function auth(payload, done) { | |
User.findOne({ id: payload.id }) | |
.then(user => { | |
if (!user || !user.validatePassword(password)) { | |
done(null, false, { message: 'Неверный логин или пароль' }); | |
} else { | |
done(null, user); | |
} | |
}) | |
.catch(error => done(error)); | |
} | |
module.exports = new JwtStrategy(options, auth); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment