angular-oauth2-oidc: Tokens are not set immediately after redirect
Hello,
I am using version 3.1.4 of this library.
My set-up is as follows:
after users enters his credentials on the identity server he is redirected to a protected resource.
My canActivate method looks as follows.
canActivate(): boolean {
const validIdToken = this.oauthService.hasValidIdToken;
const validAccessToken = this.oauthService.hasValidAccessToken();
return (validIdToken && validAccessToken);
}
However, at the time when canActivate() is called both tokens are not available immediately
(even though user is authenticated and they should be set).
canActivate() also returns false.
I can see they eventually arrive:
this.oauthService.events.subscribe(({ type } : OAuthEvent) => {
switch (type) {
case 'token_received':
const idToken = this.oauthService.getIdToken();
const accessToken = this.oauthService.getAccessToken();
if (accessToken && idToken) {
console.log(accessToken);
console.log(idToken);
}
}
});
Is there some way to prevent this - ensuring that they are already set when canActivate() is called?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 16 (5 by maintainers)
@Razzeee In my app, on routing i use CanActivate - which secure access to component before you are not login ( You are not logged - component cannot be load, first login bro!)
My app.component.ts
Routing:
And finally guard
you’ll need to resolve the above event that you demonstrate subscribing to within your guard because you must wait for the discovery document to load, which is async. canActivate can accept a promise return, or better yet an Observable<boolean>. One option might be to use the OAuthService.TryLogin() which returns a promise, something like:
*The above is pseudo-code, your implementation will most likely vary. HTH
谢谢你朋友,我通过你的方法解决了,页面登录死循环问题,很感谢你! 这是我app.component.ts的代码
import { Component, OnInit } from '@angular/core'; import { OAuthService, JwksValidationHandler } from 'angular-oauth2-oidc'; import { } from 'angular-oauth2-oidc'; import { authConfig } from './auth.config'; import { Router } from '@angular/router'; import { Observable } from 'rxjs'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.less'] }) export class AppComponent implements OnInit { constructor(private authService: OAuthService, public router: Router, public routerActive: ActivatedRoute) { this.authService.configure(authConfig); this.authService.tokenValidationHandler = new JwksValidationHandler(); this.authService.loadDiscoveryDocumentAndLogin(); } ngOnInit() { } }这是auth.guard.ts `import { Injectable } from ‘@angular/core’; import {CanActivate, Router } from ‘@angular/router’; import { OAuthService, JwksValidationHandler } from ‘angular-oauth2-oidc’; import { authConfig } from ‘…/…/auth.config’; import { Observable } from ‘rxjs’;@Injectable() export class AuthGuard implements CanActivate {
}`
Thank you so much for your detailed reply. I really appreciate it. It helps a lot.
This worked for me:
I got the same problem, but it’s a bit different for me. As I’m using the api interceptor to add the bearer. But the first requests after being logged in fails due to no auth header, seems like the api calls are send faster then the interceptor knows of the token.