-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
41 lines (33 loc) · 1.24 KB
/
middleware.ts
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
32
33
34
35
36
37
38
39
40
41
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// Define public paths that don't require authentication
const publicPaths = ['/', '/login', '/auth/callback']
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// Check if the path is public
const isPublicPath = publicPaths.some(path => pathname.startsWith(path))
// Get the token from the request
const token = request.cookies.get('access_token')?.value
console.log('Middleware running for path:', pathname)
console.log('Auth tokens:', { token })
console.log('Is public path?', isPublicPath)
// Redirect to login if accessing protected route without token
if (!isPublicPath && !token) {
const loginUrl = new URL('/login', request.url)
loginUrl.searchParams.set('service_name', 'FBR Tax Portal')
return NextResponse.redirect(loginUrl)
}
return NextResponse.next()
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public files (public directory)
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\..*|api).*)',
],
}