-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.ts
76 lines (64 loc) · 1.98 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
66
67
68
69
70
71
72
73
74
75
76
import createIntlMiddleware from 'next-intl/middleware';
import { withAuth } from 'next-auth/middleware';
import { locales } from './navigation';
import { NextRequest, NextResponse } from 'next/server';
import { AUTH_PATH_REGEX, PUBLIC_PATH_REGEX } from './lib/regex';
const nextIntlMiddleware = createIntlMiddleware({
locales: ['en', 'vi'],
defaultLocale: 'en',
localeDetection: true,
});
const nextAuthMiddleware = () =>
withAuth(
(req) => {
const token = req.nextauth.token;
if (token) {
let locale = `${req.cookies.get('NEXT_LOCALE')?.value || 'en'}`;
const localeFromPath = req.nextUrl.pathname.split('/')[1];
if (
locale !== localeFromPath &&
locales.includes(localeFromPath as 'en' | 'vi')
) {
locale = localeFromPath;
}
if (!token.isOnboardingCompleted) {
return NextResponse.rewrite(
new URL(`/${locale}/auth/onboarding`, req.url),
);
} else {
if (AUTH_PATH_REGEX.test(req.nextUrl.pathname)) {
return NextResponse.redirect(new URL(`/${locale}/home`, req.url));
}
}
}
return nextIntlMiddleware(req);
},
{
callbacks: {
authorized: ({ token }) => token != null,
},
pages: {
signIn: '/auth/sign-in',
},
},
);
export default function middleware(req: NextRequest) {
const isPublic = PUBLIC_PATH_REGEX.test(req.nextUrl.pathname);
const isAuth = AUTH_PATH_REGEX.test(req.nextUrl.pathname);
if (isPublic) {
if (isAuth) {
if (req.cookies.get(process.env.NEXT_PUBLIC_AUTH_COOKIE_NAME as string)) {
return (nextAuthMiddleware() as any)(req);
}
// @ts-ignore
return nextIntlMiddleware(req);
}
return (nextAuthMiddleware() as any)(req);
}
return (nextAuthMiddleware() as any)(req);
}
export const config = {
matcher: [
'/((?!api|_next|_vercel|ref|.well-known|apple-app-site-association|.*\\..*).*)',
],
};