From 95c9314d03ed84e93c6774daa08eaeef6c82a265 Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Sat, 29 Jul 2023 21:38:59 -0600 Subject: [PATCH] Role overhaul --- prisma/schema.prisma | 10 ++--- src/pages/admin/index.tsx | 42 +++++++++--------- src/pages/dashboard/index.tsx | 4 +- src/pages/profile/index.tsx | 80 ++++++++++++++++++---------------- src/pages/room/[id].tsx | 7 +-- src/server/api/routers/user.ts | 41 ++++++++++++----- src/server/auth.ts | 16 ++++--- src/utils/types.ts | 10 +---- 8 files changed, 114 insertions(+), 96 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 41ff26a..edcd219 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,9 +1,3 @@ -enum RoleValue { - USER - ADMIN - VIP -} - generator client { provider = "prisma-client-js" } @@ -47,7 +41,6 @@ model Session { model User { id String @id @default(cuid()) - role RoleValue @default(USER) createdAt DateTime @default(now()) name String? email String? @unique @@ -58,6 +51,9 @@ model User { rooms Room[] votes Vote[] logs Log[] + isAdmin Boolean @default(false) + isVIP Boolean @default(false) + } model VerificationToken { diff --git a/src/pages/admin/index.tsx b/src/pages/admin/index.tsx index 0a081d0..9d9a86f 100644 --- a/src/pages/admin/index.tsx +++ b/src/pages/admin/index.tsx @@ -7,7 +7,6 @@ import { IoTrashBinOutline } from "react-icons/io5"; import { SiGithub, SiGoogle } from "react-icons/si"; import { GiStarFormation } from "react-icons/gi"; import { api } from "~/utils/api"; -import type { Role } from "~/utils/types"; import { getServerAuthSession } from "../../server/auth"; export const getServerSideProps: GetServerSideProps = async (ctx) => { @@ -23,7 +22,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => { }; } - if (session.user.role !== "ADMIN") { + if (!session.user.isAdmin) { ctx.res.statusCode = 403; return { redirect: { @@ -91,7 +90,8 @@ const AdminBody: React.FC = () => { id: string; }[]; id: string; - role: Role; + isAdmin: boolean; + isVIP: boolean; name: string | null; email: string | null; }) => { @@ -120,7 +120,13 @@ const AdminBody: React.FC = () => { }, }); - const setRoleMutation = api.user.setRole.useMutation({ + const setAdminMutation = api.user.setAdmin.useMutation({ + onSuccess: async () => { + await refetchData(); + }, + }); + + const setVIPMutation = api.user.setVIP.useMutation({ onSuccess: async () => { await refetchData(); }, @@ -138,8 +144,12 @@ const AdminBody: React.FC = () => { await clearSessionsMutation.mutateAsync(); }; - const setUserRoleHandler = async (userId: string, role: Role) => { - await setRoleMutation.mutateAsync({ userId, role }); + const setAdmin = async (userId: string, value: boolean) => { + await setAdminMutation.mutateAsync({ userId, value }); + }; + + const setVIP = async (userId: string, value: boolean) => { + await setVIPMutation.mutateAsync({ userId, value }); }; const refetchData = async () => { @@ -264,36 +274,28 @@ const AdminBody: React.FC = () => { diff --git a/src/pages/dashboard/index.tsx b/src/pages/dashboard/index.tsx index 7ed5495..7e20c68 100644 --- a/src/pages/dashboard/index.tsx +++ b/src/pages/dashboard/index.tsx @@ -59,10 +59,10 @@ const HomePageBody: React.FC = () => { <>

Hi, {sessionData?.user.name}!{" "} - {sessionData?.user.role === "ADMIN" && ( + {sessionData?.user.isAdmin && ( )} - {sessionData?.user.role === "VIP" && ( + {sessionData?.user.isVIP && ( )}

diff --git a/src/pages/profile/index.tsx b/src/pages/profile/index.tsx index e7196e9..27b53ac 100644 --- a/src/pages/profile/index.tsx +++ b/src/pages/profile/index.tsx @@ -9,6 +9,7 @@ import { FaShieldAlt } from "react-icons/fa"; import { SiGithub, SiGoogle } from "react-icons/si"; import { api } from "~/utils/api"; import { getServerAuthSession } from "../../server/auth"; +import { GiStarFormation } from "react-icons/gi"; export const getServerSideProps: GetServerSideProps = async (ctx) => { const session = await getServerAuthSession(ctx); @@ -103,7 +104,7 @@ const ProfileBody: React.FC = () => { @@ -114,79 +115,82 @@ const ProfileBody: React.FC = () => {

Profile:

- { sessionData.user.image && ( -
- { sessionData.user.role === "ADMIN" && ( - -
- -
-
- ) } + {sessionData.user.image && ( + Profile picture. + )} - Profile picture. -
- ) } +
+ {sessionData.user.isAdmin && ( +
+ +
+ )} + {sessionData.user.isVIP && ( +
+ +
+ )} +
- { providersLoading ? ( + {providersLoading ? (
- { " " } + {" "}
) : (
- ) } + )} - { sessionData.user.name && ( + {sessionData.user.name && ( setNameText(event.target.value) } + value={nameText} + onChange={(event) => setNameText(event.target.value)} /> - ) } + )} - { sessionData.user.email && ( + {sessionData.user.email && ( - ) } + )} - {/* */ } + {/* */}