yunohost-portal/middleware/auth.global.ts

37 lines
847 B
TypeScript
Raw Permalink Normal View History

import {
useIsLoggedIn,
useRedirectUrl,
2023-11-30 15:59:41 +01:00
useQueryMsg,
useSettings,
} from '@/composables/states'
export default defineNuxtRouteMiddleware(async (to) => {
const isLoggedIn = useIsLoggedIn()
const settings = await useSettings()
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 = to.query.msg as string
2023-11-30 15:59:41 +01:00
} 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')
}
})