-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
35 lines (31 loc) · 1010 Bytes
/
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
// middleware.ts
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 token = await getToken({
req: request,
secret: process.env.JWT_SECRET
});
if (request.nextUrl.pathname.startsWith('/administration/login') && !!token) {
return NextResponse.redirect(
new URL('/administration/bo/airtable', request.url)
);
} else if (
request.nextUrl.pathname.startsWith('/administration/bo') &&
!token
) {
return NextResponse.redirect(new URL('/administration/login', request.url));
}
if (
request.nextUrl.pathname === '/administration' ||
request.nextUrl.pathname === '/administration/bo'
) {
return NextResponse.redirect(new URL('/administration/login', request.url));
}
}
// See "Matching Paths" below to learn more
export const config = {
matcher: '/administration/:path*'
};