yunohost-portal/middleware/auth.global.ts
2023-10-17 14:47:46 +02:00

23 lines
601 B
TypeScript

import {
useIsLoggedIn,
useRedirectUrl,
useSettings,
} from '@/composables/states'
export default defineNuxtRouteMiddleware(async (to, from) => {
const isLoggedIn = useIsLoggedIn()
const settings = await useSettings()
useRedirectUrl().value = (from.query.r as string) || null
if (useRedirectUrl().value && to.name === 'login') {
to.query.r = useRedirectUrl().value
}
if (to.name === 'login') {
if (isLoggedIn.value) {
return navigateTo('/')
}
} else if (!isLoggedIn.value && !(to.meta.public && settings.value.public)) {
return navigateTo('/login')
}
})