Files
linux-odyssey/server/src/auth/passwordStrategy.ts
T
Justin Lin 2feedc329c refactor: use bun for package management
+ add package.json to each packages
+ update import path
2026-10-11 16:50:01 +11:00

43 lines
1.0 KiB
TypeScript

/* 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 '@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, {
id: user.id,
username: user.username,
email: user.email,
})
} catch (err) {
return done(err)
}
}
export default new LocalStrategy(verifyPassword)