forked from Lafiya-xyz/Lafiya-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
82 lines (71 loc) · 2.95 KB
/
Copy pathproxy.ts
File metadata and controls
82 lines (71 loc) · 2.95 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { createServerClient } from "@supabase/ssr";
import { type NextRequest, NextResponse } from "next/server";
import { clientEnv } from "@/lib/env";
const PROTECTED_PREFIXES = ["/profile"];
const AUTH_ONLY_PATHS = ["/signin", "/signup"];
export async function proxy(request: NextRequest) {
let response = NextResponse.next({ request });
const supabase = createServerClient(
clientEnv.NEXT_PUBLIC_SUPABASE_URL,
clientEnv.NEXT_PUBLIC_SUPABASE_ANON_KEY,
{
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),
);
},
},
},
);
// getUser() (not getSession()) revalidates the token against the auth
// server on every request rather than trusting a potentially-stale JWT —
// required reading before touching this: https://supabase.com/docs/guides/auth/server-side/nextjs
const {
data: { user },
} = await supabase.auth.getUser();
const { pathname } = request.nextUrl;
const isPublicCard = pathname === "/card" || pathname.startsWith("/card/");
if (isPublicCard) {
// The URL is a bearer capability (legacy UUID or current capability). It
// must never be sent as a referrer, placed in a shared CDN cache, indexed,
// or allowed to trigger third-party network requests from the card page.
response.headers.set("Referrer-Policy", "no-referrer");
response.headers.set("X-Robots-Tag", "noindex, nofollow, noarchive");
response.headers.set("Cache-Control", "private, no-store, max-age=0");
response.headers.set(
"Content-Security-Policy",
"default-src 'self'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'; object-src 'none'; connect-src 'self'; img-src 'self' data: blob: https://*.supabase.co http://127.0.0.1:54321; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'",
);
}
const isProtected = PROTECTED_PREFIXES.some(
(prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`),
);
const isAuthOnly = AUTH_ONLY_PATHS.includes(pathname);
if (!user && isProtected) {
const signInUrl = new URL("/signin", request.url);
signInUrl.searchParams.set("next", pathname);
return NextResponse.redirect(signInUrl);
}
if (user && isAuthOnly) {
return NextResponse.redirect(new URL("/profile", request.url));
}
return response;
}
export const config = {
matcher: [
/*
* Match all request paths except for static assets and image
* optimization files, so the session cookie still gets refreshed on
* every navigable page without doing this work for every asset.
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};