Files
Inventory/frontend/app/middleware/profile.global.ts
Matthieu 5e7a744151
All checks were successful
Auto Tag Develop / tag (push) Successful in 8s
feat : add maintenance mode toggle in admin panel
- Backend: MaintenanceModeListener blocks non-admin API requests when
  var/maintenance flag file exists. MaintenanceController provides
  toggle (PUT /api/admin/maintenance) and public check endpoint
  (GET /api/maintenance/check).
- Frontend: Toggle button in admin page, maintenance.vue page for
  blocked users, middleware redirects non-admins to /maintenance.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 16:18:32 +02:00

43 lines
1.4 KiB
TypeScript

import { useProfileSession, usePermissions, useApi } 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");
const isMaintenanceRoute = normalizedPath === "/maintenance";
// Redirect to login if no active profile
if (!activeProfile.value && !isProfilesRoute && !isMaintenanceRoute) {
return navigateTo("/profiles");
}
// Permission checks
if (activeProfile.value) {
const { isAdmin } = usePermissions();
// Admin-only routes
if (normalizedPath.startsWith("/admin")) {
if (!isAdmin.value) {
return navigateTo("/");
}
}
// Maintenance mode check for non-admin users
if (!isAdmin.value && !isMaintenanceRoute && !isProfilesRoute) {
const { apiCall } = useApi();
const res = await apiCall<{ enabled: boolean }>('/maintenance/check');
if (res.success && res.data?.enabled) {
return navigateTo("/maintenance");
}
}
}
});