ebondu / angular2-keycloak

Angular 2 Keycloak client wrapper
19 stars 26 forks source link

After login success parsedToken, isAuthenticated and profile is null or false (v 0.9) #18

Open pako-g opened 7 years ago

pako-g commented 7 years ago

After login the following variables are false or null

parsenToken
isAuthenticated
profile

this is the HomeComponent

export class HomeComponent implements OnInit {

  private parsedToken: any;
  private isAuthenticated: boolean;

  products: string[] = [];
  private profile: any;
  constructor(private keycloak: Keycloak,
              private keycloakAuthz: KeycloakAuthorization,
              private http: Http) {

    Keycloak.authenticatedObs.subscribe(auth => {
      this.isAuthenticated = auth;
      this.parsedToken = keycloak.tokenParsed;
      /*console.info('APP: authentication status changed...');*/
    });
  }

  ngOnInit() {
   /* console.info('APP : initializing home component...');*/

    this.keycloakAuthz.init();

    // comment or change regarding your app-name
    this.keycloak.config = 'keycloak.json';

    this.keycloak.init({
      checkLoginIframe: false
    });
  }

  login() {
    this.keycloak.login({});
  }

  logout() {
    this.keycloak.logout({});
  }

  loadProfile() {
    this.keycloak.loadUserProfile().subscribe(profile => {
      this.profile = profile;
    });
  }
}

I have change the Keycloak.tokenParsed with keycloak.tokenParsed

after login success schermata del 2017-08-25 19-45-11

ebondu commented 7 years ago

I have change the Keycloak.tokenParsed with keycloak.tokenParsed

You are right I didn't update the example/doc yet for changes since 0.9.0.

The authenticatedObs may return false during init phases, you may have to filter the returned values to ensure auth sucess and to get the token :

Keycloak.authenticatedObs.subscribe(authenticated => {
      if (authenticated) {
        this.identity = keycloak.tokenParsed;
      }
});
pako-g commented 7 years ago

I tried with

Keycloak.authenticatedObs.subscribe(authenticated => {
      if (authenticated) {
        this.isAuthenticated = authenticated;
        this.parsedToken = keycloak.tokenParsed;
      }
    });

inside the constructor but not work

pako-g commented 7 years ago

I can not get the token, login works but I can not load user data, token, and more authenticated is always false I also updated to version 0.9.1

sunmeplz commented 7 years ago

@ebondu Hi! do you plan to publish sources of 0.9.x version? Thanks

ebondu commented 7 years ago

Hi, Of course, I will try to push it today.

ebondu commented 7 years ago

I pushed last changes to the ng4 branch

AlanCrevon commented 7 years ago

Hi @ebondu.

As the documnetation doesn't match the npm version, i can't find a way to make your wrapper work with 0.9.1, even though i can see this is supposed to do exactly what i need the way i want it.

Could you please update your documentation ?

Thanks. Alan

ebondu commented 7 years ago

Hi @AlanCrevon, I updated the documentation on the ng4 branch, and I will try to provide an example before the end of the week. Main changes are related to AoT compilation: some static methods of the Keycloak class are now public. That means you need to inject the keycloak and call methods on the injected object instead of calling the method statically.