-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
34 lines (25 loc) · 989 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
import { hasLocale, isRootPath } from '@/app/utils/helpers';
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { getLocale } from '@/app/i18n/helpers';
import { locales } from '@/app/i18n/settings';
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (hasLocale(pathname, locales)) return;
const token = request.cookies.get('accessToken');
const locale = getLocale(request);
if (!token && !isRootPath(pathname)) {
return NextResponse.redirect(new URL('/', request.url));
}
if (isRootPath(pathname) && token) {
return NextResponse.redirect(new URL(`${locale}/dashboard/`, request.url));
}
if (pathname.startsWith('/dashboard')) {
request.nextUrl.pathname = `/${locale}${pathname}`;
return NextResponse.redirect(request.nextUrl);
}
return NextResponse.next();
}