c4eb598a3a
Il ruolo `campus` diventa `piattaforme` e non apre più solo la sezione Biohacking: copre tutte le aree riservate, con migrazione guardata sugli utenti esistenti. Nell'header la vecchia voce Biohacking lascia il posto a un menu Piattaforme con i due link, visibile solo a chi ha il ruolo, e /piattaforme diventa la pagina di atterraggio. Stress Index è portata dentro il sito: le nove viste dell'area professionisti diventano pagine Astro sotto /piattaforme/stress-index, con lo stile del sito al posto del design system del prototipo. React resta solo dove serve davvero — grafici recharts e scheda cliente — il resto è Astro con un filo di JS. I dati sono ancora quelli dimostrativi, raccolti in lib/stress-index/data.ts. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
import { defineMiddleware } from 'astro:middleware';
|
|
import { getDb } from './lib/db';
|
|
import { getSessionUser, SESSION_COOKIE, canAccessAdminPath, landingFor } 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();
|
|
|
|
const token = context.cookies.get(SESSION_COOKIE)?.value;
|
|
const user = token ? getSessionUser(getDb(), token) : null;
|
|
if (!user) {
|
|
if (pathname.startsWith('/api/')) {
|
|
return new Response(JSON.stringify({ error: 'Non autorizzato' }), {
|
|
status: 401, headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
// Le piattaforme riservate si raggiungono dal sito, quindi passano dal login
|
|
// pubblico e ci tornano dopo l'accesso; il pannello ha il proprio.
|
|
if (pathname.startsWith('/campus') || pathname.startsWith('/piattaforme'))
|
|
return context.redirect(`/login?next=${encodeURIComponent(pathname)}`);
|
|
return context.redirect('/admin/login');
|
|
}
|
|
|
|
if (!canAccessAdminPath(user.role, pathname)) {
|
|
if (pathname.startsWith('/api/')) {
|
|
return new Response(JSON.stringify({ error: 'Permessi insufficienti' }), {
|
|
status: 403, headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
return context.redirect(landingFor(user.role));
|
|
}
|
|
|
|
context.locals.user = user;
|
|
return next();
|
|
});
|