1- import { dev } from '$app/environment' ;
21import { createAuth } from '$lib/auth/lucia' ;
32import { FeideProvider } from '$lib/auth/feide' ;
43import { createDatabase } from '$lib/db/drizzle' ;
@@ -24,17 +23,17 @@ import { PendingApplicationService } from '$lib/services/pending-application.ser
2423import { csrf } from '$lib/hooks/csrf' ;
2524
2625const main : Handle = async ( { event, resolve } ) => {
26+ // Set up primitive services from Cloudflare environment
2727 const STATUS_KV = event . platform ! . env . STATUS_KV ;
2828 const R2_BUCKET = event . platform ! . env . BUCKET ;
2929 const DB = event . platform ! . env . DB ;
3030
3131 const banService = new BanService ( STATUS_KV ) ;
3232 event . locals . banService = banService ;
3333
34- const ip = event . getClientAddress ( ) ;
35- const isIpBanned = await banService . isIpBanned ( ip ) ;
36-
37- if ( isIpBanned ) {
34+ // Check if IP is banned
35+ const isBanned = await banService . isBanned ( event ) ;
36+ if ( isBanned ) {
3837 return new Response ( null , {
3938 status : 429 ,
4039 headers : {
@@ -63,7 +62,8 @@ const main: Handle = async ({ event, resolve }) => {
6362 ) ;
6463 event . locals . feideProvider = feideProvider ;
6564
66- // Setup status service
65+ // Setup services
66+
6767 const statusService = new StatusService ( event . platform ! . env . STATUS_KV ) ;
6868 event . locals . statusService = statusService ;
6969
@@ -122,14 +122,14 @@ const main: Handle = async ({ event, resolve }) => {
122122 event . locals . session = null ;
123123 }
124124
125+ // Clear cookie if no valid user
125126 if ( ! event . locals . user ) {
126127 event . cookies . delete ( auth . sessionCookieName , {
127- path : '/' ,
128- httpOnly : true ,
129- secure : ! dev
128+ path : '/'
130129 } ) ;
131130 }
132131
132+ // Only board members have access to routes under /portal/admin
133133 if ( event . url . pathname . startsWith ( '/portal/admin' ) && event . locals . user ?. role !== 'board' ) {
134134 return new Response ( null , {
135135 status : 307 ,
@@ -139,6 +139,7 @@ const main: Handle = async ({ event, resolve }) => {
139139 } ) ;
140140 }
141141
142+ // Only authenticated users have access to routes under /portal
142143 if ( event . url . pathname . startsWith ( '/portal' ) && ! event . locals . user ) {
143144 return new Response ( null , {
144145 status : 307 ,
0 commit comments