-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
43 lines (34 loc) · 1.41 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
import NextAuth from 'next-auth';
import { NextRequest, NextResponse } from 'next/server';
import createMiddleware from 'next-intl/middleware';
import { routing } from './i18n/routing';
import { authConfig } from './auth.config';
const intlMiddleware = createMiddleware(routing);
const auth = NextAuth(authConfig).auth;
const authMiddleware = auth((req) => {
const isLoggedIn = !!req.auth;
const isOnDashboard = req.nextUrl.pathname.includes('/dashboard');
const localeCookie = req.cookies.get('NEXT_LOCALE');
const locale = localeCookie?.value;
if (isOnDashboard && isLoggedIn) return intlMiddleware(req);
if (isLoggedIn && req.nextUrl.pathname === '/') {
// redirect to dashboard
return Response.redirect(new URL(`/${locale}/dashboard`, req.nextUrl.origin));
}
if (!isLoggedIn && !req.nextUrl.pathname.includes('login') && req.nextUrl.pathname !== '/' && req.nextUrl.pathname !== `/${locale}`) {
// redirect to login
return Response.redirect(new URL(`/${locale}/login`, req.nextUrl.origin));
}
if (!isLoggedIn && req.nextUrl.pathname === `/${locale}/`) {
// redirect to public index when not logged in
return;
}
return intlMiddleware(req);
});
export default function middleware(req: NextRequest) {
return (authMiddleware as any)(req);
}
export const config = {
// Skip all paths that should not be internationalized
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
};