forked from enkhbold470/hackathon-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (24 loc) · 958 Bytes
/
middleware.ts
File metadata and controls
31 lines (24 loc) · 958 Bytes
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
31
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
// Get the pathname
const path = request.nextUrl.pathname
// Define public paths that don't require authentication
const isPublicPath = path === '/login'
// Check if user is logged in (via cookie)
const isAuthenticated = request.cookies.has('admin_authenticated')
// Redirect logic
if (!isPublicPath && !isAuthenticated) {
// Redirect to login if trying to access protected pages without auth
return NextResponse.redirect(new URL('/login', request.url))
}
if (isPublicPath && isAuthenticated) {
// Redirect to dashboard if already logged in and trying to access login page
return NextResponse.redirect(new URL('/', request.url))
}
return NextResponse.next()
}
// Configure the paths this middleware runs on
export const config = {
matcher: ['/', '/login']
}