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

Bug/3464 fix login bug #3467

Merged
merged 8 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
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 @@ -565,6 +565,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
11 changes: 9 additions & 2 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 @@ -273,9 +277,11 @@ class Login extends PureComponent {
console.warn(e);
}
} else {
this.checkIfRememberMeEnabled();
await this.checkIfRememberMeEnabled();
}
}

this.props.checkedAuth();
}

componentWillUnmount() {
Expand Down Expand Up @@ -653,6 +659,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