diff --git a/lib/hooks/login-hooks.js b/lib/hooks/login-hooks.js --- a/lib/hooks/login-hooks.js +++ b/lib/hooks/login-hooks.js @@ -27,35 +27,21 @@ const inactiveStep = { step: 'inactive' }; -type UsePasswordLogInInput = { - // Called after successful identity auth, but before successful authoritative - // keyserver auth. Used by callers to trigger local persistence of credentials - +saveCredentials?: ?({ +username: string, +password: string }) => mixed, -}; -function usePasswordLogIn( - input?: ?UsePasswordLogInInput, -): (username: string, password: string) => Promise { +function usePasswordLogIn(): ( + username: string, + password: string, +) => Promise { const [currentStep, setCurrentStep] = React.useState(inactiveStep); - const saveCredentials = input?.saveCredentials; const identityPasswordLogIn = useIdentityPasswordLogIn(); - const identityLogInAction = React.useCallback( - async (username: string, password: string) => { - const result = await identityPasswordLogIn(username, password); - saveCredentials?.({ username, password }); - return result; - }, - [identityPasswordLogIn, saveCredentials], - ); - const dispatchActionPromise = useDispatchActionPromise(); const returnedFunc = React.useCallback( (username: string, password: string) => new Promise( // eslint-disable-next-line no-async-promise-executor async (resolve, reject) => { - const action = identityLogInAction(username, password); + const action = identityPasswordLogIn(username, password); void dispatchActionPromise(identityLogInActionTypes, action); try { await action; @@ -69,7 +55,7 @@ } }, ), - [dispatchActionPromise, identityLogInAction], + [dispatchActionPromise, identityPasswordLogIn], ); const keyserverAuth = useKeyserverAuth(authoritativeKeyserverID()); diff --git a/native/account/log-in-panel.react.js b/native/account/log-in-panel.react.js --- a/native/account/log-in-panel.react.js +++ b/native/account/log-in-panel.react.js @@ -330,6 +330,10 @@ this.passwordInputText, ); this.props.setActiveAlert(false); + await setNativeCredentials({ + username: this.usernameInputText, + password: this.passwordInputText, + }); } catch (e) { const messageForException = getMessageForException(e); if ( @@ -435,7 +439,6 @@ ); const olmSessionInitializationDataLoadingStatusSelector = createLoadingStatusSelector(getOlmSessionInitializationDataActionTypes); -const identityPasswordLogInInput = { saveCredentials: setNativeCredentials }; const ConnectedLogInPanel: React.ComponentType = React.memo(function ConnectedLogInPanel(props: BaseProps) { @@ -452,9 +455,7 @@ const dispatchActionPromise = useDispatchActionPromise(); const callLegacyLogIn = useLegacyLogIn(); - const callIdentityPasswordLogIn = usePasswordLogIn( - identityPasswordLogInInput, - ); + const callIdentityPasswordLogIn = usePasswordLogIn(); const getInitialNotificationsEncryptedMessage = useInitialNotificationsEncryptedMessage(authoritativeKeyserverID); diff --git a/native/account/registration/registration-server-call.js b/native/account/registration/registration-server-call.js --- a/native/account/registration/registration-server-call.js +++ b/native/account/registration/registration-server-call.js @@ -65,6 +65,7 @@ +step: 'waiting_for_identity_registration', +clearCachedSelections: () => void, +avatarData: ?AvatarData, + +credentialsToSave: ?{ +username: string, +password: string }, +resolve: () => void, +reject: Error => void, } @@ -72,6 +73,7 @@ +step: 'waiting_for_authoritative_keyserver_registration', +clearCachedSelections: () => void, +avatarData: ?AvatarData, + +credentialsToSave: ?{ +username: string, +password: string }, +resolve: () => void, +reject: Error => void, }; @@ -97,16 +99,11 @@ ) => { const identityRegisterPromise = (async () => { try { - const result = await callIdentityPasswordRegister( + return await callIdentityPasswordRegister( accountSelection.username, accountSelection.password, farcasterID, ); - await setNativeCredentials({ - username: accountSelection.username, - password: accountSelection.password, - }); - return result; } catch (e) { if (e.message === 'username reserved') { Alert.alert( @@ -149,7 +146,7 @@ const extraInfo = await logInExtraInfo(); const keyserverRegisterPromise = (async () => { try { - const result = await callKeyserverRegister( + return await callKeyserverRegister( { ...extraInfo, username: accountSelection.username, @@ -159,11 +156,6 @@ urlPrefixOverride: keyserverURL, }, ); - await setNativeCredentials({ - username: result.currentUserInfo.username, - password: accountSelection.password, - }); - return result; } catch (e) { if (e.message === 'username_reserved') { Alert.alert( @@ -277,10 +269,18 @@ if (siweBackupSecrets) { await commCoreModule.setSIWEBackupSecrets(siweBackupSecrets); } + const credentialsToSave = + accountSelection.accountType === 'username' + ? { + username: accountSelection.username, + password: accountSelection.password, + } + : null; setCurrentStep({ step: 'waiting_for_identity_registration', avatarData, clearCachedSelections, + credentialsToSave, resolve, reject, }); @@ -320,7 +320,13 @@ return; } registeringOnAuthoritativeKeyserverRef.current = true; - const { avatarData, clearCachedSelections, resolve, reject } = currentStep; + const { + avatarData, + clearCachedSelections, + credentialsToSave, + resolve, + reject, + } = currentStep; void (async () => { try { await keyserverAuth({ @@ -335,6 +341,7 @@ step: 'waiting_for_authoritative_keyserver_registration', avatarData, clearCachedSelections, + credentialsToSave, resolve, reject, }); @@ -366,7 +373,8 @@ return; } avatarBeingSetRef.current = true; - const { avatarData, resolve, clearCachedSelections } = currentStep; + const { avatarData, resolve, clearCachedSelections, credentialsToSave } = + currentStep; void (async () => { try { if (!avatarData) { @@ -391,6 +399,9 @@ }, }); clearCachedSelections(); + if (credentialsToSave) { + void setNativeCredentials(credentialsToSave); + } setCurrentStep(inactiveStep); avatarBeingSetRef.current = false; resolve();