-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
25 lines (19 loc) · 753 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
25 lines (19 loc) · 753 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
25
import { getToken } from 'next-auth/jwt';
import { NextRequest, NextResponse } from 'next/server';
export { default } from 'next-auth/middleware';
export async function middleware(request: NextRequest) {
const sensitiveRoutes = ['/dashboard', '/money'];
const publicRoutes = ['/'];
const pathname = request.nextUrl.pathname;
const token = await getToken({ req: request });
const isAuth = !!token;
if (isAuth && publicRoutes.some((route) => route === pathname)) {
return NextResponse.redirect(new URL('/money', request.url));
}
if (!isAuth && sensitiveRoutes.some((route) => pathname.startsWith(route))) {
return NextResponse.redirect(new URL('/', request.url));
}
}
export const config = {
matcher: ['/dashboard', '/money', '/'],
};