Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise was rejected with a value of undefined. #18

Open
SunlightBro opened this issue Sep 6, 2022 · 14 comments
Open

Promise was rejected with a value of undefined. #18

SunlightBro opened this issue Sep 6, 2022 · 14 comments

Comments

@SunlightBro
Copy link

I'm trying to use the example app to evaluate how to use this package.

And calling KeycloakService.init() throws an exception;

💪 Running with sound null safety 💪
Debug service listening on ws://127.0.0.1:60412/S8JStBjs-OQ=/ws
Flutter Web Bootstrap: Programmatic
BEFORE INIT
Error: Promise was rejected with a value of `undefined`.
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 299:10  createErrorWithStack
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 341:28            _throw
dart-sdk/lib/core/errors.dart 116:5                                           throwWithStackTrace
dart-sdk/lib/async/zone.dart 1378:11                                          callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                              _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                               _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 166:15           <fn>

I tried this with 0.0.3 and 0.0.19 of this package, on flutter 3.3.0 (but also with flutter 3.0.5 and 2.10.5)

flutter doctor -v
[✓] Flutter (Channel stable, 3.3.0, on macOS 12.5.1 21G83 darwin-arm, locale en-DE)
  • Flutter version 3.3.0 on channel stable at /Users/sunbro/flutter
  • Upstream repository https://github.com/flutter/flutter.git
  • Framework revision ffccd96b62 (8 days ago), 2022-08-29 17:28:57 -0700
  • Engine revision 5e9e0e0aa8
  • Dart version 2.18.0
  • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc1)
  • Android SDK at /Users/sunbro/Library/Android/sdk
  • Platform android-33, build-tools 33.0.0-rc1
  • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
  • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
  • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
  • Xcode at /Applications/Xcode.app/Contents/Developer
  • Build 13F100
  • CocoaPods version 1.11.3

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

[✓] Android Studio (version 2021.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 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2022.2)
  • IntelliJ at /Applications/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

[✓] Connected device (3 available)
  • ONEPLUS A6003 (mobile) • b0a181c6 • android-arm64  • Android 11 (API 30)
  • macOS (desktop)        • macos    • darwin-arm64   • macOS 12.5.1 21G83 darwin-arm
  • Chrome (web)           • chrome   • web-javascript • Google Chrome 104.0.5112.101

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

• No issues found!
Adapted Example App
import 'dart:html';

import 'package:flutter/material.dart';
import 'package:keycloak_flutter/keycloak_flutter.dart';
import 'package:flutter_web_plugins/url_strategy.dart';

final KeycloakService keycloakService = KeycloakService(
  KeycloakConfig(
    url: 'https://***/auth/',
    realm: '***',
    clientId: '***',
  ),
);

void main() {
  // need to make sure it works without /#/
  usePathUrlStrategy();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  KeycloakProfile? _keycloakProfile;

  void _login() {
    keycloakService.login(KeycloakLoginOptions(
      redirectUri: '${window.location.origin}',
    ));
  }

  void _logout() {
    keycloakService.logout();
  }

  void _init() async {
    final options = KeycloakInitOptions(
      flow: 'implicit',
      //without out check-sso init works
      onLoad: 'check-sso',
      silentCheckSsoRedirectUri:
          '${window.location.origin}/silent-check-sso.html',
    );
    print('BEFORE INIT');
    final authed = await keycloakService.init(initOptions: options);
    // this never prints
    print('AFTER INIT: $authed');

    final profile = await keycloakService.loadUserProfile();
    setState(() => _keycloakProfile = profile);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            icon: Icon(Icons.downloading),
            onPressed: _init,
          ),
          IconButton(
            icon: Icon(Icons.login),
            onPressed: _login,
          ),
          IconButton(
            icon: Icon(Icons.logout),
            onPressed: _logout,
          )
        ],
      ),
      body: Center(
        child: Text(
          'Welcome ${_keycloakProfile?.username ?? 'Guest'}',
          style: Theme.of(context).textTheme.headline4,
        ),
      ),
    );
  }
}

flutter create --platform web . creates the index.html differently then in the past, so I'll post it as well

index.html
<!DOCTYPE html>
<html>
<head>
  <base href="$FLUTTER_BASE_HREF">

  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="A new Flutter project.">

  <!-- iOS meta tags & icons -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="example">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <!-- Favicon -->
  <link rel="icon" type="image/png" href="favicon.png"/>

  <title>example</title>
  <link rel="manifest" href="manifest.json">
  <script src="js/keycloak.js"></script>
  <!-- tested with defer or asnyc-->
  <!-- Also tested with https://test-webservice.motornet.it/auth/js/keycloak.js -->
  <script>
    // The value below is injected by flutter build, do not touch.
    var serviceWorkerVersion = null;
  </script>
  <!-- This script adds the flutter initialization JS code -->
  <script src="flutter.js" defer></script>
</head>
<body>
  <script>
    window.addEventListener('load', function(ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        }
      }).then(function(engineInitializer) {
        return engineInitializer.initializeEngine();
      }).then(function(appRunner) {
        return appRunner.runApp();
      });
    });
  </script>
</body>
</html>
@WilkoThomassen
Copy link

I'm facing the same issue

flutter doctor -v

[✓] Flutter (Channel stable, 3.3.8, on macOS 13.0 22A380 darwin-arm, locale en-NL)
• Flutter version 3.3.8 on channel stable at /Users/wilkothomassen/Development/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 52b3dc25f6 (4 weeks ago), 2022-11-09 12:09:26 +0800
• Engine revision 857bd6b74c
• Dart version 2.18.4
• DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
• Android SDK at /Users/wilkothomassen/Library/Android/sdk
• Platform android-33, build-tools 33.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
• All Android licenses accepted.

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

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

[✓] Android Studio (version 2021.3)
• 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 11.0.13+0-b1751.21-8125866)

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

[✓] Connected device (2 available)
• macOS (desktop) • macos • darwin-arm64 • macOS 13.0 22A380 darwin-arm
• Chrome (web) • chrome • web-javascript • Google Chrome 108.0.5359.94

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

@vsranjitroshan
Copy link

I am facing the same issue, can you please help.

@Quantentunnel
Copy link

When I run the example, I get the same error. I use Flutter 3.7.3.
Can you please help?

@gibahjoe
Copy link
Owner

Hi, what version of keycloak do you use?

@sachintha-s
Copy link

sachintha-s commented Apr 23, 2023

I am facing the same issue, does anyone know how to fix this? (flutter V: 3.7.12)

@gibahjoe
Copy link
Owner

What version of the library are you using?

@Bill-ON-epitech
Copy link

Hello, I have the same issue only if i use onLoad: "check-sso" in the KeycloakService Initialization. It works fine with "login-required".
Keycloak v17.0.1
Flutter v3.7.12

@gibahjoe
Copy link
Owner

gibahjoe commented May 10, 2023

What version of the plug-in are you using? Also, have you tested with the example app?

@Rajkuwar-Solace
Copy link

Rajkuwar-Solace commented May 30, 2023

i am facing issue when implementing keycloak getting error js_utils not imported and so on .
i am using 3.7.12 version
image

@Bill-ON-epitech
Copy link

What version of the plug-in are you using? Also, have you tested with the example app?

Sorry for the late response,
I got this error while testing with the example app with plugin version v0.0.20
With the following main :

void main() async {
  keycloakService = KeycloakService(KeycloakConfig(
      url: <KEYCLOAK_URL>, // Keycloak auth base url
      realm: <REALM_NAME>',
      clientId: <CLIENT_NAME>));
  await keycloakService.init(
    initOptions: KeycloakInitOptions(
      onLoad: 'login-required',
      redirectUri: '${window.location.origin}',
      silentCheckSsoRedirectUri:
          '${window.location.origin}/silent-check-sso.html',
    ),
  );
  runApp(MyApp());
}
  • If I try to use the "login-required" mode, the keycloak login window is correcty displayed. But after I enter my credentials, I get the following error :
Error: NoSuchMethodError: ''
Dynamic call with too few arguments. Expected: 1 Actual: 0
Receiver: Instance of '(KeycloakError) => Null'
Arguments: []
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 288:49      throw_
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 813:3   defaultNoSuchMethod
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 64:17                 noSuchMethod
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 808:31  noSuchMethod
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 283:12  callNSM
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 369:10  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 372:39  dcall
js/keycloak.js 1758:50

This error seems to be from keycloak_service.dart, line 29 :

authed = await promiseToFuture<bool>(_keycloak.init(initOptions));
  • If I use the "check-sso" mode, I get the error :
Error: Promise was rejected with a value of `undefined`.


dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 321:10  createErrorWithStack
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 337:28            _throw
dart-sdk/lib/core/errors.dart 120:5                                           throwWithStackTrace
dart-sdk/lib/async/zone.dart 1386:11                                          callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                              _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                               _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 177:15           <fn>

Even with the parameter :

flow: 'implicit',

@gibahjoe
Copy link
Owner

gibahjoe commented Jun 9, 2023

Please what command do you use to build the application? Are you building with canvaskit? I cant seem to replicate this issue

@Bill-ON-epitech
Copy link

Bill-ON-epitech commented Jun 12, 2023

@gibahjoe
I'm usually building with html, and i also tried with Canvaskit to no avail. I recently switched from Keycloak v17 to v21 but I still encounter the same issues.
Below are the commands I use to build the example application :

git clone [email protected]:gibahjoe/keycloak_flutter.git
cd keycloak_flutter
cd example
flutter pub get
code . 
flutter run -d chrome --web-port=8090 ./lib/main.dart --web-renderer html

When I launch VS Code with the code . command, I only modilfy URL, Realm and ClientId
I launch flutter on port 8090 because I assigned "http://localhost:8090" as one of my client's redirect URIs.
In case I forgot to mention it, I'm using Ubuntu 22.04 and the example App is launched on Google Chrome.

@gibahjoe
Copy link
Owner

@Bill-ON

Can you please share the keycloak.js file you are using

Also, please share your keycloak clients' .json config file so that I can check if the issue is a client config issue. thank you.

@gibahjoe
Copy link
Owner

@Bill-ON

Hi, I managed to track down this issue and resolve it.

I recreated it when the web origins config was not set in the client. Please check that that is your case

I have updated the documentation to reflect this and also included a sample keycloak client in the example app. If you are still having the issue, kindly import the sample client and test with that.

I have also pushed an update to the library (v0.0.21). Please test it and let me know.

thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants