2023-09-04 16:49:13 +02:00
|
|
|
import {
|
|
|
|
useIsLoggedIn,
|
|
|
|
useRedirectUrl,
|
|
|
|
useSettings,
|
|
|
|
} from '@/composables/states'
|
2023-07-20 17:36:08 +02:00
|
|
|
|
2023-09-04 16:49:13 +02:00
|
|
|
export default defineNuxtRouteMiddleware(async (to, from) => {
|
2023-07-20 17:36:08 +02:00
|
|
|
const isLoggedIn = useIsLoggedIn()
|
2023-09-04 16:49:13 +02:00
|
|
|
const settings = await useSettings()
|
2023-07-20 17:36:08 +02:00
|
|
|
|
2023-07-26 05:24:04 +02:00
|
|
|
useRedirectUrl().value = (from.query.r as string) || null
|
|
|
|
if (useRedirectUrl().value && to.name === 'login') {
|
|
|
|
to.query.r = useRedirectUrl().value
|
|
|
|
}
|
|
|
|
|
2023-07-20 17:36:08 +02:00
|
|
|
if (to.name === 'login' && isLoggedIn.value) {
|
|
|
|
return navigateTo('/')
|
|
|
|
}
|
2023-09-04 16:49:13 +02:00
|
|
|
if (!isLoggedIn.value && !(to.meta.public && settings.value.public)) {
|
2023-07-26 05:24:04 +02:00
|
|
|
return navigateTo('/login')
|
2023-07-20 17:36:08 +02:00
|
|
|
}
|
|
|
|
})
|