-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
64 lines (52 loc) · 2.23 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
/* eslint-disable @typescript-eslint/no-unused-vars */
import { match } from '@formatjs/intl-localematcher';
import Negotiator from 'negotiator';
import NextAuth from 'next-auth';
import { NextRequest, NextResponse } from 'next/server';
import { authConfig } from './auth.config';
const protectedRoutes = ['/success', '/payment', '/bookings'];
const defaultLocale = 'en';
const locales = ['bn', 'en'];
const ROOT = '/';
const { auth } = NextAuth(authConfig);
// Function to get the locale from the request's Accept-Language header
function getLocale(request: NextRequest): string {
const acceptedLanguage = request.headers.get('accept-language') ?? undefined;
const headers = { 'accept-language': acceptedLanguage };
const languages = new Negotiator({ headers }).languages();
return match(languages, locales, defaultLocale);
}
export default auth(async function middleware(request: NextRequest): Promise<NextResponse | void> {
const { nextUrl } = request;
const pathname = nextUrl.pathname;
const isProtectedRoute = protectedRoutes.includes(pathname);
const isAuthenticated = !!request.auth;
// Skip API routes explicitly
if (pathname.startsWith('/api')) {
return;
}
// Check for protected routes and handle redirection for unauthenticated users
if (isProtectedRoute && !isAuthenticated) {
return NextResponse.redirect(new URL('/login', request.nextUrl));
}
// Check if pathname contains a locale
const pathnameHasLocale = locales.some((locale) => pathname.startsWith(`/${locale}`));
// If no locale is present, redirect to the default or detected locale
if (!pathnameHasLocale) {
const locale = getLocale(request);
return NextResponse.redirect(new URL(`/${locale}${pathname}`, nextUrl.origin));
}
// Ensure the `page` query parameter is set to 1 if missing
const urlSearchParams = new URLSearchParams(nextUrl.search);
if (!urlSearchParams.has('page')) {
urlSearchParams.set('page', '1');
return NextResponse.redirect(new URL(`${pathname}?${urlSearchParams.toString()}`, nextUrl.origin));
}
// No redirect necessary, just continue the request
});
export const config = {
matcher: [
'/((?!api|assets|_next|.*\\..*).*)', // Exclude API and static assets
'/', // Include the root URL
],
};