refactor: extract password hashing functions into a dedicated password module

This commit is contained in:
Firman Ramdhani
2026-10-01 13:48:48 +07:00
parent 6d6d127126
commit d932d9af1c
3 changed files with 11 additions and 10 deletions
+2 -1
View File
@@ -3,7 +3,8 @@
import prisma from '@/lib/prisma' import prisma from '@/lib/prisma'
import { revalidatePath } from 'next/cache' import { revalidatePath } from 'next/cache'
import { hashPassword, verifyPassword, encrypt, decrypt } from '@/lib/auth' import { encrypt, decrypt } from '@/lib/auth'
import { hashPassword, verifyPassword } from '@/lib/password'
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
import { sendMattermostNotification } from '@/lib/mattermost' import { sendMattermostNotification } from '@/lib/mattermost'
-9
View File
@@ -1,18 +1,9 @@
import { SignJWT, jwtVerify } from 'jose' import { SignJWT, jwtVerify } from 'jose'
import bcrypt from 'bcryptjs'
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
const secretKey = process.env.JWT_SECRET const secretKey = process.env.JWT_SECRET
const key = new TextEncoder().encode(secretKey) const key = new TextEncoder().encode(secretKey)
export async function hashPassword(password: string) {
return await bcrypt.hash(password, 10)
}
export async function verifyPassword(password: string, hash: string) {
return await bcrypt.compare(password, hash)
}
export async function encrypt(payload: any) { export async function encrypt(payload: any) {
return await new SignJWT(payload).setProtectedHeader({ alg: 'HS256' }).setIssuedAt().sign(key) // No expiration as requested return await new SignJWT(payload).setProtectedHeader({ alg: 'HS256' }).setIssuedAt().sign(key) // No expiration as requested
} }
+9
View File
@@ -0,0 +1,9 @@
import bcrypt from 'bcryptjs'
export async function hashPassword(password: string) {
return await bcrypt.hash(password, 10)
}
export async function verifyPassword(password: string, hash: string) {
return await bcrypt.compare(password, hash)
}