-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
42 lines (40 loc) · 1.46 KB
/
Copy pathmiddleware.ts
File metadata and controls
42 lines (40 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
import { createServerClient } from "@supabase/auth-helpers-nextjs";
import { NextRequest, NextResponse } from "next/server";
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
// NextRequest exposes cookies via `req.cookies` helpers
// `getAll` returns an array of { name, value }
// Fallback to empty array if not available
// @ts-ignore
return req.cookies?.getAll ? req.cookies.getAll() : [];
},
setAll(
cookiesToSet: Array<{
name: string;
value: string;
options?: Record<string, any>;
}>,
) {
try {
cookiesToSet.forEach(({ name, value, options }) =>
res.cookies.set(name, value, options as any),
);
} catch {
// ignore
}
},
},
},
);
await supabase.auth.getSession(); // attaches session to req.cookies
return res;
}
export const config = {
matcher: ["/dashboard/:path*"],
};