forked from summit-webapp/summit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
39 lines (39 loc) · 1.24 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
import { NextResponse } from 'next/server';
import type { NextFetchEvent } from 'next/server';
import type { NextRequest } from 'next/server';
import axios from 'axios';
import { CONSTANTS } from './services/config/app-config';
export async function middleware(request: NextRequest, event: NextFetchEvent) {
try {
if (CONSTANTS.ENABLE_REDIRECT_FEATURE) {
const redirect = await fetch(
`${CONSTANTS.API_BASE_URL}/${CONSTANTS.API_MANDATE_PARAMS}&method=get_redirecting_urls&entity=signin`,
{
method: 'GET',
}
).then((res: any) => res.json());
const url = request.nextUrl.clone();
if (url.pathname.startsWith('/_next')) {
return NextResponse.next();
}
const matchingRedirect = redirect.message.find(
(value: any) => value.from === url.pathname
);
if (matchingRedirect) {
url.pathname = matchingRedirect.to;
url.search = '';
return Response.redirect(url, 308);
} else {
return NextResponse.next();
}
} else {
console.log('Redirect feature is disabled.');
}
} catch (err) {
console.error('Error fetching redirect data:', err);
return NextResponse.next();
}
}
export const config = {
matcher: '',
};