-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
39 lines (32 loc) · 1.15 KB
/
Copy pathmiddleware.ts
File metadata and controls
39 lines (32 loc) · 1.15 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
import createIntlMiddleware from 'next-intl/middleware';
import { createServerClient } from '@supabase/ssr';
import type { NextRequest } from 'next/server';
import { routing } from './i18n/routing';
const handleI18nRouting = createIntlMiddleware(routing);
export async function middleware(request: NextRequest) {
// 1) Let next-intl produce the response (handles locale prefix routing)
const response = handleI18nRouting(request);
// 2) Refresh the Supabase session and copy any auth cookies onto the intl response
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) => {
response.cookies.set(name, value, options);
});
},
},
},
);
await supabase.auth.getUser();
return response;
}
export const config = {
// Match all non-asset, non-API paths so both locale routing and session refresh apply
matcher: ['/((?!api|_next|_vercel|.*\\..*).*)'],
};