Last active
October 22, 2017 15:13
-
-
Save klinyecviktor/3f818476a3b8e0afdd252fc9cb356323 to your computer and use it in GitHub Desktop.
This file contains 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
module.exports.getUserByUsername = function (username, callback) { | |
const query = { username }; | |
return new Promise((resolve, reject) => { | |
User.findOne(query, (error, user) => error ? reject(error) : resolve(user)); | |
}); | |
}; |
This file contains 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 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