diff --git a/src/features/app_start/vip/congregation_create/congregation_access_code/useCongregationAccessCode.tsx b/src/features/app_start/vip/congregation_create/congregation_access_code/useCongregationAccessCode.tsx index 22c95bca9c..a8c7a8456f 100644 --- a/src/features/app_start/vip/congregation_create/congregation_access_code/useCongregationAccessCode.tsx +++ b/src/features/app_start/vip/congregation_create/congregation_access_code/useCongregationAccessCode.tsx @@ -48,14 +48,7 @@ const useCongregationAccessCode = () => { const { status, data } = await apiSetCongregationAccessCode(encryptedKey); if (status !== 200) { - await displayOnboardingFeedback({ - title: t('error_app_generic-title'), - message: getMessageByCode(data.message), - }); - showMessage(); - - setIsProcessing(false); - return; + throw new Error(data.message); } await dbAppSettingsUpdate({ diff --git a/src/features/app_start/vip/congregation_create/congregation_master_key/useCongregationMasterKey.tsx b/src/features/app_start/vip/congregation_create/congregation_master_key/useCongregationMasterKey.tsx index e6e80b3296..2f29d51018 100644 --- a/src/features/app_start/vip/congregation_create/congregation_master_key/useCongregationMasterKey.tsx +++ b/src/features/app_start/vip/congregation_create/congregation_master_key/useCongregationMasterKey.tsx @@ -46,14 +46,7 @@ const useCongregationMasterKey = () => { const { status, data } = await apiSetCongregationMasterKey(encryptedKey); if (status !== 200) { - await displayOnboardingFeedback({ - title: t('error_app_generic-title'), - message: getMessageByCode(data.message), - }); - showMessage(); - - setIsProcessing(false); - return; + throw new Error(data.message); } await dbAppSettingsUpdate({ diff --git a/src/features/app_start/vip/oauth/button_base/useButtonBase.tsx b/src/features/app_start/vip/oauth/button_base/useButtonBase.tsx index 92ec80ca49..e3f03010a1 100644 --- a/src/features/app_start/vip/oauth/button_base/useButtonBase.tsx +++ b/src/features/app_start/vip/oauth/button_base/useButtonBase.tsx @@ -1,5 +1,7 @@ import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; import { + congIDState, + congregationCreateStepState, currentProviderState, isAuthProcessingState, isEmailAuthState, @@ -15,7 +17,7 @@ import { displayOnboardingFeedback } from '@services/recoil/app'; import { getMessageByCode } from '@services/i18n/translation'; import { apiSendAuthorization } from '@services/api/user'; import { dbAppSettingsUpdate } from '@services/dexie/settings'; -import { APP_ROLES } from '@constants/index'; +import { APP_ROLES, VIP_ROLES } from '@constants/index'; import { NextStepType } from './index.types'; import { UserLoginResponseType } from '@definition/api'; import { settingsState } from '@states/settings'; @@ -38,6 +40,8 @@ const useButtonBase = ({ provider, isEmail }) => { const setIsEncryptionCodeOpen = useSetRecoilState(isEncryptionCodeOpenState); const setIsEmailAuth = useSetRecoilState(isEmailAuthState); const setTokenDev = useSetRecoilState(tokenDevState); + const setCurrentStep = useSetRecoilState(congregationCreateStepState); + const setCongID = useSetRecoilState(congIDState); const settings = useRecoilValue(settingsState); const currentProvider = useRecoilValue(currentProviderState); @@ -60,35 +64,53 @@ const useButtonBase = ({ provider, isEmail }) => { if (message === 'MFA_VERIFY') { nextStep.isVerifyMFA = true; + return nextStep; } - if (app_settings?.user_settings.mfa === 'not_enabled') { - if (!app_settings.cong_settings) { - nextStep.createCongregation = true; - } + if (!app_settings) return nextStep; - if ( - app_settings.cong_settings && - app_settings.user_settings.cong_role?.length === 0 - ) { - nextStep.unauthorized = true; - } + const { user_settings, cong_settings } = app_settings; - if (app_settings.user_settings.cong_role?.length > 0) { - const approvedRole = app_settings.user_settings.cong_role.some((role) => - APP_ROLES.includes(role) - ); + if (!cong_settings) { + nextStep.createCongregation = true; + return nextStep; + } - if (!approvedRole) { - nextStep.unauthorized = true; - } + if (!user_settings.cong_role || user_settings.cong_role?.length === 0) { + nextStep.unauthorized = true; + return nextStep; + } - if (approvedRole) { - nextStep.encryption = true; - } - } + const approvedRole = user_settings.cong_role.some((role) => + APP_ROLES.includes(role) + ); + + if (!approvedRole) { + nextStep.unauthorized = true; + return nextStep; + } + + const remoteMasterKey = cong_settings.cong_master_key; + const remoteAccessCode = cong_settings.cong_access_code; + const masterKeyNeeded = user_settings.cong_role.some((role) => + VIP_ROLES.includes(role) + ); + + if (masterKeyNeeded && remoteMasterKey.length === 0) { + setCongID(cong_settings.id); + setCurrentStep(1); + nextStep.createCongregation = true; + return nextStep; + } + + if (remoteAccessCode.length === 0) { + setCongID(cong_settings.id); + setCurrentStep(2); + nextStep.createCongregation = true; + return nextStep; } + nextStep.encryption = true; return nextStep; }; diff --git a/src/features/app_start/vip/startup/useStartup.tsx b/src/features/app_start/vip/startup/useStartup.tsx index 839d2c62e8..2f1a97e0f2 100644 --- a/src/features/app_start/vip/startup/useStartup.tsx +++ b/src/features/app_start/vip/startup/useStartup.tsx @@ -135,11 +135,9 @@ const useStartup = () => { return; } - if (status === 200) { - if (congNumber.length > 0 && result.cong_number !== congNumber) { - await handleDeleteDatabase(); - return; - } + if (congNumber.length > 0 && result.cong_number !== congNumber) { + await handleDeleteDatabase(); + return; } const remoteMasterKey = result.cong_master_key || ''; @@ -147,13 +145,16 @@ const useStartup = () => { if ( isAuthenticated && - congName.length === 0 && (remoteMasterKey.length === 0 || remoteAccessCode.length === 0) ) { setCongID(result.cong_id); if (masterKeyNeeded && remoteMasterKey.length === 0) { setCurrentStep(1); + setIsLoading(false); + setIsStart(false); + setCongCreate(true); + return; } if ( @@ -162,15 +163,17 @@ const useStartup = () => { remoteAccessCode.length === 0 ) { setCurrentStep(2); + setIsLoading(false); + setIsStart(false); + setCongCreate(true); + return; } - - setIsLoading(false); - setIsStart(false); - setCongCreate(true); - return; } - if (congAccessCode.length === 0 && congName.length > 0) { + if ( + (masterKeyNeeded && congMasterKey.length === 0) || + congAccessCode.length === 0 + ) { setIsStart(false); setIsEncryptionCodeOpen(true); }