forked from GridApe/v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
47 lines (40 loc) · 1.39 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
42
43
44
45
46
47
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const protectedPaths = ['/api/user', '/api/contacts'];
const authPages = ['/auth/login', '/auth/register'];
const isProtectedPath = protectedPaths.some((path) => request.nextUrl.pathname.startsWith(path));
const isAuthPage = authPages.includes(request.nextUrl.pathname);
const isHomePage = request.nextUrl.pathname === '/';
const token = request.cookies.get('token')?.value;
if (isAuthPage && token) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
// Redirect users from the home page
if (isHomePage) {
return token
? NextResponse.redirect(new URL('/dashboard', request.url))
: NextResponse.redirect(new URL('/auth/login', request.url));
}
// Protect API routes
if (isProtectedPath && !token) {
return NextResponse.json(
{
success: false,
error: 'Unauthorized',
message: 'No token provided, please login first.',
},
{ status: 401 }
);
}
// Attach token for protected API routes
if (isProtectedPath && token) {
const response = NextResponse.next();
response.headers.set('Authorization', `Bearer ${token}`);
return response;
}
return NextResponse.next();
}
export const config = {
matcher: ['/', '/auth/:path*', '/api/:path*'],
};