add redirect after login

This commit is contained in:
axolotle 2023-07-26 05:24:04 +02:00
parent 9cc3bad2d5
commit 0628a09749
3 changed files with 16 additions and 3 deletions

View file

@ -1 +1,3 @@
export const useIsLoggedIn = () => useState<boolean>('isLoggedIn', () => false)
export const useRedirectUrl = () =>
useState<string | null>('redirectUrl', () => null)

View file

@ -1,12 +1,17 @@
import { useIsLoggedIn } from '@/composables/states'
import { useIsLoggedIn, useRedirectUrl } from '@/composables/states'
export default defineNuxtRouteMiddleware((to, from) => {
const isLoggedIn = useIsLoggedIn()
useRedirectUrl().value = (from.query.r as string) || null
if (useRedirectUrl().value && to.name === 'login') {
to.query.r = useRedirectUrl().value
}
if (to.name === 'login' && isLoggedIn.value) {
return navigateTo('/')
}
if (!isLoggedIn.value) {
navigateTo('/login')
if (to.name !== 'login' && !isLoggedIn.value) {
return navigateTo('/login')
}
})

View file

@ -19,6 +19,12 @@ async function login(form) {
})
if (!error.value) {
const redirectUrl = useRedirectUrl().value
if (redirectUrl) {
await navigateTo(atob(redirectUrl), { external: true })
}
isLoggedIn.value = true
await navigateTo('/')
} else {