-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
54 lines (47 loc) · 1.46 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
49
50
51
52
53
54
import { type NextRequest, NextResponse, userAgent } from 'next/server';
import { getToken } from 'next-auth/jwt';
import { ACCESS_TOKEN, APPLY_PERIOD } from '@/constants';
import { Schedule } from './utils/Schedule';
export async function middleware(request: NextRequest) {
const {
nextUrl: { pathname },
url,
} = request;
const accessToken = request.cookies.get(ACCESS_TOKEN);
if (pathname.includes('admin')) {
if (pathname === '/admin') {
if (accessToken) {
return NextResponse.redirect(new URL('/admin/application', url));
}
return NextResponse.next();
}
if (accessToken) {
return NextResponse.next();
}
return NextResponse.redirect(new URL('/admin', url));
}
const period = Schedule.getCurrentPeriod();
if (period !== APPLY_PERIOD.RECRUIT) {
return NextResponse.redirect(new URL('/', url));
}
const token = await getToken({ req: request, secret: process.env.NEXTAUTH_SECRET });
const {
device: { type = 'desktop' },
} = userAgent(request);
if (type !== 'desktop') {
return NextResponse.redirect(new URL('/', url));
}
if (pathname.includes('apply')) {
if (token?.accessToken) {
return NextResponse.next();
}
return NextResponse.redirect(new URL('/login', url));
}
if (token?.accessToken) {
return NextResponse.redirect(new URL('/apply', url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/admin/:path*', '/login', '/apply'],
};