Fix redirect to last page when login guard breaks flow

This commit is contained in:
wvffle 2022-11-28 18:40:12 +00:00 committed by Kasper Seweryn
parent d2cf335a66
commit b1828394ce
2 changed files with 5 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import type { BackendError } from '~/types'
import type { RouteLocationRaw } from 'vue-router' import type { RouteLocationRaw } from 'vue-router'
import { ref, reactive, computed, onMounted, nextTick } from 'vue' import { ref, reactive, computed, onMounted, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import { useStore } from '~/store' import { useStore } from '~/store'
@ -23,6 +24,7 @@ const props = withDefaults(defineProps<Props>(), {
const domain = location.hostname const domain = location.hostname
const { t } = useI18n() const { t } = useI18n()
const store = useStore() const store = useStore()
const router = useRouter()
const credentials = reactive({ const credentials = reactive({
username: '', username: '',
@ -47,6 +49,7 @@ const submit = async () => {
try { try {
if (domain === store.getters['instance/domain']) { if (domain === store.getters['instance/domain']) {
await store.dispatch('auth/login', { credentials }) await store.dispatch('auth/login', { credentials })
await router.push(props.next)
} else { } else {
await store.dispatch('auth/oauthLogin', props.next) await store.dispatch('auth/oauthLogin', props.next)
} }

View File

@ -2,7 +2,6 @@
import type { NavigationGuardNext, RouteLocationNamedRaw, RouteLocationNormalized } from 'vue-router' import type { NavigationGuardNext, RouteLocationNamedRaw, RouteLocationNormalized } from 'vue-router'
import type { Permission } from '~/store/auth' import type { Permission } from '~/store/auth'
import router from '~/router'
import store from '~/store' import store from '~/store'
export const hasPermissions = (permission: Permission) => (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => { export const hasPermissions = (permission: Permission) => (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
@ -16,7 +15,8 @@ export const hasPermissions = (permission: Permission) => (to: RouteLocationNorm
export const requireLoggedIn = (fallbackLocation?: RouteLocationNamedRaw) => (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => { export const requireLoggedIn = (fallbackLocation?: RouteLocationNamedRaw) => (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
if (store.state.auth.authenticated) return next() if (store.state.auth.authenticated) return next()
return next(fallbackLocation ?? { name: 'login', query: { next: router.currentRoute.value.fullPath } }) console.log('!', to)
return next(fallbackLocation ?? { name: 'login', query: { next: to.fullPath } })
} }
export const requireLoggedOut = (fallbackLocation: RouteLocationNamedRaw) => (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => { export const requireLoggedOut = (fallbackLocation: RouteLocationNamedRaw) => (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {