refactor: put passport to auth folder

This commit is contained in:
wancat committed 2023-09-11 23:38:29 +08:00
1 parent 41111f5036
commit 030dc95e24
8 files changed
+58 -76

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
import bcrypt from 'bcrypt'
import LocalStrategy from 'passport-local'
import User from '../models/user.js'
async function verifyPassword(username, password, done) {
try {
const user = await User.findOne({ username })
if (!user) {
return done(null, false, { message: 'Incorrect username.' })
}
const isPasswordValid = await bcrypt.compare(password, user.hashedPassword)
if (!isPasswordValid) {
return done(null, false, { message: 'Incorrect username or password.' })
}
return done(null, user)
} catch (err) {
return done(err)
}
}
export default new LocalStrategy(verifyPassword)