Files
money.sori.studio/middleware/auth.global.ts
2026-08-14 13:29:26 +09:00

26 lines
698 B
TypeScript

export default defineNuxtRouteMiddleware(async (to) => {
if (to.path === '/login' || to.path.startsWith('/share/')) return
const session = useState<{ id: string; email: string; name: string } | null>('session')
const sessionCookie = useCookie<string | null>('money_session')
const authReason = sessionCookie.value ? 'expired' : 'required'
if (session.value === undefined) {
try {
session.value = await $fetch('/api/auth/me')
} catch {
session.value = null
}
}
if (!session.value) {
return navigateTo({
path: '/login',
query: {
reason: authReason,
redirect: to.fullPath === '/' ? undefined : to.fullPath
}
})
}
})