TimTim
TimTim
TTCTheo's Typesafe Cult
Created by şiar_619 on 1/15/2025 in #questions
Admin authentication
You can check their role at the level of the page you want to display only to admin role users. If they're not an admin, redirect them to another page. Something like this
const ProtectedPage = () => {
const { data: session, status } = useSession();
const router = useRouter();

// If session is loading, display a loading indicator
if (status === "loading") {
return <p>Loading...</p>;
}

// If there's no session or the user role is not "user", redirect to unauthorized
if (!session || session.user?.role !== "user") {
router.push("/unauthorized");
return null; // Prevents rendering protected content
}

return (
<div>
<h1>Welcome to the Protected Page</h1>
<p>Your role: {session.user.role}</p>
</div>
);
};
const ProtectedPage = () => {
const { data: session, status } = useSession();
const router = useRouter();

// If session is loading, display a loading indicator
if (status === "loading") {
return <p>Loading...</p>;
}

// If there's no session or the user role is not "user", redirect to unauthorized
if (!session || session.user?.role !== "user") {
router.push("/unauthorized");
return null; // Prevents rendering protected content
}

return (
<div>
<h1>Welcome to the Protected Page</h1>
<p>Your role: {session.user.role}</p>
</div>
);
};
4 replies
TTCTheo's Typesafe Cult
Created by kgeek on 1/14/2025 in #questions
Next.js 15 App Router vs. React Router:
I would go with the built in next router unless you have a specific use case as to why it wouldn't satisfy what you need to do
5 replies