-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
86 lines (72 loc) · 2.35 KB
/
Copy pathmiddleware.ts
File metadata and controls
86 lines (72 loc) · 2.35 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
83
84
85
86
import { type NextRequest, NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
import createIntlMiddleware from 'next-intl/middleware';
import { isDevelopmentEnvironment } from '@/src/lib/constants';
const intlMiddleware = createIntlMiddleware({
locales: ['es', 'en'],
defaultLocale: 'es',
localePrefix: 'as-needed',
});
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Pass pathname to the layout via headers
const response = NextResponse.next();
response.headers.set('x-pathname', pathname);
/*
* Playwright starts the dev server and requires a 200 status to
* begin the tests, so this ensures that the tests can start
*/
if (pathname.startsWith('/ping')) {
return new Response('pong', { status: 200 });
}
if (pathname.startsWith('/api/auth')) {
return response;
}
// Handle i18n only for locale-prefixed routes
if (pathname.startsWith('/en/') || pathname.startsWith('/es/')) {
const intlResponse = intlMiddleware(request);
if (intlResponse) {
intlResponse.headers.set('x-pathname', pathname);
return intlResponse;
}
}
const token = await getToken({
req: request,
secret: process.env.AUTH_SECRET,
secureCookie: !isDevelopmentEnvironment,
});
// Handle login/register redirects first
if (['/login', '/register'].includes(pathname)) {
if (token) {
const redirectUrl = request.nextUrl.searchParams.get('redirectUrl');
return NextResponse.redirect(
new URL(redirectUrl || '/admin', request.url),
);
}
return response;
}
// Check if the route requires authentication (only admin routes)
const requiresAuth = pathname.includes('admin');
// Allow all non-admin routes to pass through
if (!requiresAuth) {
return response;
}
if (!token) {
const redirectUrl = encodeURIComponent(request.url);
return NextResponse.redirect(
new URL(`/login?redirectUrl=${redirectUrl}`, request.url),
);
}
return response;
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
*/
'/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
],
};