-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
82 lines (63 loc) · 2.38 KB
/
middleware.ts
File metadata and controls
82 lines (63 loc) · 2.38 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { NextRequest, NextResponse } from 'next/server';
import { LOCALES, DEFAULT_LOCALE as DEFAULT } from "@/lib/utils/translation";
const LOCALE_COOKIE = 'NEXT_LOCALE';
const TOKEN_COOKIE = 'refreshToken';
const COOKIE_OPTS = {
path: '/',
maxAge: 60 * 60 * 24 * 365,
sameSite: 'lax' as const,
secure: process.env.NODE_ENV === 'production',
};
function splitPath(pathname: string) {
const segs = pathname.split('/').filter(Boolean);
const first = segs[0];
const hasLocale = (LOCALES as readonly string[]).includes(first as any);
const locale = hasLocale ? (first as typeof LOCALES[number]) : null;
const rest = '/' + (hasLocale ? segs.slice(1) : segs).join('/');
return { hasLocale, locale, rest: rest === '/' ? '/' : rest };
}
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
// Skip assets/API/files
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/api') ||
/\.[a-zA-Z0-9]+$/.test(pathname)
) {
return NextResponse.next();
}
const token = req.cookies.get(TOKEN_COOKIE)?.value || null;
const { hasLocale, locale: urlLocale, rest } = splitPath(pathname);
const fromCookie = req.cookies.get(LOCALE_COOKIE)?.value;
const effectiveLocale =
(urlLocale ??
(fromCookie && (LOCALES as readonly string[]).includes(fromCookie as any) ? (fromCookie as any) : null)) || DEFAULT;
const redirect = (toPath: string) => {
const url = req.nextUrl.clone();
url.pathname = toPath;
const res = NextResponse.redirect(url);
res.cookies.set(LOCALE_COOKIE, effectiveLocale, COOKIE_OPTS);
return res;
};
if (!hasLocale) {
if (pathname === '/' || pathname === '') {
return redirect(`/${effectiveLocale}${token ? '/dashboard' : '/signin'}`);
}
return redirect(`/${effectiveLocale}${pathname}`);
}
if (rest === '/' || rest === '') {
return redirect(`/${effectiveLocale}${token ? '/dashboard' : '/signin'}`);
}
const isAuthPage = rest === '/signin' || rest === '/signup';
const isPrivateArea = rest.startsWith('/dashboard');
if (token && isAuthPage) {
return redirect(`/${effectiveLocale}/dashboard`);
}
if (!token && isPrivateArea) {
return redirect(`/${effectiveLocale}/signin`);
}
const res = NextResponse.next();
res.cookies.set(LOCALE_COOKIE, effectiveLocale, COOKIE_OPTS);
return res;
}
export const config = { matcher: ['/((?!_next|api|.*\\..*).*)'] };