fix: isProtectedPath nel middleware, landingFor per cliente e trainer
This commit is contained in:
@@ -124,5 +124,18 @@ export function isRole(v: unknown): v is Role {
|
||||
export function landingFor(role: string): string {
|
||||
if (role === 'superuser') return '/admin/content';
|
||||
if (role === 'piattaforme') return '/piattaforme';
|
||||
if (role === 'cliente') return '/longevity/io';
|
||||
if (role === 'trainer') return '/longevity/gestionale';
|
||||
return '/admin';
|
||||
}
|
||||
|
||||
export function isProtectedPath(pathname: string): boolean {
|
||||
return (
|
||||
(pathname.startsWith('/admin') && pathname !== '/admin/login') ||
|
||||
pathname.startsWith('/api/admin') ||
|
||||
pathname.startsWith('/campus') ||
|
||||
pathname.startsWith('/piattaforme') ||
|
||||
pathname.startsWith('/longevity') ||
|
||||
pathname.startsWith('/api/longevity')
|
||||
);
|
||||
}
|
||||
|
||||
+2
-7
@@ -1,15 +1,10 @@
|
||||
import { defineMiddleware } from 'astro:middleware';
|
||||
import { getDb } from './lib/db';
|
||||
import { getSessionUser, SESSION_COOKIE, canAccessAdminPath, landingFor } from './lib/auth';
|
||||
import { getSessionUser, SESSION_COOKIE, canAccessAdminPath, landingFor, isProtectedPath } from './lib/auth';
|
||||
|
||||
export const onRequest = defineMiddleware((context, next) => {
|
||||
const { pathname } = context.url;
|
||||
const isProtected =
|
||||
(pathname.startsWith('/admin') && pathname !== '/admin/login') ||
|
||||
pathname.startsWith('/api/admin') ||
|
||||
pathname.startsWith('/campus') ||
|
||||
pathname.startsWith('/piattaforme');
|
||||
if (!isProtected) return next();
|
||||
if (!isProtectedPath(pathname)) return next();
|
||||
|
||||
const token = context.cookies.get(SESSION_COOKIE)?.value;
|
||||
const user = token ? getSessionUser(getDb(), token) : null;
|
||||
|
||||
Reference in New Issue
Block a user