feat(frontend): Get project running again after move to Mac Mini

This commit is contained in:
2025-05-31 10:14:36 +02:00
parent d553668893
commit 075847f0ee
10 changed files with 86 additions and 28 deletions

View File

@@ -1,6 +1,7 @@
// Copied from https://github.com/atinux/nuxthub-better-auth
import { defu } from 'defu'
import type { RouteLocationNormalized } from '#vue-router'
type MiddlewareOptions =
| false
@@ -31,33 +32,44 @@ declare module 'vue-router' {
}
}
export default defineNuxtRouteMiddleware(async (to) => {
// If auth is disabled, skip middleware
export default defineNuxtRouteMiddleware(async (to: RouteLocationNormalized) => {
// 1. If auth is disabled, skip middleware
if (to.meta?.auth === false) {
console.log('[1] Auth middleware disabled for this route:', to.path)
return
}
const { loggedIn, options, fetchSession } = useAuth()
const { loggedIn, options, fetchSession, isPublicRoute } = useAuth()
const { only, redirectUserTo, redirectGuestTo } = defu(to.meta?.auth, options)
// If guest mode, redirect if authenticated
// 2. If guest mode, redirect if authenticated
if (only === 'guest' && loggedIn.value) {
// Avoid infinite redirect
console.log('[2] Guest mode: user is authenticated, redirecting to', redirectUserTo)
if (to.path === redirectUserTo) {
console.log('[2.1] Already at redirectUserTo:', redirectUserTo)
return
}
return navigateTo(redirectUserTo)
}
// If client-side, fetch session between each navigation
// 3. If client-side, fetch session between each navigation
if (import.meta.client) {
await fetchSession()
console.log('[3] Client-side navigation, fetching session')
try {
await fetchSession()
} catch (e) {
console.error(e)
}
}
// If not authenticated, redirect to home
// 4. If not authenticated, redirect to home or guest route
if (!loggedIn.value) {
// Avoid infinite redirect
if (to.path === redirectGuestTo) {
if (isPublicRoute(to)) {
console.log('[4] Not authenticated, but route is public:', to.path)
// Continue navigating to the public route
return
}
// No public route, redirect to guest route
console.log('[4.1] Not authenticated, redirecting to guest route:', redirectGuestTo)
return navigateTo(redirectGuestTo)
}
})