Flutter save credentials

Hi guys,

I’m creating a flutter app, do anyone know how to save the credentials? The quickstart does not show how to do this (unlike the iOS Swift Quickstart), and I could not find anything on the docs.

This is my app so far:

Credentials? _credentials;
...
await auth0.webAuthentication(scheme: 'demo').login();
setState(() {
   _credentials = credentials;
});

I think all I have to do is store the data and validate the credentials, but I’m not sure how to do that. Does anyone have any idea how to?

Hey @jakkipally !

Good question - After logging in you get a Credentials instance which contains an access token and an ID token (which in turn contains the user profile data, that is exposed through the user property as well). That is, the SDK should handle storing the credentials for you.

Thanks @tyf. However, I don’t think that the credentials are being saved. I’ll double check again, but last time, I had to after leaving my app, and when the popup appears, I don’t have to sign in and it redirects to logged in page.

@tyf Just for some clarification, if I use this code to show login or show the logged in page:

print(_credentials);
if (_credentials == null) {
// logged out
} else {
//logged in
}

I’m not sure if this is the problem because I’m doing something wrong, but I get this log:

Instance of 'Credentials' is when I log in and it successfully logs my credentials

Then when I exit the tab and re-enter it, I get null, showing that when I initialize:

Credentials? _credentials;

It does not store the credentials when I do:

setState(() {
   _credentials = credentials;
});


What I think is that I’m probably saving the credentials but not using the credentialsManager hasValidCredentials. I’ll look into the credential manager api and try and figure it out.

Hey guys, I figured it out myself!

  Credentials? _credentials;
  ...
  bool? isLoggedIn;
  

  @override
  void initState() {
    super.initState();
    ...
    _getData();
  }

  void _getData() async {
    isLoggedIn = await auth0.credentialsManager.hasValidCredentials();
    _credentials = await auth0.credentialsManager.credentials();
    setState(() {});
  }

And then in the widget build:

if (isLoggedIn == null) {
   if (_credentials == null) {
      // needs to login
   } else {
      // loading credentials
   }
} else {
   if (_credentials == null) {
      // logged in with credentials
   } else {
      // loading
   }
}
1 Like

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