mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 11:30:13 +08:00
refactor: auth
This commit is contained in:
1 parent
d6c95cea4a
commit
5489de2432
8 files changed
+120
-22
No files matched your search
@@ -0,0 +1,38 @@
|
||||
/* eslint-disable no-unused-vars */
|
||||
import bcrypt from 'bcrypt'
|
||||
import { Strategy as LocalStrategy } from 'passport-local'
|
||||
import type { IVerifyOptions } from 'passport-local'
|
||||
import type { Express } from 'express'
|
||||
import { User } from '@linux-odyssey/models'
|
||||
|
||||
async function verifyPassword(
|
||||
username: string,
|
||||
password: string,
|
||||
done: (
|
||||
error: any,
|
||||
user?: Express.User | false,
|
||||
options?: IVerifyOptions
|
||||
) => void
|
||||
) {
|
||||
try {
|
||||
const user = await User.findOne({
|
||||
$or: [{ username }, { email: username }],
|
||||
})
|
||||
if (!user) {
|
||||
return done(null, false, { message: 'Incorrect username or password.' })
|
||||
}
|
||||
|
||||
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)
|
||||
Reference in new issue
Block a user