|
1 | 1 | import { NextResponse } from 'next/server'
|
2 | 2 |
|
3 | 3 | export function middleware(request) {
|
4 |
| - // Get the pathname of the request and remove the base path |
5 |
| - const path = request.nextUrl.pathname.replace('/crypt-webapp', '') |
| 4 | + const path = request.nextUrl.pathname |
6 | 5 |
|
7 |
| - // Define public paths that don't require authentication |
8 |
| - const isPublicPath = path === '/' || |
9 |
| - path === '/login' || |
10 |
| - path === '/register' |
| 6 | + // Only login and register are public |
| 7 | + const isPublicPath = path === '/crypt-webapp/' || |
| 8 | + path === '/crypt-webapp/login' || |
| 9 | + path === '/crypt-webapp/register' |
11 | 10 |
|
12 |
| - // Get the token from the cookies |
13 | 11 | const token = request.cookies.get('token')?.value || ''
|
14 | 12 |
|
15 |
| - // Get the base URL for redirects |
16 |
| - const baseUrl = new URL('/crypt-webapp', request.url).toString().replace(/\/$/, '') |
17 |
| - |
18 |
| - // Redirect authenticated users away from login/register pages |
| 13 | + // If logged in, redirect to dashboard from public pages |
19 | 14 | if (isPublicPath && token) {
|
20 |
| - return NextResponse.redirect(`${baseUrl}/dashboard`) |
| 15 | + return NextResponse.redirect(new URL('/crypt-webapp/dashboard', request.url)) |
21 | 16 | }
|
22 | 17 |
|
23 |
| - // Redirect unauthenticated users to login page |
| 18 | + // If not logged in, redirect to login from private pages |
24 | 19 | if (!isPublicPath && !token) {
|
25 |
| - return NextResponse.redirect(`${baseUrl}/login`) |
| 20 | + return NextResponse.redirect(new URL('/crypt-webapp/login', request.url)) |
26 | 21 | }
|
27 | 22 | }
|
28 | 23 |
|
|
0 commit comments