Skip to content

Commit ce1f396

Browse files
committed
Move redirect logic in generic error handling
1 parent 7cb2c4b commit ce1f396

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

components/User/TwoFactorSetupModal.vue

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,12 @@ const {
111111
} = useTwoFactorSetup()
112112
113113
const fetchStatusAndQRCode = async () => {
114-
const { data: twoFactorData } = await useAPI<{ response: { tf_primary_method: string | null, reauth_required: boolean } | null }>('/tf-setup')
115-
isConfigured.value = !!twoFactorData.value?.response?.tf_primary_method
116-
if (twoFactorData.value?.response?.reauth_required) {
117-
toast.error(t('Vous devez confirmer votre mot de passe pour configurer l\'authentification deux facteurs.'))
118-
isOpen.value = false
119-
await navigateTo({ path: '/verify', query: { next: '/admin/me/profile' } }, { replace: true })
114+
// fetch two factor status. If freshness is required, a 401 is returned with automatic handling and redirection to /verify
115+
const { data: twoFactorData, status } = await useAPI<{ response: { tf_primary_method: string | null, reauth_required: boolean } | null }>('/tf-setup')
116+
if (status.value !== 'error') {
117+
isConfigured.value = !!twoFactorData.value?.response?.tf_primary_method
118+
await fetchQRCode()
120119
}
121-
122-
await fetchQRCode()
123120
}
124121
125122
const submit = async (close: () => void) => {

plugins/api.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ export default defineNuxtPlugin({
4545
}
4646

4747
if (response.status === 401) {
48-
await nuxtApp.runWithContext(() => navigateTo({ path: '/login', query: { next: route.fullPath } }))
48+
if (response._data?.response && typeof response._data.response === 'object' && 'reauth_required' in response._data.response) {
49+
await nuxtApp.runWithContext(() => navigateTo({ path: '/verify', query: { next: route.fullPath } }))
50+
}
51+
else {
52+
await nuxtApp.runWithContext(() => navigateTo({ path: '/login', query: { next: route.fullPath } }))
53+
}
4954
}
5055

5156
if (response.status === 429) {
@@ -70,6 +75,9 @@ export default defineNuxtPlugin({
7075
else if ('errors' in response._data && typeof response._data.errors === 'object') {
7176
message = Object.entries(response._data.errors).map(([key, value]) => `${key}: ${value}`).join(' ; ')
7277
}
78+
else if ('response' in response._data && 'errors' in response._data.response && Array.isArray(response._data.response.errors)) {
79+
message = response._data.response.errors.join(' ; ')
80+
}
7381
}
7482
catch (e) {
7583
console.error(e)

0 commit comments

Comments
 (0)