Spaces:
Running
Running
axeronis commited on
Commit Β·
8aa33fe
1
Parent(s): f38c921
Fix: allow /verify-email in middleware
Browse files5 unmodified lines678910111222 unmodified lines353637383940414243444546474849501 unmodified line525354555657583 unmodified lines62636465666768697071727374757677787980815 unmodified lines// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// Public routes (no auth required)const publicRoutes = ['/', '/login', '/register']// Auth routes (redirect if logged in)const authRoutes = ['/login', '/register']22 unmodified linesexport function middleware(request: NextRequest) {const { pathname } = request.nextUrl// Get token from cookiesconst token = request.cookies.get('auth_token')?.valueconst isPublicRoute = publicRoutes.includes(pathname)const isAuthRoute = authRoutes.includes(pathname)const isOwnerOnlyRoute = ownerOnlyRoutes.some(route => pathname.startsWith(route))console.log(`π£οΈ [Middleware] ${pathname} | Token: ${token ? 'YES' : 'NO'} | Public: ${isPublicRoute}`)// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// STEP 1: Redirect authenticated users away from auth pages// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ1 unmodified lineconsole.log('π [Middleware] Authenticated user accessing auth page -> /dashboard')return NextResponse.redirect(new URL('/dashboard', request.url))}// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// STEP 2: Redirect unauthenticated users to login// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ3 unmodified linesloginUrl.searchParams.set('from', pathname)return NextResponse.redirect(loginUrl)}// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// STEP 3: Role-based access control (OWNER vs MANAGER)// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// NOTE: Role verification happens client-side in AuthContext and Sidebar// Middleware just handles basic token presence// For strict server-side role enforcement, we'd need to decode JWT here// TODO: If needed, decode JWT to check role:// const user = decodeJWT(token)// if (isOwnerOnlyRoute && user.role !== 'OWNER') {// return NextResponse.redirect(new URL('/dashboard', request.url))// }console.log('β [Middleware] Access granted')return NextResponse.next()}5 unmodified lines678910111222 unmodified lines353637383940414243444546474849501 unmodified line525354555657583 unmodified lines62636465666768697071727374757677787980815 unmodified lines// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// Public routes (no auth required)const publicRoutes = ['/', '/login', '/register', '/verify-email']// Auth routes (redirect if logged in)const authRoutes = ['/login', '/register']22 unmodified linesexport function middleware(request: NextRequest) {const { pathname } = request.nextUrl// Get token from cookiesconst token = request.cookies.get('auth_token')?.valueconst isPublicRoute = publicRoutes.includes(pathname)const isAuthRoute = authRoutes.includes(pathname)const isOwnerOnlyRoute = ownerOnlyRoutes.some(route => pathname.startsWith(route))console.log(`π£οΈ [Middleware] ${pathname} | Token: ${token ? 'YES' : 'NO'} | Public: ${isPublicRoute}`)// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// STEP 1: Redirect authenticated users away from auth pages// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ1 unmodified lineconsole.log('π [Middleware] Authenticated user accessing auth page -> /dashboard')return NextResponse.redirect(new URL('/dashboard', request.url))}// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// STEP 2: Redirect unauthenticated users to login// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ3 unmodified linesloginUrl.searchParams.set('from', pathname)return NextResponse.redirect(loginUrl)}// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// STEP 3: Role-based access control (OWNER vs MANAGER)// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// NOTE: Role verification happens client-side in AuthContext and Sidebar// Middleware just handles basic token presence// For strict server-side role enforcement, we'd need to decode JWT here// TODO: If needed, decode JWT to check role:// const user = decodeJWT(token)// if (isOwnerOnlyRoute && user.role !== 'OWNER') {// return NextResponse.redirect(new URL('/dashboard', request.url))// }console.log('β [Middleware] Access granted')return NextResponse.next()}
frontend/middleware.ts+9-9