Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

feat: admin page and added role to session (#89)

Ayush7945410758fa
diff --git a/src/app/admin/page.tsx b/src/app/admin/page.tsx
new file mode 100644
index 00000000..cea68734
--- /dev/null
+++ b/src/app/admin/page.tsx
@@ -0,0 +1,29 @@
+'use client';
+
+import { Lock } from 'lucide-react';
+import { useSession } from 'next-auth/react';
+import Head from 'next/head';
+
+export default function Admin() {
+ const session = useSession();
+ return (
+ <div>
+ <Head>
+ <title>Admin</title>
+ </Head>
+ {session.data?.user.role === 'ADMIN' ? (
+ <div className="flex flex-col">{/** Admin Content Go Here */}</div>
+ ) : (
+ <div
+ className="flex w-full flex-col items-center justify-center space-y-2"
+ style={{ minHeight: 'calc(100dvh - 112px)' }}
+ >
+ <Lock className="h-8 w-8 text-red-500" />
+ <span className="max-w-[40ch] text-center text-black/50 dark:text-white/50">
+ You do not have enough permissions to access the page.
+ </span>
+ </div>
+ )}
+ </div>
+ );
+}
diff --git a/src/server/auth.ts b/src/server/auth.ts
index 8b9fb617..50bc1851 100644
--- a/src/server/auth.ts
+++ b/src/server/auth.ts
@@ -1,6 +1,7 @@
import { PrismaAdapter } from '@next-auth/prisma-adapter';
+import { Role } from '@prisma/client';
import { type GetServerSidePropsContext } from 'next';
-import { getServerSession, type NextAuthOptions, type DefaultSession } from 'next-auth';
+import { getServerSession, type DefaultSession, type NextAuthOptions } from 'next-auth';
import GitHubProvider from 'next-auth/providers/github';
import { env } from '~/env.mjs';
import { prisma } from '~/server/db';
@@ -15,15 +16,13 @@ declare module 'next-auth' {
interface Session extends DefaultSession {
user: {
id: string;
- // ...other properties
- // role: UserRole;
+ role: Role;
} & DefaultSession['user'];
}
- // interface User {
- // // ...other properties
- // // role: UserRole;
- // }
+ interface User {
+ role: Role;
+ }
}
export const authOptions: NextAuthOptions = {
@@ -33,6 +32,7 @@ export const authOptions: NextAuthOptions = {
user: {
...session.user,
id: user.id,
+ role: user.role,
},
}),
},