-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
48 lines (40 loc) · 1.53 KB
/
Copy pathmiddleware.ts
File metadata and controls
48 lines (40 loc) · 1.53 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
// Get the pathname of the request
const { pathname } = request.nextUrl;
// Define protected routes
const protectedRoutes = ['/dashboard', '/chat', '/resources', '/crisis'];
const authRoutes = ['/auth'];
// Check if the current path is a protected route
const isProtectedRoute = protectedRoutes.some(route => pathname.startsWith(route));
const isAuthRoute = authRoutes.some(route => pathname.startsWith(route));
// Get the token from cookies or headers
const token = request.cookies.get('heal_access_token')?.value ||
request.headers.get('authorization')?.replace('Bearer ', '');
// If it's a protected route and there's no token, redirect to auth
if (isProtectedRoute && !token) {
const url = request.nextUrl.clone();
url.pathname = '/auth';
return NextResponse.redirect(url);
}
// If it's an auth route and there's a token, redirect to dashboard
if (isAuthRoute && token) {
const url = request.nextUrl.clone();
url.pathname = '/dashboard';
return NextResponse.redirect(url);
}
return NextResponse.next();
}
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)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
};