| 1234567891011121314151617181920 |
- // Authentication middleware to protect routes
- export default defineNuxtRouteMiddleware((to, _from) => {
- const { isAuthenticated, isInitialized } = useAuth()
- // Check if route requires authentication
- // Routes with auth: false in their page meta are public
- if (to.meta.auth === false) {
- return
- }
- // If user is not authenticated, redirect to login
- if (isInitialized.value && !isAuthenticated.value) {
- const redirect = to.fullPath
- return navigateTo({
- path: '/login',
- query: redirect && redirect !== '/login' ? { redirect } : undefined,
- })
- }
- })
|