mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 05:51:55 +08:00
refactor: Migrate auth layout to server-side redirection with improved layout structure
This commit is contained in:
parent
df3ffe0cd5
commit
47e9d686d6
@ -1,27 +1,24 @@
|
|||||||
"use client";
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
import { useEffect } from "react";
|
import { getCurrentUser } from "@/lib/session";
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { useSession } from "next-auth/react";
|
|
||||||
|
|
||||||
export default function AuthLayout({
|
export default async function AuthLayout({
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}) {
|
}) {
|
||||||
const { data: session, status } = useSession();
|
const user = await getCurrentUser();
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
useEffect(() => {
|
if (user) {
|
||||||
if (status === "authenticated") {
|
if (user.role === "ADMIN") redirect("/admin");
|
||||||
if (session?.user?.role === "ADMIN") {
|
redirect("/dashboard");
|
||||||
router.push("/admin");
|
}
|
||||||
} else {
|
|
||||||
router.push("/dashboard");
|
|
||||||
}
|
|
||||||
router.refresh();
|
|
||||||
}
|
|
||||||
}, [session, status, router]);
|
|
||||||
|
|
||||||
return <>{children}</>;
|
return (
|
||||||
|
<div className="min-h-screen">
|
||||||
|
<div className="flex h-screen w-screen flex-col items-center justify-center">
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user