From 30ef3f8eff112c0e794af3c62fb259edb9cecd91 Mon Sep 17 00:00:00 2001 From: Chad Date: Fri, 19 Apr 2024 18:53:51 -0700 Subject: [PATCH] updated middleware to look more like the astro docs --- src/middleware.ts | 9 +++++---- src/utils/utils.js | 6 ------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index e793937..119685b 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -3,6 +3,7 @@ import { lucia } from "./lib/auth"; import { verifyRequestOrigin as verifyOrig } from "lucia"; // import { defineMiddleware } from "astro:middleware"; + export const onRequest = async (context, next) => { const path = new URL(context.request.url).pathname; // Skip middleware for non-admin paths @@ -21,21 +22,21 @@ export const onRequest = async (context, next) => { const isDev = import.meta.env.APP_ENV === 'dev'; // In development, we might skip the origin check for ease of testing if (!isDev && (!originHeader || !hostHeader || !verifyOrig(originHeader, [hostHeader]))) { - // console.log('Origin verification failed'); + console.log('Origin verification failed'); return new Response("Forbidden", { status: 403 }); } // Validate session and user, then proceed const { session, user } = await lucia.validateSession(sessionId); - // console.log('Session and user validated:', session, user); + console.log('Session and user validated:', session, user); if (!['superadmin', 'admin','editor','writer'].includes(user.role)) { - // console.log('User role not allowed'); + console.log('User role not allowed'); return new Response(null, { status: 302, headers: { Location: '/login' } }); } context.locals.session = session; context.locals.user = user; return next(); } catch (error) { - // console.error('Session validation error:', error); + console.error('Session validation error:', error); // Clear session cookie on validation error and redirect to login const sessionCookie = lucia.createBlankSessionCookie(); context.cookies.set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes); diff --git a/src/utils/utils.js b/src/utils/utils.js index a3443cc..7c5a935 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -488,8 +488,6 @@ export const uploadS3 = async (base64Data, Key, ContentType='', Bucket='') => { }; export const seedSuperUser = async () => { - console.log('Seeding super user'); - const email = import.meta.env.SITE_ADMIN_EMAIL.trim().toLowerCase(); const userFound = (await db.select().from(Users).where(eq(Users.email, email))).length; const name = site.author; @@ -527,10 +525,6 @@ export const seedSuperUser = async () => { await db.insert(Team).values(teamMember); } catch (e) { console.error('seedSuperUser team:', e); } } - - - - }