import type { User } from './auth' import { UserRole } from '@admin/types/auth' import { getAdminPostLoginPath } from '@admin/utils/auth/get-admin-post-login-path' import { isUserRole } from '@admin/utils/auth/is-user-role' import { headers } from 'next/headers' import { redirect } from 'next/navigation' import { cache } from 'react' import { auth } from './auth' export { UserRole } from '@admin/types/auth' interface GetSessionOptions { disableCookieCache?: boolean } export const getSession = cache(async (options: GetSessionOptions = {}) => { const session = await auth.api.getSession({ query: options.disableCookieCache ? { disableCookieCache: true } : undefined, headers: await headers() }) return session }) export async function requireRole(allowedRoles: readonly UserRole[]): Promise { const session = await getSession({ disableCookieCache: true }) if (!session?.user) { redirect('/admin/login') } const user = session.user const role = isUserRole(user.role) ? user.role : UserRole.MEMBER if (!allowedRoles.includes(role)) { redirect(getAdminPostLoginPath(role)) } return user }