diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 404f43b..85eb51e 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; import { BehaviorSubject, Subject } from 'rxjs'; +import { first } from 'rxjs/operators'; import { User } from '../entities/user'; import { ApiService } from './api.service'; import { ChatService } from './chat.service'; @@ -15,6 +16,8 @@ export enum AuthState { providedIn: 'root' }) export class AuthService { + private checkAuthSubject: Subject; + private loggedSubject = new BehaviorSubject(false); public logged$ = this.loggedSubject.asObservable(); @@ -29,6 +32,14 @@ export class AuthService { login(): void { this.oidcSecurityService.authorize(); + + this.checkAuthSubject?.pipe(first()).subscribe((auth) => { + console.log('Login result=', auth); + if (auth) { + // Endpoint /user/ creates User in DB if it does not exist + this.apiService.getUser().subscribe(); + } + }); } logout(): void { @@ -40,16 +51,9 @@ export class AuthService { } async configureAndTryLogin(chatLoginToken: string): Promise { - const checkAuthSubject = new Subject(); - this.oidcSecurityService.checkAuth().subscribe(checkAuthSubject); + this.checkAuthSubject = new Subject(); + this.oidcSecurityService.checkAuth().subscribe(this.checkAuthSubject); - checkAuthSubject.subscribe((auth) => { - console.log('is authenticated', auth); - this.loggedSubject.next(auth); - if (auth) { - this.apiService.getUser().subscribe(); - } - }); this.oidcSecurityService.userData$.subscribe((claims) => { console.log(claims); console.log('name=' + User.fromClaims(claims)?.name); @@ -63,41 +67,33 @@ export class AuthService { this.loggedSubject.next(auth); }); - return checkAuthSubject.toPromise().then((auth) => { - console.log('is authenticated2', auth); + return this.checkAuthSubject.toPromise().then((auth) => { + console.log('is authenticated', auth); if (auth) { - // this.loggedSubject.next(true); - return AuthState.Logged; + if (sessionStorage.getItem('chatLoginInProcess') === 'true') { + sessionStorage.removeItem('chatLoginInProcess'); + + if (chatLoginToken) { + console.log('chatLoginToken=' + chatLoginToken); + this.chatService.login(chatLoginToken); + } + else { + console.error('CHAT LOGIN TOKEN NOT RECEIVED'); + } + + this.loggedSubject.next(true); + return AuthState.Logged; + } + else { + console.log('GO TO SSO LOGIN'); + sessionStorage.setItem('chatLoginInProcess', 'true'); + this.chatService.goToSsoLogin(); + return AuthState.ChatLoggingInProcess; + } } return AuthState.NotLogged; }); - - // const logged = await this.tryOauthLogin(); - // if (logged) { - // if (sessionStorage.getItem('chatLoginInProcess') === 'true') { - // sessionStorage.removeItem('chatLoginInProcess'); - - // if (chatLoginToken) { - // console.log('chatLoginToken=' + chatLoginToken); - // this.chatService.login(chatLoginToken); - // } - // else { - // console.error('CHAT LOGIN TOKEN NOT RECEIVED'); - // } - - // this.loggedSubject.next(true); - // return AuthState.Logged; - // } - // else { - // console.log('GO TO SSO LOGIN'); - // sessionStorage.setItem('chatLoginInProcess', 'true'); - // this.chatService.goToSsoLogin(); - // return AuthState.ChatLoggingInProcess; - // } - // } - - // return AuthState.NotLogged; } }