students fked up
This commit is contained in:
6
apps/admin/app/(main)/actions.ts
Normal file
6
apps/admin/app/(main)/actions.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
'use server'
|
||||
import { signOut } from "@/auth";
|
||||
|
||||
export async function signOutAction() {
|
||||
await signOut();
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
Search,
|
||||
ChevronDown
|
||||
} from 'lucide-react';
|
||||
import { signOutAction } from './actions';
|
||||
|
||||
const navLinks = [
|
||||
{
|
||||
@@ -180,6 +181,7 @@ export default function MainLayout({ children }: { children: React.ReactNode })
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={signOutAction}
|
||||
className="w-full justify-start text-red-600 hover:text-red-700 hover:bg-red-50"
|
||||
>
|
||||
<LogOut className="w-4 h-4 mr-2" />
|
||||
|
||||
@@ -10,6 +10,7 @@ declare module 'next-auth' {
|
||||
role?: 'ADMIN' | 'USER';
|
||||
adminId?: number;
|
||||
studentId?: number;
|
||||
completedProfile?: boolean;
|
||||
[key: string]: any;
|
||||
} & DefaultSession["user"];
|
||||
}
|
||||
@@ -18,6 +19,7 @@ declare module 'next-auth' {
|
||||
role?: 'ADMIN' | 'USER';
|
||||
adminId?: number;
|
||||
studentId?: number;
|
||||
completedProfile?: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,13 +32,14 @@ declare module 'next/server' {
|
||||
const authConfig: NextAuthConfig = {
|
||||
providers: [Google],
|
||||
callbacks: {
|
||||
async jwt({ token, account, user, profile }) {
|
||||
async jwt({ token, account, user }) {
|
||||
// Only check DB on first sign in
|
||||
if (account && user && user.email) {
|
||||
const admin = await db.select().from(admins).where(eq(admins.email, user.email)).limit(1);
|
||||
if (admin.length > 0 && admin[0]) {
|
||||
token.role = 'ADMIN';
|
||||
token.adminId = admin[0].id;
|
||||
token.completedProfile = true;
|
||||
} else {
|
||||
token.role = 'USER';
|
||||
const student = await db
|
||||
@@ -46,6 +49,7 @@ const authConfig: NextAuthConfig = {
|
||||
.limit(1);
|
||||
if (student.length > 0 && student[0]) {
|
||||
token.studentId = student[0].id;
|
||||
token.completedProfile = student[0].rollNumber ? true : false;
|
||||
} else {
|
||||
const nameParts = user.name?.split(' ') ?? [];
|
||||
const firstName = nameParts[0] || '';
|
||||
@@ -61,6 +65,7 @@ const authConfig: NextAuthConfig = {
|
||||
.returning({ id: students.id });
|
||||
if (newStudent[0]) {
|
||||
token.studentId = newStudent[0].id;
|
||||
token.completedProfile = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,6 +82,9 @@ const authConfig: NextAuthConfig = {
|
||||
if (token?.studentId) {
|
||||
session.user.studentId = token.studentId as number;
|
||||
}
|
||||
if (token?.completedProfile !== undefined) {
|
||||
session.user.completedProfile = token.completedProfile as boolean;
|
||||
}
|
||||
return session;
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user