forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusePromptContent.ts
More file actions
78 lines (68 loc) · 3.81 KB
/
usePromptContent.ts
File metadata and controls
78 lines (68 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type {OnyxEntry} from 'react-native-onyx';
import type DotLottieAnimation from '@components/LottieAnimations/types';
import {MULTIFACTOR_AUTHENTICATION_PROMPT_UI} from '@components/MultifactorAuthentication/config';
import type {MultifactorAuthenticationPromptType} from '@components/MultifactorAuthentication/config/types';
import useOnyx from '@hooks/useOnyx';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Account} from '@src/types/onyx';
import {useMultifactorAuthenticationState} from './State';
type PromptContent = {
animation: DotLottieAnimation;
title: TranslationPaths;
subtitle: TranslationPaths | undefined;
shouldDisplayConfirmButton: boolean;
};
/**
* Selector to check if server has any registered credentials for this account.
* Note: This checks server state only, not device-local credentials.
*/
function serverHasRegisteredCredentials(data: OnyxEntry<Account>) {
const credentialIDs = data?.multifactorAuthenticationPublicKeyIDs;
return credentialIDs && credentialIDs.length > 0;
}
/**
* Hook to get the prompt content (animation, title, subtitle) for the MFA prompt page.
* Handles the logic for determining the correct title and subtitle based on:
* - Whether the user is a returning user (already has biometrics registered)
* - Whether registration has just been completed
* - The default content from the prompt configuration
*
* Uses context state instead of Onyx for state changes during the flow to avoid
* timing issues with optimistic updates.
*/
function usePromptContent(promptType: MultifactorAuthenticationPromptType): PromptContent {
const {state} = useMultifactorAuthenticationState();
const [serverHasCredentials = false] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true, selector: serverHasRegisteredCredentials});
const [deviceBiometricsState] = useOnyx(ONYXKEYS.DEVICE_BIOMETRICS, {canBeMissing: true});
const hasEverAcceptedSoftPrompt = deviceBiometricsState?.hasAcceptedSoftPrompt ?? false;
const contentData = MULTIFACTOR_AUTHENTICATION_PROMPT_UI[promptType];
// Returning user: server has credentials, but user hasn't approved soft prompt yet
const isReturningUser = hasEverAcceptedSoftPrompt && serverHasCredentials && !state.softPromptApproved;
let title: TranslationPaths = contentData.title;
let subtitle: TranslationPaths | undefined = contentData.subtitle;
// Customize title and subtitle based on the user's scenario:
// 1. Returning User (isReturningUser): User already has biometrics registered on server and just opened the app.
// Show "Let's authenticate you" to guide into the authorization flow.
// 2. New User Registration Complete (isRegistrationComplete): User just finished registering biometrics in this session.
// Show "Now let's authenticate you" to transition from registration to authorization.
if (isReturningUser) {
title = 'multifactorAuthentication.letsAuthenticateYou';
subtitle = undefined;
} else if (state.isRegistrationComplete && hasEverAcceptedSoftPrompt) {
title = 'multifactorAuthentication.nowLetsAuthenticateYou';
subtitle = undefined;
}
// Display confirm button only for new users during their first biometric registration.
// Hide it for: users who already approved the soft prompt, users who finished registration,
// or returning users with existing server credentials. The button prompts users to enable biometrics.
const shouldDisplayConfirmButton = !hasEverAcceptedSoftPrompt || (!state.softPromptApproved && !state.isRegistrationComplete && !serverHasCredentials);
return {
animation: contentData.animation,
title,
subtitle,
shouldDisplayConfirmButton,
};
}
export default usePromptContent;
export {serverHasRegisteredCredentials};