1- import { building } from "$app/environment" ;
2- import { env as envPublic } from "$env/dynamic/public" ;
3- import { auth } from "$lib/server/auth" ;
4- import { UserRole } from "$lib/shared/roles" ;
5- import { runMigrations } from "$src/lib/server/db/migrate" ;
61import { handleErrorWithSentry , sentryHandle } from "@sentry/sveltekit" ;
7- import { redirect , type Handle , type ServerInit } from "@sveltejs/kit" ;
2+ import { type Handle } from "@sveltejs/kit" ;
83import { sequence } from "@sveltejs/kit/hooks" ;
9- import { svelteKitHandler } from "better-auth/svelte-kit" ;
104
11- const protectedRouteGroupName = "(protected)" ;
12- const protectedAdminRouteGroupName = "(admin)" ;
13- const signInPath = "/login" ;
14-
15- export const init : ServerInit = async ( ) => {
16- if ( building ) {
17- console . info ( "Skipping migrations during build." ) ;
18- return ;
19- }
20- await runMigrations ( ) ;
21- } ;
5+ // This fork is a public stats/card embed (skycrypt-embed.lunarclient.com) deployed
6+ // on Cloudflare Workers. It has no user accounts, so the upstream better-auth +
7+ // Postgres pipeline is intentionally omitted: DB-backed session lookups need a
8+ // database the embed doesn't have, and migrate-on-startup can't run on Workers
9+ // (no filesystem / process.cwd). See src/lib/server/db/index.ts (lazy db).
2210
2311const headersHandler = ( async ( { event, resolve } ) => {
2412 const response = await resolve ( event ) ;
@@ -29,18 +17,8 @@ const headersHandler = (async ({ event, resolve }) => {
2917 response . headers . set ( "Permissions-Policy" , "accelerometer=(), autoplay=(), camera=(), encrypted-media=(), fullscreen=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(), publickey-credentials-get=(), sync-xhr=(), usb=(), xr-spatial-tracking=(), geolocation=()" ) ;
3018 response . headers . set ( "X-Content-Type-Options" , "nosniff" ) ;
3119 response . headers . set ( "Strict-Transport-Security" , "max-age=31536000; includeSubDomains; preload" ) ;
32-
33- // Clickjacking protection. The newsroom draft preview must be embeddable in the Payload
34- // CMS admin's live-preview iframe, so allow only the CMS origin to frame that one route;
35- // everything else stays frame-locked.
36- const isNewsroomPreview = url . pathname . startsWith ( "/newsroom/" ) && url . searchParams . get ( "preview" ) === "1" ;
37- if ( isNewsroomPreview ) {
38- const cms = envPublic . PUBLIC_CMS_URL ?. trim ( ) ;
39- response . headers . append ( "Content-Security-Policy" , `frame-ancestors 'self'${ cms ? ` ${ cms } ` : "" } ` ) ;
40- } else {
41- response . headers . set ( "X-Frame-Options" , "DENY" ) ;
42- response . headers . append ( "Content-Security-Policy" , "frame-ancestors 'none'" ) ;
43- }
20+ // No frame-locking: this fork is embedded as an iframe (skycrypt-embed.lunarclient.com),
21+ // so X-Frame-Options / frame-ancestors restrictions are intentionally omitted.
4422
4523 // Cross-Origin policies
4624 // COEP intentionally unsafe-none: tightening would require all cross-origin
@@ -66,40 +44,4 @@ const headersHandler = (async ({ event, resolve }) => {
6644// If you have a custom error handler, pass it to `handleErrorWithSentry`
6745export const handleError = handleErrorWithSentry ( ) ;
6846
69- const betterAuthHandler : Handle = async ( { event, resolve } ) => {
70- const session = await auth . api . getSession ( { headers : event . request . headers } ) ;
71-
72- if ( session ) {
73- event . locals . session = session . session ;
74- event . locals . user = session . user ;
75- }
76-
77- return svelteKitHandler ( { event, resolve, auth, building } ) ;
78- } ;
79-
80- const protectedHandler = ( async ( { event, resolve } ) => {
81- const { locals, route } = event ;
82- if ( ! locals . user ) {
83- if ( route . id ?. includes ( protectedRouteGroupName ) ) {
84- console . info ( "Redirecting to sign-in page as user is not authenticated." ) ;
85- redirect ( 307 , signInPath ) ;
86- }
87- }
88- if ( locals . user ) {
89- const roles = ( locals . user . role ? locals . user . role . split ( "," ) : [ ] ) as UserRole [ ] ;
90- const isAdmin = roles . includes ( UserRole . Admin ) ;
91- if ( route . id ?. includes ( protectedAdminRouteGroupName ) && ! isAdmin ) {
92- console . info ( "Redirecting to dashboard as user lacks admin role." ) ;
93- redirect ( 307 , "/dashboard" ) ;
94- }
95- }
96- if ( locals . user && locals . session ) {
97- if ( route . id ?. startsWith ( signInPath ) ) {
98- redirect ( 307 , "/dashboard" ) ;
99- }
100- }
101- return resolve ( event ) ;
102- } ) satisfies Handle ;
103-
104- // If you have custom handlers, make sure to place them after `sentryHandle()` in the `sequence` function.
105- export const handle = sequence ( sentryHandle ( ) , betterAuthHandler , protectedHandler , headersHandler ) satisfies Handle ;
47+ export const handle = sequence ( sentryHandle ( ) , headersHandler ) satisfies Handle ;
0 commit comments