Giter Club home page Giter Club logo

Comments (10)

mauriciovigolo avatar mauriciovigolo commented on September 4, 2024 1

@abhishekkaura and @angular101

I've been working on a new example to show how to handle the keycloak events and I started to build it using angular 6, so I came across a bigger issue, regarding the angular versions. I expect to finish all the migration to angular 6 this weekend and also release the examples.

from keycloak-angular.

mauriciovigolo avatar mauriciovigolo commented on September 4, 2024

Hi @angular101,

First of all you need to check your keycloak initialization. From the keycloak javascript adapter docs

By default, the JavaScript adapter creates a hidden iframe that is used to detect if a Single-Sign Out has occurred. This does not require any network traffic, instead the status is retrieved by looking at a special status cookie. This feature can be disabled by setting checkLoginIframe: false in the options passed to the init method.
You should not rely on looking at this cookie directly. It’s format can change and it’s also associated with the URL of the Keycloak server, not your application.

So, you cannot set the checkLoginIframe to false.

Regarding the way to invoke a function for the events, you could do something like this:

ngOnInit() {
    ....

    const keycloakInstance = this.keycloakService.getKeycloakInstance();
    keycloakInstance.onAuthLogout = () => console.log('logout');
  }

This code is just an example, use it inside the best method or function in your project.

from keycloak-angular.

angular101 avatar angular101 commented on September 4, 2024

Hi @mauriciovigolo ,
Thanks for responding and sorry for my late response.

I checked and I have checkLoginIframe set to false.

onAuthLogout works fine - I still cannot get onAuthSuccess to fire?

I register the onAuthSuccess callback after keycloak.init() which is maybe too late, is it?
but I cannot set it up before keycloak.init() as keycloak instance is undefined.
You see what I mean?

from keycloak-angular.

angular101 avatar angular101 commented on September 4, 2024

Hi @mauriciovigolo ,
As an example I've set onAuthSuccess in 2 locations below in the keycloakService init() method:

The first will be called - the second won't (if you uncomment it)

I presume first one is called because it's set before _this.instance.init() is called.

The second is after _this.instance.init() and seems to be too late?

Given the current keycloakService implementation I can only get the keycloak instance after this, so how can I set onAuthSuccess so it will be called? I must be missing something?
Thanks.

 KeycloakService.prototype.init = function (options) {
        var _this = this;
        if (options === void 0) { options = {}; }
        return new Promise(function (resolve, reject) {
            _this.bearerExcludedUrls = options.bearerExcludedUrls || [];
            _this.instance = Keycloak(options.config);
            _this.instance.onAuthSuccess = () => { console.log('************ LOGIN')};
            _this.instance
                .init(options.initOptions)
                .success(function (authenticated) {
                  // _this.instance.onAuthSuccess = () => { console.log('************ LOGIN')};
                  return __awaiter(_this, void 0, void 0, function () {

from keycloak-angular.

angular101 avatar angular101 commented on September 4, 2024

Hi,
It's been a week since my last post so hopefully someone can find a little time to just confirm/deny what I'm asking.

I don't think it's possible to set onReady or onAuthSuccess correctly with the current KeycloakService implementation because you can only set them after keycloak has initialized?

Thanks.

from keycloak-angular.

mauriciovigolo avatar mauriciovigolo commented on September 4, 2024

Hi @angular101,

Sorry for the delay. You can only set the events after the keycloak-js initialization. I was working on an example to show you how to handle the events, but didn't finish it yet.

Don't know if it is clear, but onAuthSuccess will be called if there is a second tab opened and a success login happen. The login workflow should not be onLoad: 'login-required', because in this case, if the user is not logged in, he will be redirected to the keycloak login page.
You should instead use onLoad: 'check-sso' and add the route without authentication in the bearerExcludedUrls array, then if a log in happens in another tab you will be able to get the event.

I will try to add an example today, okay? You could also find me on slack.

from keycloak-angular.

angular101 avatar angular101 commented on September 4, 2024

Thanks @mauriciovigolo appreciate you creating an example.

from keycloak-angular.

abhishekkaura avatar abhishekkaura commented on September 4, 2024

@angular101 @mauriciovigolo By any chance you are able to create example as I am also facing the same issue for onAuthSuccess()

from keycloak-angular.

abhishekkaura avatar abhishekkaura commented on September 4, 2024

@mauriciovigolo Thanks !!!

from keycloak-angular.

mauriciovigolo avatar mauriciovigolo commented on September 4, 2024

Just found a bug in KeycloakService while doing the example, related to the order to register the events.

@angular101, during the keycloak-js events tests, I discover that the only propagated event between tabs is the onAuthLogout. All the others don't work between tabs. 😞

Thanks for reporting.

from keycloak-angular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.