auth0 / auth0-flutter

Auth0 SDK for Flutter
https://pub.dev/documentation/auth0_flutter/latest/
Apache License 2.0
61 stars 43 forks source link

Credentials are not returned when trying to login, app gets stuck #380

Open aakashawalkar opened 11 months ago

aakashawalkar commented 11 months ago

Checklist

Description

After logging in with auth0, the credentials cannot be retrieved on Android or the Web (Works for iOS). The warning message, No stored state: unable to handle response appears on the console when the webpage gets stuck and you have to kill the app and relaunch the app to try logging in again.

Initially, I was using the flutter_appauth package for logging in using Auth0 and everything was working fine. Now, I am trying to migrate it to the official Auth0 package i.e. auth0_flutter and it is now working for Android and Web.

Reproduction

  1. Login function below:
Future<Credentials?> login() async {
    try {
      if (kIsWeb) {
        /// Attempt to login the user with the redirect method
          await _auth0Web.loginWithRedirect(
              audience: auth0Audience,
              scopes: authScopes['auth'],
              redirectUrl: auth0RedirectUri);

         return await _auth0Web.credentials();
      } else {
        /// Handle the needed credentials for mobile
        Credentials? credentials = await _auth0
            .webAuthentication(scheme: 'com.project.portal')
            .login(
              audience: auth0Audience,
              scopes: const {'openid', 'profile', 'email', 'offline_access'},
              redirectUrl: auth0RedirectUri,
              idTokenValidationConfig: IdTokenValidationConfig(issuer: 'https://$auth0Domain'),
              safariViewController: const SafariViewController(),
              parameters: {'prompt': 'login'},
            );

        return credentials;
      }
    } on Exception catch (e) {
      print('Error logging in: $e');
      return null;
    }
  }
  1. Call above login function on a button click:
ElevatedButton(
  onPressed: () async {
    final credentials = await login();

    setState(() {
      _credentials = credentials;
    });
  },
  child: const Text('Login'),
),

Additional context

On Auth0, I have first created a new Native Application. I have created a flutter application that supports iOS, Android, and the Web which uses the same configuration from Auth0. The auth0 domain is a custom domain

Flutter Doctor Output ```console Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.13.9, on macOS 14.1.1 23B81 darwin-x64, locale en-IN) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc2) [✓] Xcode - develop for iOS and macOS (Xcode 15.0.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2022.3) [✓] VS Code (version 1.85.0) [✓] Connected device (3 available) [✓] Network resources • No issues found! ```

auth0_flutter version

1.3.1

Flutter version

3.13.9

Platform

Android, Web

Platform version(s)

No response

Widcket commented 11 months ago

Hi @aakashawalkar, thanks for raising this.

Can you confirm that you can indeed reproduce this on the auth0_flutter sample app?

aakashawalkar commented 11 months ago

@Widcket - Yes, I see the same behavior on the sample app.

Widcket commented 11 months ago

Thanks. @poovamraj could you please take a look?

aakashawalkar commented 11 months ago

Hi @Widcket @poovamraj -

When I tried tweaking the Auth0 configuration, most of the time I saw the below error message

No stored state - unable to handle the response

Also, sometimes, I see below error messages while logging in -

E/flutter (26530): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: a0.sdk.internal_error.unknown: Received error with code a0.sdk.internal_error.unknown
E/flutter (26530): #0      MethodChannelAuth0FlutterWebAuth.invokeRequest (package:auth0_flutter_platform_interface/src/method_channel_auth0_flutter_web_auth.dart:45:7)
E/flutter (26530): <asynchronous suspension>
E/flutter (26530): #1      MethodChannelAuth0FlutterWebAuth.login (package:auth0_flutter_platform_interface/src/method_channel_auth0_flutter_web_auth.dart:21:9)
E/flutter (26530): <asynchronous suspension>
E/flutter (26530): #2      WebAuthentication.login (package:auth0_flutter/src/mobile/web_authentication.dart:82:25)
E/flutter (26530): <asynchronous suspension>
sarbogast commented 2 months ago

Has any solution been found for this issue? I am in the same situation. Trying to migrate away from flutter_appauth, and it works in iOS but not in Android.

pankajpreet commented 2 months ago

Any workarounds to fix this?