Skip to content

Instantly share code, notes, and snippets.

@klinyecviktor
Last active October 22, 2017 15:13
Show Gist options
  • Save klinyecviktor/3f818476a3b8e0afdd252fc9cb356323 to your computer and use it in GitHub Desktop.
Save klinyecviktor/3f818476a3b8e0afdd252fc9cb356323 to your computer and use it in GitHub Desktop.
module.exports.getUserByUsername = function (username, callback) {
const query = { username };
return new Promise((resolve, reject) => {
User.findOne(query, (error, user) => error ? reject(error) : resolve(user));
});
};
const UNKNOWN_MESSAGE = 'Unknown User';
const INVALID_MESSAGE = 'Invalid password';
passport.use(new LocalStrategy(((username, password, done) => {
let userStore;
User.getUserByUsername(username).then((user) => {
if (!user) {
return done(null, false, { message: UNKNOWN_MESSAGE });
}
userStore = user
return User.comparePassword(password, user.password);
}).then((isMatch) => {
if (isMatch) {
return done(null, userStore);
}
return done(null, false, { message: INVALID_MESSAGE });
}).catch(({ message }) => console.error(message));
})));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment