Skip to content

Commit

Permalink
updated middleware to look more like the astro docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chadananda committed Apr 20, 2024
1 parent c0586d1 commit 30ef3f8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -527,10 +525,6 @@ export const seedSuperUser = async () => {
await db.insert(Team).values(teamMember); }
catch (e) { console.error('seedSuperUser team:', e); }
}




}


0 comments on commit 30ef3f8

Please sign in to comment.