mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 02:09:05 +03:00
29 lines
1023 B
JavaScript
29 lines
1023 B
JavaScript
const LocalStrategy = require('passport-local').Strategy;
|
||
const bcrypt = require('bcryptjs');
|
||
|
||
function initialize(passport, getUserByEmail, getUserById) {
|
||
const authenticateUser = async (email, password, done) => {
|
||
const user = getUserByEmail(email);
|
||
if (user == null) {
|
||
return done(null, false, { message: 'Администратор с такой почтой не найден!' });
|
||
}
|
||
|
||
try {
|
||
if (await bcrypt.compare(password, user.password)) {
|
||
return done(null, user);
|
||
} else {
|
||
return done(null, false, { message: 'Неверный пароль!' });
|
||
}
|
||
} catch (error) {
|
||
return done(error);
|
||
}
|
||
}
|
||
|
||
passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser));
|
||
passport.serializeUser((user, done) => done(null, user.id));
|
||
passport.deserializeUser((id, done) => {
|
||
return done(null, getUserById(id))
|
||
})
|
||
}
|
||
|
||
module.exports = initialize; |