Spaces:
Sleeping
Sleeping
Download src/lib/supabase/proxy.ts from ashnhash/UniHunt: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/spaces/ashnhash/UniHunt/resolve/main/src/lib/supabase/proxy.ts
- Command line
-
hf download hf://spaces/ashnhash/UniHunt/src/lib/supabase/proxy.ts
-
curl -L -o proxy.ts https://huggingface.co/spaces/ashnhash/UniHunt/resolve/main/src/lib/supabase/proxy.ts
1.65 kB
| import { createServerClient } from "@supabase/ssr"; | |
| import { NextResponse, type NextRequest } from "next/server"; | |
| import type { Database } from "./database.types"; | |
| export async function updateSession(request: NextRequest) { | |
| let supabaseResponse = NextResponse.next({ request }); | |
| const supabase = createServerClient<Database>( | |
| 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 }) => | |
| request.cookies.set(name, value) | |
| ); | |
| supabaseResponse = NextResponse.next({ request }); | |
| cookiesToSet.forEach(({ name, value, options }) => | |
| supabaseResponse.cookies.set(name, value, options) | |
| ); | |
| }, | |
| }, | |
| } | |
| ); | |
| const { | |
| data: { user }, | |
| } = await supabase.auth.getUser(); | |
| const isAuthRoute = | |
| request.nextUrl.pathname.startsWith("/login") || | |
| request.nextUrl.pathname.startsWith("/auth"); | |
| const isProtectedRoute = | |
| request.nextUrl.pathname.startsWith("/search") || | |
| request.nextUrl.pathname.startsWith("/shortlist"); | |
| if (!user && isProtectedRoute) { | |
| const url = request.nextUrl.clone(); | |
| url.pathname = "/login"; | |
| url.searchParams.set("redirect", request.nextUrl.pathname); | |
| return NextResponse.redirect(url); | |
| } | |
| if (user && isAuthRoute && request.nextUrl.pathname === "/login") { | |
| const url = request.nextUrl.clone(); | |
| url.pathname = "/search"; | |
| return NextResponse.redirect(url); | |
| } | |
| return supabaseResponse; | |
| } | |