import { redirect } from 'next/navigation' import Image from 'next/image' import { auth } from '@/lib/auth' import { prisma } from '@/lib/prisma' export default async function AuthLayout({ children, }: { children: React.ReactNode }) { let session = null try { session = await auth() } catch (error) { // Auth check failed, continue as unauthenticated console.error('Auth check failed in auth layout:', error) } // Redirect logged-in users to their dashboard // But NOT if they still need to set their password if (session?.user && !session.user.mustSetPassword) { // Verify user still exists in DB and check onboarding status const dbUser = await prisma.user.findUnique({ where: { id: session.user.id }, select: { id: true, onboardingCompletedAt: true }, }) if (dbUser) { // If user hasn't completed onboarding, don't redirect away from auth pages. // The /onboarding page lives in this (auth) layout, so they need to stay here. if (!dbUser.onboardingCompletedAt) { // Fall through — let them access /onboarding (and other auth pages) } else { const role = session.user.role if (role === 'SUPER_ADMIN' || role === 'PROGRAM_ADMIN') { redirect('/admin') } else if (role === 'JURY_MEMBER') { redirect('/jury') } else if (role === 'OBSERVER') { redirect('/observer') } else if (role === 'MENTOR') { redirect('/mentor') } else if (role === 'APPLICANT') { redirect('/applicant') } } } // If user doesn't exist in DB, fall through and show auth page } return (