Angular-auth-oidc-client: getToken() returns ""

Created on 11 Jun 2020  路  5Comments  路  Source: damienbod/angular-auth-oidc-client

Describe the bug
I use the Azure AD OIDC Code Flow with PKCE. Set everything up with protected routes and everything works fine. But, when i call the getToken()-method i get a "" in return.

The following code in my auth-guard returns true as shown in screenshot further down;

return this.oidcSecurityService.isAuthenticated$.pipe(
            map((isAuthorized: boolean) => {
                console.log('AuthorizationGuard, canActivate isAuthorized: ' + isAuthorized);

                if (!isAuthorized) {
                    this.router.navigate(['/unauthorized']);
                    return false;
                }

                return true;
            })
        );

Expected behavior
I expected the access-code to be returned so i can use it in my http-requests.

Screenshots
Snapshot from runtime (from method inside oicd-package):

image

Some snapshots from console after route-changing to show that i actually am authorized.:
image

Desktop (please complete the following information):

  • OS: Linux Ubuntu 20.04
  • Browser Chrome and safari

Additional context
Add any other context about the problem here.

question

Most helpful comment

Well, after some tinkering, i figured out what was wrong. - typo in my scopes.

All 5 comments

hi @kimrunar Are you using the getToken before the authorize request is finished? How do you use this, in an interceptor?

Greetings Damien

No, i use the getToken in another protected component on ng-init. So i first change routes to check if everything is ok. - Which it is based on the output from the interceptor.. Then i go to the route that runs the getToken that oddly fails to retrieve my token.

image

The chechAuth completes after the getToken is called, which means you call it before the auth process is complete.
Use the authenticated event to check if you are authenticated and then call the getToken().
Pease check the examples and the docs for appropriate usage

https://github.com/damienbod/angular-auth-oidc-client/blob/master/docs/samples.md

Here's 2 more examples:

https://github.com/damienbod/AzureAD-Auth-MyUI-with-MyAPI/tree/master/MySpaAngularUI

https://github.com/FabianGosebrink/angular-oauth2-oidc-sample/tree/master/client/angular-oidc-oauth2-ngrx

Greetings Damien

Ok thank you,
so now i tried your example 'MySpaAngularUI' and its the same result.
Why is auth : false in console when it says i'm logged in/authorized in the app...? Is there something i miss?

image

Well, after some tinkering, i figured out what was wrong. - typo in my scopes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jonesie picture Jonesie  路  4Comments

profet23 picture profet23  路  3Comments

nizarkhsib picture nizarkhsib  路  4Comments

toddtsic picture toddtsic  路  4Comments

vit100 picture vit100  路  4Comments