-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
30 lines (23 loc) · 1.15 KB
/
Copy pathmiddleware.js
File metadata and controls
30 lines (23 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { NextResponse } from 'next/server'
// Marketing paths that authenticated users should bypass
const MARKETING_PATHS = ['/', '/about', '/services', '/blog', '/portfolio', '/faq', '/pricing', '/contact', '/enroll', '/live', '/certificates']
export function middleware(request) {
const { pathname } = request.nextUrl
const token = request.cookies.get('ga-admin')?.value
// Protect admin routes
if (pathname.startsWith('/admin') && pathname !== '/admin/login') {
if (!token) return NextResponse.redirect(new URL('/admin/login', request.url))
}
// Protect student portal and profile
if (pathname === '/portal' || pathname === '/profile') {
if (!token) return NextResponse.redirect(new URL('/login', request.url))
}
// Redirect authenticated users away from the identity/marketing website
if (token && MARKETING_PATHS.includes(pathname)) {
return NextResponse.redirect(new URL('/portal', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/admin/:path*', '/portal', '/profile', '/', '/about', '/services', '/blog', '/portfolio', '/faq', '/pricing', '/contact', '/enroll', '/live', '/certificates'],
}