Skip to content

Commit

Permalink
Bug/3464 fix login bug (#3467)
Browse files Browse the repository at this point in the history
* Fix switch navigator replacement logic

* Update unit tests

* Fix unit tests. Commit changes from yarn setup

* Fix login bug. Temporarily control splash animation.

* Fix login bug

Co-authored-by: sethkfman <10342624+sethkfman@users.noreply.github.com>
Co-authored-by: Curtis <Curtis.David7@gmail.com>
  • Loading branch information
3 people authored Dec 4, 2021
1 parent 3eedff8 commit b49b535
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 42 deletions.
6 changes: 6 additions & 0 deletions app/actions/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ export function logOut() {
type: 'LOGOUT',
};
}

export function checkedAuth() {
return {
type: 'CHECKED_AUTH',
};
}
97 changes: 60 additions & 37 deletions app/components/Nav/App/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { NavigationContainer, CommonActions } from '@react-navigation/native';
import { Animated } from 'react-native';
import { Animated, StyleSheet, View } from 'react-native';
import { createStackNavigator } from '@react-navigation/stack';
import { createDrawerNavigator } from '@react-navigation/drawer';
import Login from '../../Views/Login';
Expand Down Expand Up @@ -30,10 +30,14 @@ import { trackErrorAsAnalytics } from '../../../util/analyticsV2';
import { routingInstrumentation } from '../../../util/setupSentry';
import Analytics from '../../../core/Analytics';
import AsyncStorage from '@react-native-community/async-storage';
import { connect } from 'react-redux';

import { connect, useSelector, useDispatch } from 'react-redux';
import { EXISTING_USER, CURRENT_APP_VERSION, LAST_APP_VERSION } from '../../../constants/storage';
import { getVersion } from 'react-native-device-info';
import { checkedAuth } from '../../../actions/user';

const styles = StyleSheet.create({
fill: { flex: 1 },
});

const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
Expand Down Expand Up @@ -147,6 +151,10 @@ const App = ({ userLoggedIn }) => {
const [route, setRoute] = useState();
const [animationPlayed, setAnimationPlayed] = useState();

const isAuthChecked = useSelector((state) => state.user.isAuthChecked);
const dispatch = useDispatch();
const triggerCheckedAuth = () => dispatch(checkedAuth());

const handleDeeplink = useCallback(({ error, params, uri }) => {
if (error) {
trackErrorAsAnalytics(error, 'Branch:');
Expand Down Expand Up @@ -196,6 +204,9 @@ const App = ({ userLoggedIn }) => {
const existingUser = await AsyncStorage.getItem(EXISTING_USER);
const route = !existingUser ? 'OnboardingRootNav' : 'Login';
setRoute(route);
if (!existingUser) {
triggerCheckedAuth();
}
}

checkExsiting();
Expand Down Expand Up @@ -225,14 +236,23 @@ const App = ({ userLoggedIn }) => {
} catch (error) {
Logger.error(error);
}

animation?.current?.play();
animationName?.current?.play();
}

startApp();
}, []);

useEffect(() => {
if (!isAuthChecked) {
return;
}
const startAnimation = async () => {
await new Promise((res) => setTimeout(res, 50));
animation?.current?.play();
animationName?.current?.play();
};
startAnimation();
}, [isAuthChecked]);

const onAnimationFinished = useCallback(() => {
Animated.timing(opacity, {
toValue: 0,
Expand All @@ -244,41 +264,44 @@ const App = ({ userLoggedIn }) => {
});
}, [opacity]);

if (!animationPlayed) {
return (
<MetaMaskAnimation
animation={animation}
animationName={animationName}
opacity={opacity}
onAnimationFinish={onAnimationFinished}
/>
);
}
const renderSplash = () => {
if (!animationPlayed) {
return (
<MetaMaskAnimation
animation={animation}
animationName={animationName}
opacity={opacity}
onAnimationFinish={onAnimationFinished}
/>
);
}
return null;
};

return (
// do not render unless a route is defined
(route && (
<NavigationContainer
ref={navigator}
onReady={() => {
routingInstrumentation.registerNavigationContainer(navigator);
}}
>
<Stack.Navigator route={route} initialRouteName={route}>
{userLoggedIn ? (
<Stack.Screen name="HomeNav" component={HomeNav} options={{ headerShown: false }} />
) : (
<>
<Stack.Screen name="Login" component={Login} options={{ headerShown: false }} />
<Stack.Screen
name="OnboardingRootNav"
component={OnboardingRootNav}
options={{ headerShown: false }}
/>
</>
)}
</Stack.Navigator>
</NavigationContainer>
<View style={styles.fill}>
<NavigationContainer
ref={navigator}
onReady={() => {
routingInstrumentation.registerNavigationContainer(navigator);
}}
>
<Stack.Navigator route={route} initialRouteName={route}>
<Stack.Screen name="Login" component={Login} options={{ headerShown: false }} />
<Stack.Screen
name="OnboardingRootNav"
component={OnboardingRootNav}
options={{ headerShown: false }}
/>
{userLoggedIn && (
<Stack.Screen name="HomeNav" component={HomeNav} options={{ headerShown: false }} />
)}
</Stack.Navigator>
</NavigationContainer>
{renderSplash()}
</View>
)) ||
null
);
Expand Down
1 change: 1 addition & 0 deletions app/components/UI/DrawerView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ class DrawerView extends PureComponent {
};

logOut = () => {
this.props.navigation.navigate('Login');
this.props.logOut();
};

Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/MetaMaskAnimation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const LOGO_PADDING = 25;

const styles = StyleSheet.create({
main: {
flex: 1,
...StyleSheet.absoluteFillObject,
backgroundColor: colors.white,
},
metamaskName: {
Expand Down
1 change: 1 addition & 0 deletions app/components/Views/LockScreen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class LockScreen extends PureComponent {
}

logOut = () => {
this.props.navigation.navigate('Login');
this.props.logOut();
};

Expand Down
17 changes: 14 additions & 3 deletions app/components/Views/Login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { strings } from '../../../../locales/i18n';
import SecureKeychain from '../../../core/SecureKeychain';
import FadeOutOverlay from '../../UI/FadeOutOverlay';
import setOnboardingWizardStep from '../../../actions/wizard';
import { logIn, logOut } from '../../../actions/user';
import { logIn, logOut, checkedAuth } from '../../../actions/user';
import { connect } from 'react-redux';
import Device from '../../../util/device';
import { OutlinedTextField } from 'react-native-material-textfield';
Expand Down Expand Up @@ -214,6 +214,10 @@ class Login extends PureComponent {
selectedAddress: PropTypes.string,
logIn: PropTypes.func,
logOut: PropTypes.func,
/**
* TEMPORARY state for animation control on Nav/App/index.js
*/
checkedAuth: PropTypes.func,
};

state = {
Expand Down Expand Up @@ -272,10 +276,15 @@ class Login extends PureComponent {
} catch (e) {
console.warn(e);
}
if (!enabled) {
await this.checkIfRememberMeEnabled();
}
} else {
this.checkIfRememberMeEnabled();
await this.checkIfRememberMeEnabled();
}
}

this.props.checkedAuth();
}

componentWillUnmount() {
Expand All @@ -293,7 +302,6 @@ class Login extends PureComponent {
*/
checkIfRememberMeEnabled = async () => {
const credentials = await SecureKeychain.getGenericPassword();
//This
if (credentials) {
this.setState({ rememberMe: true });
// Restore vault with existing credentials
Expand Down Expand Up @@ -440,6 +448,8 @@ class Login extends PureComponent {
updateBiometryChoice = async (biometryChoice) => {
if (!biometryChoice) {
await AsyncStorage.setItem(BIOMETRY_CHOICE_DISABLED, TRUE);
// This line will disable biometrics the next time SecureKeychain.getGenericPassword is called
await SecureKeychain.resetGenericPassword();
} else {
await AsyncStorage.removeItem(BIOMETRY_CHOICE_DISABLED);
}
Expand Down Expand Up @@ -653,6 +663,7 @@ const mapDispatchToProps = (dispatch) => ({
setOnboardingWizardStep: (step) => dispatch(setOnboardingWizardStep(step)),
logIn: () => dispatch(logIn()),
logOut: () => dispatch(logOut()),
checkedAuth: () => dispatch(checkedAuth()),
});

export default connect(mapStateToProps, mapDispatchToProps)(Login);
1 change: 1 addition & 0 deletions app/components/Views/Onboarding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class Onboarding extends PureComponent {
}

logOut = () => {
this.props.navigation.navigate('Login');
this.props.logOut();
};

Expand Down
6 changes: 6 additions & 0 deletions app/reducers/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ const initialState = {
protectWalletModalVisible: false,
gasEducationCarouselSeen: false,
userLoggedIn: false,
isAuthChecked: false,
};

const userReducer = (state = initialState, action) => {
switch (action.type) {
case 'CHECKED_AUTH':
return {
...state,
isAuthChecked: true,
};
case 'LOGIN':
return {
...state,
Expand Down
12 changes: 11 additions & 1 deletion app/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,22 @@ const persistTransform = createTransform(
{ whitelist: ['engine'] }
);

const persistUserTransform = createTransform(
(inboundState) => {
const { isAuthChecked, ...state } = inboundState;
// Reconstruct data to persist
return state;
},
null,
{ whitelist: ['user'] }
);

const persistConfig = {
key: 'root',
version,
blacklist: ['onboarding'],
storage: MigratedStorage,
transforms: [persistTransform],
transforms: [persistTransform, persistUserTransform],
stateReconciler: autoMergeLevel2, // see "Merge Process" section for details.
migrate: createMigrate(migrations, { debug: false }),
timeout: TIMEOUT,
Expand Down

0 comments on commit b49b535

Please sign in to comment.