2023-09-04 16:49:13 +02:00
|
|
|
import {
|
|
|
|
useIsLoggedIn,
|
|
|
|
useRedirectUrl,
|
2023-11-30 15:59:41 +01:00
|
|
|
useQueryMsg,
|
2023-09-04 16:49:13 +02:00
|
|
|
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-11-30 15:58:10 +01:00
|
|
|
const redirectUrl = useRedirectUrl()
|
|
|
|
if (to.query.r) {
|
|
|
|
redirectUrl.value = to.query.r as string
|
|
|
|
if (to.name !== 'login') {
|
|
|
|
return navigateTo({ path: '/login', query: { r: to.query.r } })
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
redirectUrl.value = null
|
2023-07-26 05:24:04 +02:00
|
|
|
}
|
|
|
|
|
2023-11-30 15:59:41 +01:00
|
|
|
const queryMsg = useQueryMsg()
|
|
|
|
if (to.query.msg) {
|
|
|
|
queryMsg.value = from.query.msg as string
|
|
|
|
} else {
|
|
|
|
queryMsg.value = null
|
|
|
|
}
|
|
|
|
|
2023-10-17 14:47:46 +02:00
|
|
|
if (to.name === 'login') {
|
|
|
|
if (isLoggedIn.value) {
|
|
|
|
return navigateTo('/')
|
|
|
|
}
|
|
|
|
} else 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
|
|
|
}
|
|
|
|
})
|