Skip to content

Beautiful Open-Source Form Builder

Enhance two-factor authentication handling in login flow (#983)

Chirag Chhatrala136a32ce6486
diff --git a/client/components/pages/auth/components/LoginForm.vue b/client/components/pages/auth/components/LoginForm.vue
index 6c7260721..f6b38edb3 100644
--- a/client/components/pages/auth/components/LoginForm.vue
+++ b/client/components/pages/auth/components/LoginForm.vue
@@ -135,7 +135,7 @@ const oAuth = useOAuth()
const router = useRouter()
const { login: loginMutationFactory } = useAuth()
const authFlow = useAuthFlow()
-const { showTwoFactorModal, pendingAuthToken, handleTwoFactorVerified, handleTwoFactorCancel } = authFlow
+const { showTwoFactorModal, pendingAuthToken, handleTwoFactorVerified, handleTwoFactorCancel, handleTwoFactorError } = authFlow
// Feature flags
const oidcAvailable = computed(() => useFeatureFlag('oidc.available', false))
@@ -243,6 +243,14 @@ const login = () => {
redirect()
}
}).catch((error) => {
+ // Check if this is a 2FA requirement (422 with requires_2fa flag)
+ const twoFactorData = handleTwoFactorError(error)
+ if (twoFactorData) {
+ // This is a 2FA requirement, already handled in onError, just return
+ return
+ }
+
+ // Handle other errors
console.log(error)
if (error.response?._data?.message == "You must change your credentials when in self host mode") {
redirect()
diff --git a/client/composables/query/useAuth.js b/client/composables/query/useAuth.js
index 2769e04f8..50920c9d8 100644
--- a/client/composables/query/useAuth.js
+++ b/client/composables/query/useAuth.js
@@ -4,7 +4,7 @@ import { initServiceClients } from '~/composables/useAuthFlow'
export function useAuth() {
const queryClient = useQueryClient()
- const { handleAuthSuccess, handleManualLogout } = useAuthFlow()
+ const { handleAuthSuccess, handleManualLogout, handleTwoFactorError } = useAuthFlow()
const { isAuthenticated } = useIsAuthenticated()
// Queries
@@ -70,6 +70,18 @@ export function useAuth() {
// Handle auth flow coordination
handleAuthSuccess(tokenData, variables?.source || 'credentials')
},
+ onError: (error, variables) => {
+ // Check if this is a 2FA requirement (422 with requires_2fa flag)
+ const twoFactorData = handleTwoFactorError(error)
+ if (twoFactorData) {
+ // Handle auth flow coordination (will show 2FA modal)
+ handleAuthSuccess(twoFactorData, variables?.source || 'credentials')
+ return
+ }
+
+ // This is a real error, let it propagate
+ throw error
+ },
...options
})
}
diff --git a/client/composables/useAuthFlow.js b/client/composables/useAuthFlow.js
index 4c87e3bac..7d98368f3 100644
--- a/client/composables/useAuthFlow.js
+++ b/client/composables/useAuthFlow.js
@@ -27,16 +27,16 @@ export const initServiceClients = (userData) => {
useCrisp().setUser(userData)
}
+// Shared state for 2FA modal - ensures all instances of useAuthFlow share the same state
+const showTwoFactorModal = ref(false)
+const pendingAuthToken = ref(null)
+const pendingAuthContext = ref(null)
+
export const useAuthFlow = () => {
const authStore = useAuthStore()
const { logEvent } = useAmplitude()
const router = useRouter()
- // 2FA modal state
- const showTwoFactorModal = ref(false)
- const pendingAuthToken = ref(null)
- const pendingAuthContext = ref(null)
-
/**
* Core authentication success handler
* Now checks for 2FA requirement before proceeding