From f1c3824362f3d10e72c5cb92f92b99ce7c3e321d Mon Sep 17 00:00:00 2001 From: Eneko Date: Sat, 20 Mar 2021 01:11:16 +0100 Subject: [PATCH] Limpieza auth --- src/app/app.component.ts | 5 ++-- src/app/services/auth.service.ts | 40 +++++++++++--------------------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 171be5f..614c95c 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -19,9 +19,8 @@ export class AppComponent implements OnInit, OnDestroy { async ngOnInit(): Promise { console.log('AppComponent.ngInit()'); - // const chatLoginToken = await this.waitChatToken(); - // const authState = await this.authService.configureAndTryLogin(chatLoginToken); - const authState = await this.authService.configureAndTryLogin(null); + const chatLoginToken = await this.waitChatToken(); + const authState = await this.authService.configureAndTryLogin(chatLoginToken); if (authState !== AuthState.ChatLoggingInProcess) { this.showOverlay = false; diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 85eb51e..4b483ee 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -1,7 +1,6 @@ import { Injectable } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; -import { BehaviorSubject, Subject } from 'rxjs'; -import { first } from 'rxjs/operators'; +import { BehaviorSubject } from 'rxjs'; import { User } from '../entities/user'; import { ApiService } from './api.service'; import { ChatService } from './chat.service'; @@ -16,8 +15,6 @@ export enum AuthState { providedIn: 'root' }) export class AuthService { - private checkAuthSubject: Subject; - private loggedSubject = new BehaviorSubject(false); public logged$ = this.loggedSubject.asObservable(); @@ -32,14 +29,6 @@ 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 { @@ -51,24 +40,21 @@ export class AuthService { } async configureAndTryLogin(chatLoginToken: string): Promise { - this.checkAuthSubject = new Subject(); - this.oidcSecurityService.checkAuth().subscribe(this.checkAuthSubject); - - this.oidcSecurityService.userData$.subscribe((claims) => { - console.log(claims); - console.log('name=' + User.fromClaims(claims)?.name); - this.userSubject.next(User.fromClaims(claims)); - if (claims == null) { - this.loggedSubject.next(false); - } - }); this.oidcSecurityService.isAuthenticated$.subscribe((auth) => { console.log('isAuthenticated', auth); this.loggedSubject.next(auth); }); - return this.checkAuthSubject.toPromise().then((auth) => { - console.log('is authenticated', auth); + this.oidcSecurityService.userData$.subscribe((claims) => { + if (claims != null) { + console.log(claims); + console.log('name=' + User.fromClaims(claims)?.name); + } + this.userSubject.next(User.fromClaims(claims)); + }); + + return this.oidcSecurityService.checkAuth().toPromise().then((auth) => { + console.log('AUTH INITIALIZED result=', auth); if (auth) { if (sessionStorage.getItem('chatLoginInProcess') === 'true') { @@ -82,7 +68,9 @@ export class AuthService { console.error('CHAT LOGIN TOKEN NOT RECEIVED'); } - this.loggedSubject.next(true); + // Endpoint /user/ creates User in DB if it does not exist + this.apiService.getUser().subscribe(res => console.log(res)); + return AuthState.Logged; } else {