30 lines
682 B
TypeScript
30 lines
682 B
TypeScript
|
|
import { redirect } from 'next/navigation'
|
||
|
|
import type { Route } from 'next'
|
||
|
|
import { auth } from '@/lib/auth'
|
||
|
|
import type { UserRole } from '@prisma/client'
|
||
|
|
|
||
|
|
const ROLE_DASHBOARDS: Record<string, string> = {
|
||
|
|
SUPER_ADMIN: '/admin',
|
||
|
|
PROGRAM_ADMIN: '/admin',
|
||
|
|
JURY_MEMBER: '/jury',
|
||
|
|
MENTOR: '/mentor',
|
||
|
|
OBSERVER: '/observer',
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function requireRole(...allowedRoles: UserRole[]) {
|
||
|
|
const session = await auth()
|
||
|
|
|
||
|
|
if (!session?.user) {
|
||
|
|
redirect('/login')
|
||
|
|
}
|
||
|
|
|
||
|
|
const userRole = session.user.role
|
||
|
|
|
||
|
|
if (!allowedRoles.includes(userRole)) {
|
||
|
|
const dashboard = ROLE_DASHBOARDS[userRole]
|
||
|
|
redirect((dashboard || '/login') as Route)
|
||
|
|
}
|
||
|
|
|
||
|
|
return session
|
||
|
|
}
|