-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
24 lines (20 loc) · 790 Bytes
/
middleware.ts
File metadata and controls
24 lines (20 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getToken } from 'next-auth/jwt';
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
const secret = process.env.NEXTAUTH_SECRET;
const token = await getToken({ req: request, secret });
const isAuthenticated = !!token;
const isLoginPage = request.nextUrl.pathname === '/login';
// Redirect authenticated users away from login page to dashboard
if (isAuthenticated && isLoginPage) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
// Allow all other routes to proceed
return NextResponse.next();
}
// Match only the login page
export const config = {
matcher: ['/login'],
};