Skip to content

Commit

Permalink
fix(startup): unable to create master key and access code
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 20, 2024
1 parent 94b63d2 commit 1227058
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
66 changes: 44 additions & 22 deletions src/features/app_start/vip/oauth/button_base/useButtonBase.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import {
congIDState,
congregationCreateStepState,
currentProviderState,
isAuthProcessingState,
isEmailAuthState,
Expand All @@ -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';
Expand All @@ -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);
Expand All @@ -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;
};

Expand Down
27 changes: 15 additions & 12 deletions src/features/app_start/vip/startup/useStartup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,26 @@ 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 || '';
const remoteAccessCode = result.cong_access_code || '';

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 (
Expand All @@ -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);
}
Expand Down

0 comments on commit 1227058

Please sign in to comment.