-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
47 lines (41 loc) · 1.46 KB
/
Copy pathmiddleware.ts
File metadata and controls
47 lines (41 loc) · 1.46 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
import { NextResponse, type NextRequest } from "next/server";
import { createServerClient } from "@supabase/ssr";
const url = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!;
/**
* Refreshes the Supabase auth session on every request so tokens stay valid
* and the refreshed cookies reach both the browser and any server client.
* Standard @supabase/ssr pattern — see supabase.com/docs/guides/auth/server-side/nextjs.
*/
export async function middleware(request: NextRequest) {
let response = NextResponse.next({ request });
const supabase = createServerClient(url, anonKey, {
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value }) =>
request.cookies.set(name, value)
);
response = NextResponse.next({ request });
cookiesToSet.forEach(({ name, value, options }) =>
response.cookies.set(name, value, options)
);
},
},
});
// Touch the session so expired access tokens get refreshed.
await supabase.auth.getUser();
return response;
}
export const config = {
matcher: [
/*
* Match all request paths except static assets:
* _next/static, _next/image, favicon.ico, the web manifest, and common
* image files.
*/
"/((?!_next/static|_next/image|favicon.ico|manifest.webmanifest|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico)$).*)",
],
};