BUG FIX && 权限验证
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import { Metadata } from "next"
|
||||
import { createId } from "@paralleldrive/cuid2"
|
||||
import { eq } from "drizzle-orm"
|
||||
|
||||
import { db } from "@/shared/db"
|
||||
import { users } from "@/shared/db/schema"
|
||||
import type { ActionState } from "@/shared/types/action-state"
|
||||
import { RegisterForm } from "@/modules/auth/components/register-form"
|
||||
|
||||
export const metadata: Metadata = {
|
||||
@@ -7,5 +13,33 @@ export const metadata: Metadata = {
|
||||
}
|
||||
|
||||
export default function RegisterPage() {
|
||||
return <RegisterForm />
|
||||
async function registerAction(formData: FormData): Promise<ActionState> {
|
||||
"use server"
|
||||
|
||||
const name = String(formData.get("name") ?? "").trim()
|
||||
const email = String(formData.get("email") ?? "").trim().toLowerCase()
|
||||
const password = String(formData.get("password") ?? "")
|
||||
|
||||
if (!email) return { success: false, message: "Email is required" }
|
||||
if (!password) return { success: false, message: "Password is required" }
|
||||
if (password.length < 6) return { success: false, message: "Password must be at least 6 characters" }
|
||||
|
||||
const existing = await db.query.users.findFirst({
|
||||
where: eq(users.email, email),
|
||||
columns: { id: true },
|
||||
})
|
||||
if (existing) return { success: false, message: "Email already registered" }
|
||||
|
||||
await db.insert(users).values({
|
||||
id: createId(),
|
||||
name: name.length ? name : null,
|
||||
email,
|
||||
password,
|
||||
role: "student",
|
||||
})
|
||||
|
||||
return { success: true, message: "Account created" }
|
||||
}
|
||||
|
||||
return <RegisterForm registerAction={registerAction} />
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { eq } from "drizzle-orm"
|
||||
import { getTeacherClasses } from "@/modules/classes/data-access"
|
||||
import { MyClassesGrid } from "@/modules/classes/components/my-classes-grid"
|
||||
import { auth } from "@/auth"
|
||||
import { db } from "@/shared/db"
|
||||
import { grades } from "@/shared/db/schema"
|
||||
|
||||
export const dynamic = "force-dynamic"
|
||||
|
||||
@@ -9,6 +13,16 @@ export default function MyClassesPage() {
|
||||
|
||||
async function MyClassesPageImpl() {
|
||||
const classes = await getTeacherClasses()
|
||||
const session = await auth()
|
||||
const role = String(session?.user?.role ?? "")
|
||||
const userId = String(session?.user?.id ?? "").trim()
|
||||
|
||||
const canCreateClass = await (async () => {
|
||||
if (role === "admin") return true
|
||||
if (!userId) return false
|
||||
const [row] = await db.select({ id: grades.id }).from(grades).where(eq(grades.gradeHeadId, userId)).limit(1)
|
||||
return Boolean(row)
|
||||
})()
|
||||
|
||||
return (
|
||||
<div className="flex h-full flex-col space-y-8 p-8">
|
||||
@@ -21,7 +35,7 @@ async function MyClassesPageImpl() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<MyClassesGrid classes={classes} />
|
||||
<MyClassesGrid classes={classes} canCreateClass={canCreateClass} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user