Files
seguidorDeLotes/nuxt4-app/app/middleware/auth.ts
josedario87 7612487d3f
All checks were successful
build-and-deploy / build (push) Successful in 2m7s
build-and-deploy / deploy (push) Successful in 3s
Add Nuxt 4 app with OAuth/OIDC authentication and PWA support
- Integrated Authentik OAuth/OIDC authentication
- Added PWA functionality with offline support
- Created protected and public API endpoints
- Configured Docker deployment with Traefik
- Added Gitea Actions CI/CD workflow
- Included comprehensive setup documentation
2025-10-11 17:12:05 -06:00

26 lines
866 B
TypeScript

/**
* Auth Middleware
*
* Protege rutas que requieren autenticación
* Uso: Añade definePageMeta({ middleware: 'auth' }) en las páginas que necesites proteger
*/
export default defineNuxtRouteMiddleware((to, from) => {
const { loggedIn } = useUserSession()
// Si el usuario no está autenticado y no está en modo offline
if (!loggedIn.value) {
// Verificar si hay conexión
if (import.meta.client && !navigator.onLine) {
// En modo offline, permitir navegación si hay datos en caché
console.log('Modo offline: permitiendo navegación con datos en caché')
return
}
// Guardar la ruta a la que quería acceder para redirigir después del login
const redirectPath = to.fullPath
// Redirigir al login con la ruta de destino
return navigateTo(`/login?redirect=${encodeURIComponent(redirectPath)}`)
}
})