How to stay login after user kill a task?

A common piece of functionality for native mobile applications is the ability to stay logged in even if the user closes the application in question (see e.g. the Facebook app on iOS).

How can this be achieved for Ionic Capacitor React??

I’m saving the information on the local-storage as follows and wrap the whole application with context provider.

useEffect(() => {
    const getToken = async () => {
      try {
        console.log('user TOKEN', user);
        if (isLoading) {
          return;
        }
        const userAuthDetail = await getIdTokenClaims();
        const userAccessToken = await getAccessTokenSilently({
          redirect_uri: callbackUri,
        });
        console.log('TOKEN', userAccessToken);
        if (userAccessToken) {
          setToken(userAccessToken);
          localStorage.setItem('accesstoken', userAccessToken);
        }
        console.log('userAuthDetail', userAuthDetail);
        if (userAuthDetail) {
          setUser(userAuthDetail);
          localStorage.setItem('userAuth', JSON.stringify(userAuthDetail));
        }
      } catch (error) {
        console.log('getToken', error);
      }
    };
    getToken();
  }, [getAccessTokenSilently, getIdTokenClaims, isLoading]);

Current behavior:
User is redirected to browser for login, then comes back to the application.
User kill a task and re-login is needed.

Expected behavior:
keep user logged in even when user force quit the app.

You need a method to be ran when app first starts that checks to see if the auth token is still active. If so, redirect to whichever screen.

2 Likes