Я реализовал аутентификацию аутентификации в приложении Angular 9 с помощью Azure B2C с использованием библиотеки angular-oauth2-oidc, и мне удалось получить токен, однако я одновременно получаю сообщение об ошибке «Uncaught (in обещание)» и «ошибка» при загрузке информации о пользователе TypeError. .
ошибка профиля пользователя
Обещать неперехваченную ошибку
Служба аутентификации
@Injectable()
export class AuthService implements OnInit{
_accessToken: string;
_idToken: string;
private isAuthenticatedSubject$ = new BehaviorSubject<boolean>(false);
public isAuthenticated$ = this.isAuthenticatedSubject$.asObservable();
private isDoneLoadingSubject$ = new ReplaySubject<boolean>();
public isDoneLoading$ = this.isDoneLoadingSubject$.asObservable();
public canActivateProtectedRoutes$: Observable<boolean> = combineLatest([
this.isAuthenticated$,
this.isDoneLoading$
]).pipe(map(values => values.every(b => b)));
constructor(
private oauthService: OAuthService,
private router: Router
){
//debugging - Capture Error Events
this.oauthService.events.subscribe(event =>{
if(event instanceof OAuthErrorEvent){
console.error(event);
}
else{
console.warn(event);
}
});
console.warn('Noticed changes to access_token (most likely from another tab), updating isAuthenticated');
this.isAuthenticatedSubject$.next(this.oauthService.hasValidAccessToken());
this.oauthService.events
.subscribe(_ => {
this.isAuthenticatedSubject$.next(this.oauthService.hasValidAccessToken());
});
this.oauthService.events
.pipe(filter(e => ['token_received'].includes(e.type)))
.subscribe(e => this.oauthService.loadUserProfile());
this.oauthService.events
.pipe(filter(e => ['session_terminated', 'session_error'].includes(e.type)))
.subscribe(e => this.navigateToLoginPage());
this.oauthService.setupAutomaticSilentRefresh();
}
public initializeAuthService(): Promise<void>{
this.oauthService.configure(authConfig);
if (location.hash) {
console.log('Encountered hash fragment, plotting as table...');
console.table(location.hash.substr(1).split('&').map(kvp => kvp.split('=')));
}
return this.oauthService.loadDiscoveryDocument(DiscoveryDocumentConfig.url)
.then(() => this.oauthService.tryLogin())
.then(() => {
if(this.oauthService.hasValidAccessToken()){
console.log("Discovery document resolved, Token does exist...", this.oauthService.getAccessToken());
return Promise.resolve();
}
})
.catch(error => {
console.error(error);
})
}
public login(targetUrl?: string){
this.oauthService.tryLogin({});
if(!this.oauthService.getAccessToken()){
this.oauthService.initImplicitFlow();
}
}