Clancey / simple_auth

The Simplest way to Authenticate in Flutter
MIT License
352 stars 108 forks source link

How redirect to another page after user already logged? #146

Closed Kiraneki closed 4 years ago

Kiraneki commented 4 years ago

I have a problem with a redirection on main first screen app: i already tried use loadAccountFromCache() method, but it return a null argument. I'm trying use a cached memory bool variable to control if user as logged in first time. If this is true, choose to make a new login taking all basic profile data and redirect to a Main Page. But, when i use this criteria, the login method return a Exception: You are required to implement showAuthenticator or sharedShowAuthenticator. Being that this same login method work fine on Login Screen. In this case i'm using Azure Authentication Api module. Below is described the method about user login verification, in which these return a Future widget to be choose on main file:

Future<Widget> verifyUser() async {
    //verify if has internet connection
    bool testConnection = await NoConnectionPage.verifyConnection();

    if (!testConnection) {
      //return a no connection widget in offline case
      return NoConnectionPage();
    } else {
      //take azure api variables (clientID, clientSecret)
      _azure = await _fb.azureVariables();

      /* _userAzure = await AuthConfig.loadAccount(
          AuthConfig.azureConn(_azure['clientID'], _azure['clientSecret']));
      print(_userAzure.toString());*/

      SharedPreferences prefs = await SharedPreferences.getInstance();

      if (prefs.getBool("firstLogin") == true) {
        _userAzure = await AuthConfig.login(
            AuthConfig.azureConn(_azure["clientID"], _azure["clientSecret"]));

        return MainPage(userAzure: _userAzure);
      }
      return LoginScreen();
    }
  }

*Note that comment piece was a tried account cached load call after a first login.

And finally, this is the login Authenticator and the loadAccount cached methods:

 static login(simpleAuth.AuthenticatedApi api) async {
    try {
      //connect api
      var success = await api.authenticate();

      //get Account map from api authentication
      return api.getAccountFromMap(success.toJson());
    } catch (e) {
      return e.toString();
    }
  }

static Future<simpleAuth.Account> loadAccount(
      simpleAuth.AuthenticatedApi api) async {
    return await api.loadAccountFromCache();
  }
Kiraneki commented 4 years ago

I got the solution here, and was a minimal error that i found. Sorry for the inconvenience.