6

I'm using release version of 8.0.4 in my angular 8 project with authorization code flow:

here is the code I have

    this.oauthService.configure(authConfig);
    this.oauthService.tokenValidationHandler = new JwksValidationHandler();
    this.oauthService
      .loadDiscoveryDocument()
      .then(() => this.oauthService.tryLogin()) ---> [1]
      .then(() => {
        if (this.oauthService.hasValidAccessToken()) {
          return Promise.resolve();
        }else{
          this.oauthService.initCodeFlow() ---> [2]
        }
      });
  }

Initially when user is not logged in the code at [2] redirects user to login page.

Once user provides username/password and click login, the identity provider redirects back to app with "code" in querystring, that is when I'm expecting code at [1] to login the user with code (by redeeming it for tokens).

Instead the tryLogin() method doesn't work and the user is again redirected to authorzation endpoint in endless loop.

Please help me understand, what is going wrong here.

also, does this example : https://github.com/jeroenheijmans/sample-angular-oauth2-oidc-with-auth-guards/ apply for version 8 ?

nari447
  • 834
  • 2
  • 11
  • 25
  • I believe this is not supported for code flow... I had a similar issue, where I needed to know whether or not a user was "logged-into" an identity provider. Since we are also using code flow, I could not use tryLogin() to figure-out whether I was logged-in or not. – nicolas.leblanc May 11 '21 at 12:56
  • @nicolas.leblanc so which method did you use? experiencing the same issue.. – Giacomo Bartoli Jan 17 '23 at 22:15
  • @GiacomoBartoli I did this: ``` const url = await this.service.completeLogin(); return this.router.parseUrl(url); public async completeLogin(): Promise { return this.oauthService.tryLogin().then(() => { return decodeURIComponent(this.oauthService.state); }).catch(async() => { // as nonce and PKCE verifier could have been override by another tab // we must try to silently refresh to make sure login will work await this.isLoggedIn(); return decodeURIComponent(this.oauthService.state); }); } ``` – nicolas.leblanc Jan 24 '23 at 19:37

1 Answers1

0

This function could be used to figure out whether you are already logged-in or not:

public isLoggedIn(): Promise<boolean> {
  return this.oauthService.loadDiscoveryDocument('<your discoveryDocumentUrl>').then(() => {
    return this.oauthService.silentRefresh().then(() => true).catch(() => false);
  });
}

ref: https://manfredsteyer.github.io/angular-oauth2-oidc/docs/additional-documentation/silent-refresh.html

nicolas.leblanc
  • 588
  • 7
  • 27