1

I have an app that has a LandingScreen as the initial route. If a user wants to sign-in, they have to navigate from LandingScreen to SignInScreen. If a user presses the sign-in button I set isAuthenticating to true which shows a loading view. The problem is that if authentication fails, e.g when the user enters the wrong email or password it goes back to the LandingScreen instead of the SignInScreen after loading.

Authentication Stack:

const CreateAuthStack = () => (
  <AuthStack.Navigator
    screenOptions={{
      headerStyle: {
        backgroundColor: '#3c74db',
      },
      headerTintColor: '#fff',
      headerTitleStyle: {
        fontWeight: 'bold',
      },
    }}>
    <AuthStack.Screen
      name="Landing"
      component={LandingScreen}
      options={{headerShown: false}}
    />
    <AuthStack.Screen
      name="SignIn"
      component={SignInScreen}
      options={{title: 'Sign In'}}
    />
    <AuthStack.Screen name="Register" component={RegisterScreen} />
  </AuthStack.Navigator>
);

When isAuthenticating is true:

 if (isAuthenticating) {
    return (
      <Container
        style={{
          flex: 1.5,
          alignItems: 'center',
          justifyContent: 'center',
          backgroundColor: '#3c74db',
        }}>
        <ActivityIndicator size="large" color="#fff" />
      </Container>
    );
  }

Sign in function:

const signIn = (email, password) => {
  setAuthenticating(true);
  axios
    .post(url, data, headers)
    .then(function(response) {
      if (response.status === 200) {
        setAuthenticating(false);
        setUser(email);
        AsyncStorage.setItem('user', email);
      }
    })
    .catch(function(error) {
      if (error.response.status === 404) {
        setAuthenticating(false); // Goes to Landing page instead of staying on signin page
        alert('Email is incorrect');
      } else if (error.response.status === 401) {
        setAuthenticating(false); // Goes to Landing page instead of staying on signin page
        alert('Password is incorrect');
      }
    });
};
4

0 回答 0