returning different flutter widget depending on whether user is logged in or not

28 Views Asked by At

I am new to Flutter and would like to return the login screen or the home screen depending on whether the user is logged in or not. I am using auth0 for authentication and provider as the app state management solution.

I tried using context.watch but could not make it work.

1

There are 1 best solutions below

1
079_Vikash Rajpurohit On

You can check if the user is logged in or not, general implementation for this flow is storing the token in persistent storage but in your case AuthO provides data/persistent token by itself in Credentials? _credentials;

Here is the implementation

//Declare the variables
Credentials? _credentials;
  late Auth0 auth0;
  
  ...
  
// Initialize them
  void initState() {
    super.initState();
    auth0 = Auth0('{domain}', '{clientId}');
    errorMessage = '';
  }

  ...

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Auth0 Demo',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Auth0 Demo'),
        ),
        body: Center(
          child: isBusy
              ? const CircularProgressIndicator()
              : _credentials != null
                  ? Profile(logoutAction, _credentials?.user)
                  : Login(loginAction, errorMessage),
        ),
      ),
    );
  }