nhost / nhost-dart

Nhost Dart & Flutter packages

Home Page:https://nhost.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need keep login status even when APP reopen

bigzhu opened this issue · comments

When APP reopen, always need login, it's so silly.

You can easily do that by managing the auth status with the Nhost Auth callback to update your app state.

<Your nhost client>.auth
        .addAuthStateChangedCallback((authenticationState) {
      log('AUTH STATUS CHANGED: ${authenticationState.name}');
      _updateLoginStatus();
    });

it takes less than a second for the callback to execute. You can wait for it during a splash screen.

You can use the .signInWithStoredCredentials() method on the initial load to ensure the user can log in. Please make sure to catch an error, as this method throw an exception if a silent log in is not possible.

nhostClient.auth
        .signInWithStoredCredentials()
        .then((value) {print('user is signed in $value'})
        .catchError(print);