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

User profile error #7

Open
javshak opened this issue Mar 29, 2021 · 7 comments
Open

User profile error #7

javshak opened this issue Mar 29, 2021 · 7 comments

Comments

@javshak
Copy link

javshak commented Mar 29, 2021

Hi - I have used the library for a Flutter web app I am building however I get the following error twice (details below).

Error: Exception: The user profile was not loaded as the user is not logged in.
    at Object.throw_ [as throw] (http://localhost:4234/dart_sdk.js:5348:11)
    at keycloak_service.KeycloakService.new.loadUserProfile (http://localhost:4234/packages/keycloak_flutter/src/keycloak_service.dart.lib.js:190:21)
    at loadUserProfile.next (<anonymous>)
    at runBody (http://localhost:4234/dart_sdk.js:39211:34)
    at Object._async [as async] (http://localhost:4234/dart_sdk.js:39242:7)
    at keycloak_service.KeycloakService.new.loadUserProfile (http://localhost:4234/packages/keycloak_flutter/src/keycloak_service.dart.lib.js:185:20)
    at main._MyHomePageState.new.<anonymous> (http://localhost:4234/packages/flutter_app/main.dart.lib.js:613:64)
    at Generator.next (<anonymous>)
    at runBody (http://localhost:4234/dart_sdk.js:39211:34)
    at Object._async [as async] (http://localhost:4234/dart_sdk.js:39242:7)
    at http://localhost:4234/packages/flutter_app/main.dart.lib.js:601:78
    at binding$5.WidgetsFlutterBinding.new.[_invokeFrameCallback] (http://localhost:4234/packages/flutter/src/scheduler/binding.dart.lib.js:776:9)
    at binding$5.WidgetsFlutterBinding.new.handleDrawFrame (http://localhost:4234/packages/flutter/src/scheduler/binding.dart.lib.js:749:37)
    at http://localhost:4234/packages/flutter/src/scheduler/binding.dart.lib.js:653:14
    at internalCallback (http://localhost:4234/dart_sdk.js:25272:11)

I have used the main.dart file from the example you have provided - modified just the credentials. When my page loads I get the above error. Then on clicking the Login button I am redirected to the Keycloak server and after a successful login I am sent back to my app however this error shows up again.
Any advice?

@gibahjoe
Copy link
Owner

Hi, can you provide minimal reproducible code so that I can look into it

@javshak
Copy link
Author

javshak commented Mar 31, 2021

Hi, can you provide minimal reproducible code so that I can look into it

Hey thanks for the reply! Regarding the code I have replicated the code from your 'example' project except for the following changes in the main.dart file ( Provider -> create () ).
No additional changes besides some text labels. I also downloaded your code (example) from github and ran it. I get the same error (posted previously) with no changes made when the main.dart is loaded.

            var keycloakService = KeycloakService(KeycloakConfig(
                url: 'https://ctisaas.com:9091/auth/', // Keycloak auth base url
                realm: 'master',
                clientId: 'jst'));
            keycloakService.keycloakEventsStream.listen((event) {
              if (event.type == KeycloakEventType.onAuthSuccess) {
                  print('the user is authenticated successfully!' );// User is authenticated
              }

I also see this error in the Chrome debug window -
Refused to frame 'https://ctisaas.com:9091/' because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'self'". Not sure why this would be but looks like some issue related to cookies. In the Network tab I can see this -

Request URL: https://ctisaas.com:9091/auth/realms/master/protocol/openid-connect/3p-cookies/step1.html
Request Method: GET
Status Code: 404 
Remote Address: 13.248.163.143:9091
Referrer Policy: strict-origin-when-cross-origin

Not sure why it is trying to navigate the 'step1.html'.

Let me know if this is helpful.

@VenkatraghuB
Copy link

VenkatraghuB commented Apr 1, 2022

HI .....I also have same issue.
Redirection to keycloak works.
After entering credentials .....it is even redirecting to app
but app is not able to read or loaduserprofile

error from browser console

errors.dart:251

   Uncaught (in promise) Error: Exception: The user profile was not loaded as the user is not logged in.
at Object.throw_ [as throw] (:8080/dart_sdk.js:5067:11)
at keycloak_service.KeycloakService.new.loadUserProfile (:8080/packages/keycloak_flutter/src/keycloak_service.dart.lib.js:171:21)
at loadUserProfile.next (<anonymous>)
at runBody (:8080/dart_sdk.js:40590:34)
at Object._async [as async] (:8080/dart_sdk.js:40621:7)
at keycloak_service.KeycloakService.new.loadUserProfile (:8080/packages/keycloak_flutter/src/keycloak_service.dart.lib.js:166:20)
at main._MyHomePageState.new.<anonymous> (:8080/packages/example/main.dart.lib.js:306:64)
at Generator.next (<anonymous>)
at runBody (:8080/dart_sdk.js:40590:34)
at Object._async [as async] (:8080/dart_sdk.js:40621:7)
at :8080/packages/example/main.dart.lib.js:294:78
at binding$5.WidgetsFlutterBinding.new.[_invokeFrameCallback] (:8080/packages/flutter/src/scheduler/binding.dart.lib.js:722:9)
at binding$5.WidgetsFlutterBinding.new.handleDrawFrame (:8080/packages/flutter/src/scheduler/binding.dart.lib.js:698:37)
at :8080/packages/flutter/src/scheduler/binding.dart.lib.js:603:14
at internalCallback (:8080/dart_sdk.js:26619:11)

main.dart ...exactly same as in example ....keycloak settings and prints are modified thats it

import 'dart:html';

import 'package:flutter/material.dart';
import 'package:keycloak_flutter/keycloak_flutter.dart';
import 'package:provider/provider.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider(
create: (_) {
var keycloakService = KeycloakService(KeycloakConfig(
url: 'http://localhost:7070/auth', // Keycloak auth base url
realm: 'master',
clientId: 'bcapp'));

        return keycloakService
          ..init(
            initOptions: KeycloakInitOptions(
                // onLoad: 'check-sso',
                // silentCheckSsoRedirectUri:
                //     '${window.location.origin}/silent-check-sso.html',
                ),
          );
      },
    ),
  ],
  child: MaterialApp(
    title: 'Keycloak Demo',
    debugShowCheckedModeBanner: false,
    theme: ThemeData(
      // This is the theme of your application.
      //
      // Try running your application with "flutter run". You'll see the
      // application has a blue toolbar. Then, without quitting the app, try
      // changing the primarySwatch below to Colors.green and then invoke
      // "hot reload" (press "r" in the console where you ran "flutter run",
      // or simply save your changes to "hot reload" in a Flutter IDE).
      // Notice that the counter didn't reset back to zero; the application
      // is not restarted.
      primarySwatch: Colors.blue,
    ),
    home: MyHomePage(title: 'Flutter Keycloak demo'),
  ),
);

}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String? title;

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

class _MyHomePageState extends State {
KeycloakProfile? _keycloakProfile;
late KeycloakService _keycloakService;

void _login() {
print("About to login");
_keycloakService.login(KeycloakLoginOptions(
redirectUri: '${window.location.origin}',
));

print("login fn is called");

}

@OverRide
void initState() {
// TODO: implement initState
super.initState();
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) async {
print('addPostFrameCallback is called at ' + timeStamp.toString());
_keycloakService.keycloakEventsStream.listen((event) async {
if (event.type == KeycloakEventType.onAuthSuccess) {
print(event.type);
_keycloakProfile = await _keycloakService.loadUserProfile();
} else {
_keycloakProfile = null;
}
setState(() {});
});
_keycloakProfile = await _keycloakService.loadUserProfile(false);
setState(() {});
});
}

@OverRide
Widget build(BuildContext context) {
_keycloakService = Provider.of(context);
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title!),
actions: [
IconButton(
icon: Icon(Icons.logout),
onPressed: () async {
await _keycloakService.logout();
}),
],
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Welcome ${_keycloakProfile?.username ?? 'Guest'}',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _login,
tooltip: 'Login',
child: Icon(Icons.login),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

@VenkatraghuB
Copy link

I think I found my problem .....issue is in keycloak client settings. I changed my client access type to public then it worked . This issue pops up when client access type is confidential.

@ms0713
Copy link

ms0713 commented Apr 11, 2022

Hi @VenkatraghuB
I have used the same code and I am having having the issue as you mentioned before.

Keycloak Version 16.1.1
Flutter Version 2.10.3

Here is my error:

Uncaught (in promise) Error: Exception: The user profile was not loaded as the user is not logged in.
at Object.throw_ [as throw] (errors.dart:251:49)
at keycloak_service.KeycloakService.new.loadUserProfile (keycloak_service.dart:87:7)
at loadUserProfile.next ()
at runBody (async_patch.dart:84:54)
at Object._async [as async] (async_patch.dart:123:5)
at keycloak_service.KeycloakService.new.loadUserProfile (keycloak_service.dart:81:45)
at main._MyHomePageState.new. (main.dart:97:49)
at Generator.next ()
at runBody (async_patch.dart:84:54)
at Object._async [as async] (async_patch.dart:123:5)
at main.dart:88:52
at binding$5.WidgetsFlutterBinding.new.[_invokeFrameCallback] (binding.dart:1144:15)
at binding$5.WidgetsFlutterBinding.new.handleDrawFrame (binding.dart:1089:9)
at binding.dart:862:7
at internalCallback (isolate_helper.dart:48:19)

I have checked with AccessType as "public" also but no success.

Here is my Keycloak settings.
KeyCloakSettings

What am I missing here?

@rsiva229
Copy link

rsiva229 commented May 5, 2022

Hi,
Is there an update to the above mentioned issues? Even I'm facing problems while trying to loadUserProfile. If anyone can kindly post their solution here, that would be helpful. Thank you.

@VenkatraghuB
Copy link

KeycloakSettings

This issue is mainly because of client settings in keycloak. Look out for following

  1. AccessType shld be public
  2. Redirect URIs shld be defined
  3. WebOrigins: *
    That should do the trick

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

5 participants