Реактивная навигация отправляет пользователя на начальный маршрут вместо входа в систему при сбое аутентификации - PullRequest
1 голос
/ 05 марта 2020

У меня есть приложение, которое имеет LandingScreen в качестве начального маршрута. Если пользователь хочет войти в систему, он должен перейти от LandingScreen к SignInScreen. Если пользователь нажимает кнопку входа, я устанавливаю isAuthenticating в значение true, что показывает вид загрузки. Проблема заключается в том, что в случае сбоя аутентификации, например, когда пользователь вводит неправильный адрес электронной почты или пароль, после загрузки он возвращается к LandingScreen вместо SignInScreen.

Стек аутентификации:

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>
);

Когда истинно isAuthenticating:

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

Функция входа в систему:

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');
      }
    });
};
...