Skip to content

Commit 3e06e70

Browse files
committed
refactor local strategy is passport to async/await
1 parent 616b1fd commit 3e06e70

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

server/config/passport.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,31 @@ passport.deserializeUser((id, done) => {
3535
* Sign in using Email/Username and Password.
3636
*/
3737
passport.use(
38-
new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
39-
User.findByEmailOrUsername(email)
40-
.then((user) => {
38+
new LocalStrategy(
39+
{ usernameField: 'email' },
40+
async (email, password, done) => {
41+
try {
42+
const user = await User.findByEmailOrUsername(email);
43+
4144
if (!user) {
42-
done(null, false, { msg: `Email ${email} not found.` });
43-
return;
45+
return done(null, false, { msg: `Email ${email} not found.` });
4446
} else if (user.banned) {
45-
done(null, false, { msg: accountSuspensionMessage });
46-
return;
47+
return done(null, false, { msg: 'Your account has been suspended.' });
4748
}
48-
user.comparePassword(password).then((isMatch) => {
49-
if (isMatch) {
50-
done(null, user);
51-
} else {
52-
done(null, false, { msg: 'Invalid email or password.' });
53-
}
54-
});
55-
})
56-
.catch((err) => done(null, false, { msg: err }));
57-
})
49+
50+
const isMatch = await user.comparePassword(password);
51+
52+
if (isMatch) {
53+
return done(null, user);
54+
} else { // eslint-disable-line
55+
return done(null, false, { msg: 'Invalid email or password' });
56+
}
57+
} catch (err) {
58+
console.error(err);
59+
return done(null, false, { msg: err });
60+
}
61+
}
62+
)
5863
);
5964

6065
/**

0 commit comments

Comments
 (0)