auth0 / auth0-flutter

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

Credentials doesn't return after login #291

Closed plato79 closed 1 year ago

plato79 commented 1 year ago

Checklist

Description

I prepared a provider class which should return credentials after login. Here is the full code of that class:

import 'package:auth0_flutter/auth0_flutter.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';

class Auth with ChangeNotifier {
  Credentials? _credentials;
  late Auth0 auth0;

  Auth() {
    init();
  }

  get credentials => _credentials;

  void init() {
    auth0 = Auth0(dotenv.get('AUTH0_DOMAIN'), dotenv.get('AUTH0_CLIENT_ID'));
  }

  void login() {
    auth0
        .webAuthentication(scheme: dotenv.get('AUTH0_CUSTOM_SCHEME'))
        .login()
        .then((credentials) {
      if (kDebugMode) {
        print(credentials);
      }
      _credentials = credentials;
      notifyListeners();
    }).onError((error, stackTrace) {
      if (kDebugMode) {
        print(error);
      }
    });
    // try {
    //   final credentials = await auth0
    //       .webAuthentication(scheme: dotenv.get('AUTH0_CUSTOM_SCHEME'))
    //       .login();
    //   if (kDebugMode) {
    //     print(credentials);
    //   }
    //   _credentials = credentials;
    // } catch (e) {
    // } finally {
    //   notifyListeners();
    // }
  }

  void logout() async {
    await auth0
        .webAuthentication(scheme: dotenv.get('AUTH0_CUSTOM_SCHEME'))
        .logout();
    _credentials = null;
    notifyListeners();
  }
}

I'm using the credentials in another page with listening to the provider. Although, after login the command below doesn't work at all.. It's like it doesn't return a future or stops processing there and then. I tried with await / async and .then().onerror() but to no avail.

It does successfully login to auth0, but the credentials remain a mystery. Because I check the credentials when I click the login button it tries to login every time ( it should logout when I click that same button ).

Reproduction

  1. use the above class in the code like this in the main class:
    @override
    Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<Auth>(
          create: (_) => Auth(),
        ),
  2. call it like this on a button:
    onTap: () {
          final auth = context.read<Auth>();
          if (auth.credentials != null) {
            auth.logout();
            return;
          }
          auth.login();
        },
  3. Try to access credentials somewhere like this:
    '${context.watch<Auth>().credentials?.user.name ?? 'Guest'}'

Additional context

No response

auth0_flutter version

1.2.1

Flutter version

3.10.5

Platform

Android

Platform version(s)

33

Widcket commented 1 year ago

Hi @plato79, thanks for raising this.

Can you please confirm you can reproduce this issue with the sample app?

plato79 commented 1 year ago

Definitely getting the same result. Sample app is logging in to the system, but setState doesn't run. Because of that user stays null and the button on the screen doesn't change to "Logout"..

poovamraj commented 1 year ago

@plato79 can you share this repository so that we can reproduce this. Please remove all sensitive information before you do (especially your domain and client id)

KingUgochukwu commented 1 year ago

I also have this issue on Android using the sample app. I also had some gradle issues running the Android app and had to make some additional changes to the sample app on top of the expected changes to configure the package for my auth0 app.

in build.gradle

Screenshot 2023-07-27 at 2 34 03 PM

this was deprecated so i did this but I am not sure what adverse effects this could have.

Screenshot 2023-07-27 at 2 34 43 PM

added the deeplink to the manifest main/androidManifest.xml

Screenshot 2023-07-27 at 2 38 28 PM

Below is a video of the observed behaviour

https://github.com/auth0/auth0-flutter/assets/38926915/3cf7fc3d-442b-4803-af09-4ff6c953b9a5

flutter doctor [✓] Flutter (Channel stable, 3.7.12, on macOS 13.3.1 22E772610a darwin-arm64, locale en-US) • Flutter version 3.7.12 on channel stable at /Users/victoranyanwu/fvm/versions/3.7.12 • Upstream repository https://github.com/flutter/flutter.git • Framework revision 4d9e56e694 (3 months ago), 2023-04-17 21:47:46 -0400 • Engine revision 1a65d409c7 • Dart version 2.19.6 • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2) • Android SDK at /Users/victoranyanwu/Library/Android/sdk • Platform android-33, build-tools 33.0.2 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14C18 • CocoaPods version 1.12.1

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] IntelliJ IDEA Community Edition (version 2023.1.1) • IntelliJ at /Users/victoranyanwu/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/231.8770.65/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.78.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.68.0

[✓] Connected device (4 available) • SM S911U1 (mobile) • R3CW20LFBGA • android-arm64 • Android 13 (API 33) • iPhone 14 Pro Max (mobile) • 8AA12639-3977-4242-836F-9437C3BE94C9 • ios • com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 13.3.1 22E772610a darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 112.0.5615.137

[✓] HTTP Host Availability • All required HTTP hosts are available

• No issues found!

poovamraj commented 1 year ago

@KingUgochukwu Any idea why you had to add the deeplink to the manifest main/androidManifest.xml? We auto generate this.

You just have to update the strings.xml as mentioned here. Can you check this and let me know?

Widcket commented 1 year ago

Closing due to inactivity. Please feel free to ping if you'd prefer to reopen.

aluu4445 commented 9 months ago

@Widcket @poovamraj I'm seeing this issue as well. I use a custom scheme as well.

I tried updating strings.xml but still wasn't working - where are the intent filters generated?