-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmiddleware.ts
More file actions
68 lines (60 loc) · 1.99 KB
/
middleware.ts
File metadata and controls
68 lines (60 loc) · 1.99 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
import { NextRequest, NextResponse } from 'next/server';
import { authMiddleware } from 'next-firebase-auth-edge';
import { clientConfig, serverConfig } from './lib/config';
import { isUserAuth } from './lib/auth';
const AUTH_PROTECTED_ROUTES = [
'/dashboard',
'/settings',
'/analytics',
'/leaderboard',
'/journal',
'/journal/new',
];
export async function middleware(request: NextRequest) {
const url = request.nextUrl.clone();
const path = url.pathname;
const isAuth = await isUserAuth();
if (path.startsWith('/leaderboard')) {
return NextResponse.rewrite(new URL('/not-found', request.url));
}
// Redirect to dashboard if logged in and accessing login/register routes
if (['/login', '/register'].includes(path) && isAuth) {
url.pathname = '/dashboard';
return NextResponse.redirect(url);
}
// Redirect to login if accessing any protected route without authentication
if (
AUTH_PROTECTED_ROUTES.some((route) => path.startsWith(route)) &&
!isAuth
) {
url.pathname = '/login';
return NextResponse.redirect(url);
}
// Redirect root to dashboard if authenticated
if (path === '/' && isAuth) {
url.pathname = '/dashboard';
return NextResponse.redirect(url);
}
// Default authentication middleware
return authMiddleware(request, {
loginPath: '/api/login',
logoutPath: '/api/logout',
apiKey: clientConfig.apiKey,
cookieName: serverConfig.cookieName,
cookieSignatureKeys: serverConfig.cookieSignatureKeys,
cookieSerializeOptions: serverConfig.cookieSerializeOptions,
serviceAccount: serverConfig.serviceAccount,
});
}
export const config = {
matcher: [
'/', // Root route
'/((?!_next|api|.*\\.).*)', // Exclude _next, API, and static files
'/api/(login|logout)', // Login/logout API routes
'/(login|register)', // Login and register routes
'/dashboard', // Dashboard route
'/journal/:path*', // Journal and its subpaths
'/settings', // Settings page
'/analytics', // Analytics page
],
};