-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
65 lines (57 loc) · 1.74 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
55
56
57
58
59
60
61
62
63
64
65
import { jwtDecode } from 'jwt-decode';
import createMiddleware from 'next-intl/middleware';
import { cookies } from 'next/headers';
import { NextRequest } from 'next/server';
import {
COOKIE_KEYS,
privateRoutes,
protectedRoutes,
} from './app/lib/constants';
import { AccessToken } from './app/lib/utils/validation/assert/jwt';
const i18nMiddleware = createMiddleware({
locales: ['en', 'ko'],
defaultLocale: 'ko',
localeDetection: true,
});
const middleware = async (req: NextRequest) => {
const { pathname } = req.nextUrl.clone();
const isProtectedRoute = protectedRoutes.some((route) =>
pathname.includes(route),
);
const isPrivateRoute = privateRoutes.some((route) =>
pathname.includes(route),
);
const isMaintenance = false;
if (
isMaintenance &&
!pathname.includes('under-construction') &&
!pathname.includes('jeomshim')
) {
req.nextUrl.pathname = '/under-construction';
}
if (isProtectedRoute) {
const jwt = cookies().get(COOKIE_KEYS.accessToken)?.value;
if (jwt === undefined || jwt.length === 0) {
req.nextUrl.pathname = '/login';
return i18nMiddleware(req);
}
const { userRole } = jwtDecode<AccessToken>(jwt);
const userRoles = userRole.split(',');
const isAdmin = userRoles.includes('ROLE_ADMIN');
if (!isAdmin) {
req.nextUrl.pathname = '/';
return i18nMiddleware(req);
}
} else if (isPrivateRoute) {
const jwt = cookies().get(COOKIE_KEYS.accessToken)?.value;
if (!jwt) {
req.nextUrl.pathname = `/login?redirect=${encodeURIComponent(pathname.replace(/\/(ko|en)/, ''))}`;
return i18nMiddleware(req);
}
}
return i18nMiddleware(req);
};
export const config = {
matcher: ['/', '/(ko|en)/:path*'],
};
export default middleware;