fix: avoid clearing saved redirect in onBeforeMount to prevent race with submit

When Login.vue re-mounts inside the authenticated layout after a
successful login, its onBeforeMount hook fires again. If it calls
redirectIfSaved(), it clears the saved route from localStorage before
the submit() handler's redirectIfSaved() can use it, causing a redirect
to home instead of the saved route. Use router.push({name: 'home'})
directly since the only purpose here is to redirect already-authenticated
users away from the login page.
This commit is contained in:
kolaente
2026-02-06 10:21:13 +01:00
parent b3e95e9f4e
commit 0e2ea5c42a

View File

@@ -125,6 +125,7 @@
<script setup lang="ts">
import {computed, onBeforeMount, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import {useRouter} from 'vue-router'
import {useDebounceFn} from '@vueuse/core'
import Message from '@/components/misc/Message.vue'
@@ -143,6 +144,7 @@ import {useTitle} from '@/composables/useTitle'
const {t} = useI18n({useScope: 'global'})
useTitle(() => t('user.auth.login'))
const router = useRouter()
const authStore = useAuthStore()
const configStore = useConfigStore()
const {redirectIfSaved} = useRedirectToLastVisited()
@@ -171,9 +173,13 @@ onBeforeMount(() => {
errorMessage.value = e.message
})
// Check if the user is already logged in, if so, redirect them to the homepage
// Check if the user is already logged in, if so, redirect them to the homepage.
// We intentionally use router.push here instead of redirectIfSaved() because
// this hook also fires when Login.vue re-mounts inside the authenticated layout
// after a successful login. Using redirectIfSaved() here would clear the saved
// route before the submit() handler gets a chance to use it.
if (authenticated.value) {
redirectIfSaved()
router.push({name: 'home'})
}
})