-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproxy.ts
More file actions
34 lines (33 loc) · 1.27 KB
/
proxy.ts
File metadata and controls
34 lines (33 loc) · 1.27 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
import { NextResponse, type NextRequest } from "next/server";
import { createSupabaseServerClient } from "./lib/supabase/server-client";
/**
* Next.js proxy (formerly middleware) entry point responsible for basic auth gating.
*
* Runtime assumptions due to conflicting docs (Next.js 16):
* - Proxy runs in the Node.js runtime by default (not Edge)
* - Node runtime grants access to the shared cookie store used by Supabase
*
* What happens per request:
* - Instantiate the Supabase server client (shares cookies via `NextResponse`)
* - Call `supabase.auth.getUser()` which refreshes tokens if necessary
* - Redirect anonymous users away from `/protected` routes to `/login`
*
* Add extra path checks or redirects here when you need more complex routing rules.
*/
export async function proxy(request: NextRequest) {
const response = NextResponse.next({
request: {
headers: request.headers,
},
});
const supabase = await createSupabaseServerClient();
const {
data: { user },
} = await supabase.auth.getUser();
console.log ({ user });
// Redirect non-authenticated users away from protected routes
if (!user && request.nextUrl.pathname.startsWith("/protected")) {
return NextResponse.redirect(new URL("/login", request.url));
}
return response;
}