Reordenado auth y reactivado auth matrix

This commit is contained in:
2021-03-19 23:21:25 +01:00
parent d8e5ddc253
commit 88ae0450ad

View File

@@ -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<boolean>;
private loggedSubject = new BehaviorSubject<boolean>(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<AuthState> {
const checkAuthSubject = new Subject<boolean>();
this.oidcSecurityService.checkAuth().subscribe(checkAuthSubject);
this.checkAuthSubject = new Subject<boolean>();
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;
}
}