26 lines
698 B
TypeScript
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
|
|
}
|
|
})
|
|
}
|
|
})
|