conditionally render the admin bar based on role rather than just if they are signed in

This commit is contained in:
Brandon Egger 2023-05-22 23:14:37 -05:00
parent f63f141d72
commit f2343acb70
2 changed files with 5 additions and 1 deletions

View File

@ -1,3 +1,4 @@
import { Role } from "@prisma/client";
import { useSession } from "next-auth/react"; import { useSession } from "next-auth/react";
const AdminBarLayout = ({ const AdminBarLayout = ({
@ -11,7 +12,7 @@ const AdminBarLayout = ({
return ( return (
<div className="relative"> <div className="relative">
{data?.user ? ( {data?.user.role === Role.ADMIN ? (
<div className="sticky left-0 right-0 top-[71px] z-10 mx-auto mb-6 mt-[15px] flex max-w-4xl flex-row justify-between rounded-xl border border-neutral-600 bg-red-300 drop-shadow-xl"> <div className="sticky left-0 right-0 top-[71px] z-10 mx-auto mb-6 mt-[15px] flex max-w-4xl flex-row justify-between rounded-xl border border-neutral-600 bg-red-300 drop-shadow-xl">
<h1 className="rounded-lg px-4 py-2 font-semibold text-black"> <h1 className="rounded-lg px-4 py-2 font-semibold text-black">
Admin Mode Admin Mode

View File

@ -9,11 +9,13 @@ import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { prisma } from "~/server/db"; import { prisma } from "~/server/db";
import { loginSchema } from "~/lib/validation/auth"; import { loginSchema } from "~/lib/validation/auth";
import { verify } from "argon2"; import { verify } from "argon2";
import { type Role } from "@prisma/client";
interface SessionUser { interface SessionUser {
id: string; id: string;
name: string; name: string;
username: string; username: string;
role: Role;
} }
/** /**
@ -100,6 +102,7 @@ export const authOptions: NextAuthOptions = {
id: result.id, id: result.id,
name: result.name, name: result.name,
username, username,
role: result.role,
}; };
} catch { } catch {
return null; return null;