- Add usePermissions composable (isAdmin, canEdit, canView) - Password-protected profile login with modal on profiles page - Disable all form fields for ROLE_VIEWER across edit/create pages - Show navigation buttons (Modifier/Consulter) for all roles, hide delete for viewers - Add readonly prop to ModelTypeForm for category pages - Disable modal fields (sites, constructeurs) for viewers - Guard /admin routes in middleware Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
34 lines
972 B
TypeScript
34 lines
972 B
TypeScript
import { useProfileSession, usePermissions } from "#imports";
|
|
|
|
export default defineNuxtRouteMiddleware(async (to) => {
|
|
const { ensureSession, activeProfile } = useProfileSession();
|
|
await ensureSession();
|
|
|
|
const rawPath = to?.path ?? "";
|
|
const normalizedPath = rawPath.startsWith("/") ? rawPath : `/${rawPath}`;
|
|
const fullPath = to?.fullPath ?? normalizedPath;
|
|
const routeName = typeof to?.name === "string" ? to.name : "";
|
|
const isProfilesRoute =
|
|
normalizedPath.startsWith("/profiles") ||
|
|
fullPath.startsWith("/profiles") ||
|
|
routeName.startsWith("profiles");
|
|
|
|
// Redirect to login if no active profile
|
|
if (!activeProfile.value && !isProfilesRoute) {
|
|
return navigateTo("/profiles");
|
|
}
|
|
|
|
// Permission checks
|
|
if (activeProfile.value) {
|
|
const { isAdmin } = usePermissions();
|
|
|
|
// Admin-only routes
|
|
if (normalizedPath.startsWith("/admin")) {
|
|
if (!isAdmin.value) {
|
|
return navigateTo("/");
|
|
}
|
|
}
|
|
|
|
}
|
|
});
|