React Native does not persist login / sessions after app restart

I am following the Task Tracker tutorial and I am not able to persist the login session. I am able to register and login, however, everytime I close the app and open it again, it redirects me to the login screen.

In addition, I use AuthProvider to read user, I get “null”.

Anyone have any ideas? Do I have to write custom logic to persist login information / session tokens?

Thanks

Found the problem by looking into the library.

“user” is a run-time variable only set upon calling the login function, while “currentUser” is persisted. To fix the problem, we have to look at the currentUser object instead of the User object.

In my case, I added a currentUser variable to the AuthProvider that is set upon the initialization of the app.

const [currentUser, setCurrentUser] = useState(null);
React.useEffect(()=>{
      setCurrentUser(app.currentUser)
    }, [])

The currentUser is now exposed to other aspects of the app through the AuthProvider.

const {currentUser} = useAuth();
...
{currentUser == null ? (
            <LogInView />
          ) : (
            <TasksProvider projectId="My Project">
              <TasksView />
            </TasksProvider>
          )}

Irfan,

I’m glad you “self solved” this issue. I will speak with the docs team to make sure the currentUser variable is highlighted as the persistent variable in the docs.

Thanks for highlighting

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.