Skip to content

Instantly share code, notes, and snippets.

@vdhanan
Created May 6, 2024 14:03
Show Gist options
  • Save vdhanan/64cdb241fa550ba8711d4ee1f766d807 to your computer and use it in GitHub Desktop.
Save vdhanan/64cdb241fa550ba8711d4ee1f766d807 to your computer and use it in GitHub Desktop.
diff --git a/native/components/background-identity-login-handler.react.js b/native/components/background-identity-login-handler.react.js
index b7288bbbae..f6da27f0d6 100644
--- a/native/components/background-identity-login-handler.react.js
+++ b/native/components/background-identity-login-handler.react.js
@@ -3,12 +3,13 @@
import * as React from 'react';
import { logOutActionTypes, useLogOut } from 'lib/actions/user-actions.js';
-import { useModalContext } from 'lib/components/modal-provider.react.js';
+import { usePasswordLogIn } from 'lib/hooks/login-hooks.js';
import { accountHasPassword } from 'lib/shared/account-utils.js';
import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js';
import { useSelector } from 'lib/utils/redux-utils.js';
import { usingCommServicesAccessToken } from 'lib/utils/services-utils.js';
+import { fetchNativeKeychainCredentials } from '../account/native-credentials.js';
import Alert from '../utils/alert.js';
function BackgroundIdentityLoginHandler() {
@@ -21,28 +22,49 @@ function BackgroundIdentityLoginHandler() {
const hasAccessToken = useSelector(state => !!state.commServicesAccessToken);
const dataLoaded = useSelector(state => state.dataLoaded);
- const { pushModal } = useModalContext();
+ const callIdentityPasswordLogIn = usePasswordLogIn();
- React.useEffect(() => {
+ const handleLogOutAndAlert = React.useCallback(() => {
+ void dispatchActionPromise(logOutActionTypes, callLogOut());
+ Alert.alert(
+ 'Security update',
+ 'Unfortunately, we must log you out as we perform an update to our system.',
+ [{ text: 'OK' }],
+ );
+ }, [dispatchActionPromise, callLogOut]);
+
+ const logInIfPossibleElseLogOut = React.useCallback(async () => {
if (hasAccessToken || !dataLoaded || !usingCommServicesAccessToken) {
return;
}
- if (!isAccountWithPassword) {
- void dispatchActionPromise(logOutActionTypes, callLogOut());
- Alert.alert(
- 'Security update',
- 'Unfortunately, we must log you out as we perform an update to our system.',
- [{ text: 'OK' }],
- );
+
+ if (isAccountWithPassword) {
+ const nativeCredentials = await fetchNativeKeychainCredentials();
+ if (!nativeCredentials) {
+ return;
+ }
+ try {
+ await callIdentityPasswordLogIn(
+ nativeCredentials.username,
+ nativeCredentials.password,
+ );
+ } catch (e) {
+ handleLogOutAndAlert();
+ }
+ } else {
+ handleLogOutAndAlert();
}
}, [
- callLogOut,
+ callIdentityPasswordLogIn,
dataLoaded,
- dispatchActionPromise,
+ handleLogOutAndAlert,
hasAccessToken,
isAccountWithPassword,
- pushModal,
]);
+
+ React.useEffect(() => {
+ logInIfPossibleElseLogOut();
+ }, [logInIfPossibleElseLogOut]);
}
export default BackgroundIdentityLoginHandler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment