2
const AuthLoadingScreen = ({ navigation }) =>  {

  // auth init function
  const _bootstrapAsync = async () => {

    // Fetch token from storage
    const session = await AsyncStorage.getItem('@todo-graphql:session');
    // If session exists, validate it, else redirect to login screen
    if (session) {
      const sessionObj = JSON.parse(session);
      var currentTime = Math.floor(new Date().getTime() / 1000);
      if (currentTime < sessionObj.exp) {
        setLogout(() => navigation.navigate('Auth'));
        navigation.navigate('Main');

      } else {
        console.log("expired")
        navigation.navigate('Auth');
      }
    } else {
      navigation.navigate('Auth');
      
    }
  };

  React.useEffect(() => {
    console.log("inside Loading screen useeffect")
    _bootstrapAsync();
  }, []);

  return (
    <View>
      <CenterSpinner />
    </View>
  );
}

export default AuthLoadingScreen;

I want to navigate to the main screen after checking the asyncstorage for the access token. If the access token is not expired. If the token is expired or no token exists the Auth screen will be shown. However, when I log in using the correct access token I am not navigated instead I have to refresh the expo go app and then it works.

1 Answers1

0

You have to use a state to re-render the component. Define a state like

const [loggedIn,setLogged] = useState(false);

Pass it as a dependency in useEffect so that the screen gets re-render. To make it more efficient use redux state.

abdemirza
  • 629
  • 4
  • 16