-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
48 lines (43 loc) · 1.07 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { NextRequest, NextFetchEvent } from 'next/server';
import { NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
const secret = process.env.FT_SECRET;
export async function middleware(req: NextRequest, event: NextFetchEvent) {
const session = await getToken({ req, secret, raw: true });
const { pathname } = req.nextUrl;
if (pathname === '/login') {
if (session) {
return NextResponse.redirect(new URL('/', req.url));
}
} else {
if (!session) {
return NextResponse.redirect(new URL('/login', req.url));
}
}
if (
pathname === '/chat' ||
pathname === '/profile' ||
pathname === '/ranking' ||
pathname === '/join' ||
pathname === '/game' ||
pathname === '/generalLobby' ||
pathname === '/rankLobby'
) {
if (!session) {
return NextResponse.redirect(new URL('/login', req.url));
}
}
}
export const config = {
matcher: [
'/',
'/login',
'/chat',
'/profile/(.*)',
'/ranking',
'/join',
'/game',
'/generalLobby',
'/rankLobby',
],
};