Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Onfido flow while moving back from offline #49778

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"lodash-es": "4.17.21",
"lottie-react-native": "6.5.1",
"mapbox-gl": "^2.15.0",
"onfido-sdk-ui": "14.15.0",
"onfido-sdk-ui": "14.37.1",
"process": "^0.11.10",
"pusher-js": "8.3.0",
"react": "18.3.1",
Expand Down
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,7 @@ const CONST = {
WRONG_ANSWERS: 'Wrong answers',
ONFIDO_FIXABLE_ERROR: 'Onfido returned a fixable error',
ONFIDO_USER_CONSENT_DENIED: 'user_consent_denied',
ONFIDO_EXCEPTION: 'exception',

// KBA stands for Knowledge Based Answers (requiring us to show Idology questions)
KBA_NEEDED: 'KBA needed',
Expand Down
17 changes: 11 additions & 6 deletions src/components/Onfido/BaseOnfidoWeb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import variables from '@styles/variables';
import CONST from '@src/CONST';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import './index.css';
import type {OnfidoElement, OnfidoError, OnfidoProps} from './types';
import type {OnfidoElement, OnfidoProps} from './types';

type InitializeOnfidoProps = OnfidoProps &
Pick<LocaleContextProps, 'translate' | 'preferredLocale'> & {
Expand All @@ -23,7 +23,7 @@ type OnfidoEvent = Event & {
};

function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLocale, translate, theme}: InitializeOnfidoProps) {
OnfidoSDK.init({
return OnfidoSDK.init({
token: sdkToken,
containerId: CONST.ONFIDO.CONTAINER_ID,
customUI: {
Expand Down Expand Up @@ -94,10 +94,13 @@ function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLo
}
onSuccess(data);
},
onError: (error: OnfidoError) => {
onError: (error) => {
const errorType = error.type;
const errorMessage: string = error.message ?? CONST.ERROR.UNKNOWN_ERROR;
Log.hmmm('Onfido error', {errorType, errorMessage});
if (errorType === CONST.WALLET.ERROR.ONFIDO_EXCEPTION) {
return;
}
if (errorType === CONST.WALLET.ERROR.ONFIDO_USER_CONSENT_DENIED) {
onUserExit();
return;
Expand Down Expand Up @@ -129,7 +132,7 @@ function Onfido({sdkToken, onSuccess, onError, onUserExit}: OnfidoProps, ref: Fo
const theme = useTheme();

useEffect(() => {
initializeOnfido({
const onfido = initializeOnfido({
sdkToken,
onSuccess,
onError,
Expand All @@ -138,9 +141,11 @@ function Onfido({sdkToken, onSuccess, onError, onUserExit}: OnfidoProps, ref: Fo
translate,
theme,
});

window.addEventListener('userAnalyticsEvent', logOnFidoEvent);
return () => window.removeEventListener('userAnalyticsEvent', logOnFidoEvent);
return () => {
onfido.tearDown();
window.removeEventListener('userAnalyticsEvent', logOnFidoEvent);
};
// Onfido should be initialized only once on mount
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Onfido/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {OnfidoResult} from '@onfido/react-native-sdk';
import type {Handle} from 'onfido-sdk-ui/types/Onfido';
import type {CompleteData} from 'onfido-sdk-ui/types/Types';
import type {CompleteData} from 'onfido-sdk-ui/types/shared/SdkParameters';
import type {OnyxEntry} from 'react-native-onyx';

type OnfidoData = CompleteData | OnfidoResult;
Expand Down
6 changes: 6 additions & 0 deletions src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ function clearOnfidoToken() {
Onyx.merge(ONYXKEYS.ONFIDO_APPLICANT_ID, '');
}

function setLocalOnfidoTokens(token: string, applicationID: string) {
Onyx.merge(ONYXKEYS.ONFIDO_TOKEN, token);
Onyx.merge(ONYXKEYS.ONFIDO_APPLICANT_ID, applicationID);
}

function updateAddPersonalBankAccountDraft(bankData: Partial<PersonalBankAccountForm>) {
Onyx.merge(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, bankData);
}
Expand Down Expand Up @@ -546,6 +551,7 @@ export {
addBusinessWebsiteForDraft,
addPersonalBankAccount,
clearOnfidoToken,
setLocalOnfidoTokens,
clearPersonalBankAccount,
clearPlaid,
setPlaidEvent,
Expand Down
22 changes: 22 additions & 0 deletions src/pages/ReimbursementAccount/ReimbursementAccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type ReimbursementAccountOnyxProps = {

/** The token required to initialize the Onfido SDK */
onfidoToken: OnyxEntry<string>;

/** The application id which connected to onfido session */
onfidoApplicantID: OnyxEntry<string>;
};

type ReimbursementAccountPageProps = WithPolicyOnyxProps &
Expand Down Expand Up @@ -128,6 +131,7 @@ function ReimbursementAccountPage({
reimbursementAccount,
route,
onfidoToken = '',
onfidoApplicantID = '',
policy,
account,
isLoadingApp = false,
Expand Down Expand Up @@ -225,6 +229,7 @@ function ReimbursementAccountPage({
const prevIsReimbursementAccountLoading = usePrevious(reimbursementAccount?.isLoading);
const prevReimbursementAccount = usePrevious(reimbursementAccount);
const prevIsOffline = usePrevious(isOffline);
const onfidoLocalTokensRef = useRef({onfidoToken: '', onfidoApplicantID: ''});

/**
* Retrieve verified business bank account currently being set up.
Expand All @@ -243,6 +248,20 @@ function ReimbursementAccountPage({
BankAccounts.openReimbursementAccountPage(stepToOpen, ignoreLocalSubStep ? '' : subStep, ignoreLocalCurrentStep ? '' : localCurrentStep, policyIDParam);
}

useEffect(() => {
if (!onfidoToken) {
return;
}
onfidoLocalTokensRef.current = {onfidoToken, onfidoApplicantID};
}, [onfidoToken, onfidoApplicantID]);

useEffect(() => {
if (!prevIsReimbursementAccountLoading && !onfidoLocalTokensRef.current.onfidoToken) {
return;
}
BankAccounts.setLocalOnfidoTokens(onfidoLocalTokensRef.current.onfidoToken, onfidoLocalTokensRef.current.onfidoApplicantID);
}, [prevIsReimbursementAccountLoading]);

useEffect(() => {
if (!isReimbursementAccountLoading) {
return;
Expand Down Expand Up @@ -527,6 +546,9 @@ export default withPolicy(
onfidoToken: {
key: ONYXKEYS.ONFIDO_TOKEN,
},
onfidoApplicantID: {
key: ONYXKEYS.ONFIDO_APPLICANT_ID,
},
isLoadingApp: {
key: ONYXKEYS.IS_LOADING_APP,
},
Expand Down
Loading