Fix-auth-hashing-update-worklog
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Metadata } from "next"
|
||||
import { hash } from "bcryptjs"
|
||||
import { createId } from "@paralleldrive/cuid2"
|
||||
import { eq } from "drizzle-orm"
|
||||
|
||||
@@ -10,6 +11,12 @@ export const metadata: Metadata = {
|
||||
description: "Create an account",
|
||||
}
|
||||
|
||||
const normalizeBcryptHash = (value: string) => {
|
||||
if (value.startsWith("$2")) return value
|
||||
if (value.startsWith("$")) return `$2b${value}`
|
||||
return `$2b$${value}`
|
||||
}
|
||||
|
||||
export default function RegisterPage() {
|
||||
async function registerAction(formData: FormData): Promise<ActionState> {
|
||||
"use server"
|
||||
@@ -37,11 +44,12 @@ export default function RegisterPage() {
|
||||
})
|
||||
if (existing) return { success: false, message: "该邮箱已注册" }
|
||||
|
||||
const hashedPassword = normalizeBcryptHash(await hash(password, 10))
|
||||
await db.insert(users).values({
|
||||
id: createId(),
|
||||
name: name.length ? name : null,
|
||||
email,
|
||||
password,
|
||||
password: hashedPassword,
|
||||
role: "student",
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user