forked from hemant-i7/curalink-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
100 lines (89 loc) · 3.23 KB
/
Copy pathmiddleware.ts
File metadata and controls
100 lines (89 loc) · 3.23 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { withAuth } from "next-auth/middleware"
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
export default withAuth(
function middleware(request: any) {
const token = request.nextauth.token
const { pathname } = request.nextUrl
// Fast path for public routes - no redirects needed
if (
pathname.startsWith('/login') ||
pathname.startsWith('/select-role') ||
pathname.startsWith('/api/auth') ||
pathname.startsWith('/_next') ||
pathname.startsWith('/favicon.ico') ||
pathname === '/'
) {
return NextResponse.next()
}
// Fast authentication check
if (!token) {
return NextResponse.redirect(new URL('/login', request.url))
}
const userRole = token.role as string
// Block access to faiz routes for everyone - redirect based on role
if (pathname.startsWith('/faiz/')) {
if (userRole === 'clinician') {
return NextResponse.redirect(new URL('/medical/dashboard', request.url))
} else if (userRole === 'patient') {
return NextResponse.redirect(new URL('/patient/dashboard', request.url))
} else {
return NextResponse.redirect(new URL('/select-role', request.url))
}
}
// Fast role-based redirects
if (!userRole && pathname !== '/select-role') {
return NextResponse.redirect(new URL('/select-role', request.url))
}
// Redirect clinicians to profile setup if they haven't completed it
if (userRole === 'clinician' && pathname.startsWith('/medical/') && pathname !== '/medical/profile-setup') {
// In a real app, you would check if profile is complete via API or token
// For now, we'll let them proceed and handle it in the dashboard
}
// Optimized route protection with early returns
if (userRole === 'clinician') {
if (pathname.startsWith('/patient/')) {
return NextResponse.redirect(new URL('/medical/dashboard', request.url))
}
if (pathname === '/dashboard') {
return NextResponse.redirect(new URL('/medical/dashboard', request.url))
}
} else if (userRole === 'patient') {
if (pathname.startsWith('/medical/')) {
return NextResponse.redirect(new URL('/patient/dashboard', request.url))
}
if (pathname === '/dashboard') {
return NextResponse.redirect(new URL('/patient/dashboard', request.url))
}
}
return NextResponse.next()
},
{
callbacks: {
authorized: ({ token, req }) => {
const { pathname } = req.nextUrl
// Fast public route check
return pathname.startsWith('/login') ||
pathname.startsWith('/select-role') ||
pathname.startsWith('/api/auth') ||
pathname.startsWith('/_next') ||
pathname.startsWith('/favicon.ico') ||
pathname === '/' ||
!!token
},
},
}
)
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder
*/
"/((?!api|_next/static|_next/image|favicon.ico|public).*)",
],
}