Last active
June 24, 2018 07:38
-
-
Save AndrejGajdos/e3e57a16ed18e3f3ead0a3ea8e3a5a29 to your computer and use it in GitHub Desktop.
Passport local strategy. Whole file is available https://github.com/AndrejGajdos/auth-flow-spa-node-react/blob/master/script/controllers/auth.js
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
passport.use( | |
new LocalStrategy( | |
{ | |
usernameField: 'email', | |
passwordField: 'password', | |
}, | |
async (email, password, done) => { | |
let user = null; | |
await getAsync('usersMockDatabase').then((users) => { | |
const currUsers = JSON.parse(users); | |
user = currUsers.find(currUser => currUser.email === email); | |
}); | |
if (!user) { | |
done({ type: 'email', message: 'No such user found' }, false); | |
return; | |
} | |
if (bcrypt.compareSync(password, user.password)) { | |
done(null, { id: user.id, email: user.email, userName: user.userName }); | |
} else { | |
done({ type: 'password', message: 'Passwords did not match' }, false); | |
} | |
}, | |
), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment