-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
32 lines (24 loc) · 1014 Bytes
/
middleware.ts
File metadata and controls
32 lines (24 loc) · 1014 Bytes
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
import { CookieMethodsServer, createServerClient } from "@supabase/ssr";
import { MiddlewareConfig, NextRequest, NextResponse } from "next/server";
export async function middleware(request: NextRequest) {
return await refreshSupabaseAuth(request);
}
async function refreshSupabaseAuth(request: NextRequest) {
let response = NextResponse.next({ request });
const cookies: CookieMethodsServer = {
getAll: () => request.cookies.getAll(),
setAll: cookies => {
cookies.forEach(c => request.cookies.set(c));
response = NextResponse.next({ request });
cookies.forEach(c => response.cookies.set(c));
},
};
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
const supabase = createServerClient(url, key, { cookies });
await supabase.auth.getUser();
return response;
}
export const config: MiddlewareConfig = {
matcher: ["/((?!_next/static|_next/image|favicon\\.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)"],
};