Skip to content

Instantly share code, notes, and snippets.

@olegpolyakov
Last active December 17, 2018 22:52
Show Gist options
  • Save olegpolyakov/0c8e2995511c5045f7966cf81092a079 to your computer and use it in GitHub Desktop.
Save olegpolyakov/0c8e2995511c5045f7966cf81092a079 to your computer and use it in GitHub Desktop.
Passport JWT Strategy
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