Fixed login state problems #70

This commit is contained in:
Sven Heidemann 2022-10-16 16:01:37 +02:00
parent d5ad5ded88
commit f319e89473
4 changed files with 36 additions and 23 deletions

View File

@ -1,5 +1,5 @@
<main [class]="themeName"> <main [class]="themeName">
<ng-container *ngIf="authService.isLoggedIn; else login"> <ng-container *ngIf="isLoggedIn; else login">
<app-header></app-header> <app-header></app-header>
<section class="app"> <section class="app">

View File

@ -16,7 +16,7 @@ export class AppComponent implements OnInit {
isLoggedIn: boolean = false; isLoggedIn: boolean = false;
constructor( constructor(
public authService: AuthService, private authService: AuthService,
private themeService: ThemeService, private themeService: ThemeService,
private socket: SocketService private socket: SocketService
) { } ) { }
@ -28,9 +28,12 @@ export class AppComponent implements OnInit {
this.themeService.themeName$.subscribe(value => { this.themeService.themeName$.subscribe(value => {
this.themeName = value; this.themeName = value;
}); });
this.authService.isLoggedIn$.subscribe(value => {
this.isLoggedIn = value;
});
this.socket.startSocket();
this.themeService.loadTheme(); this.themeService.loadTheme();
this.socket.startSocket();
} }

View File

@ -2,7 +2,7 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { JwtHelperService } from '@auth0/angular-jwt'; import { JwtHelperService } from '@auth0/angular-jwt';
import { firstValueFrom, Observable, Subscription } from 'rxjs'; import { firstValueFrom, Observable, Subject, Subscription } from 'rxjs';
import { catchError } from 'rxjs/operators'; import { catchError } from 'rxjs/operators';
import { AdminUpdateUserDTO } from 'src/app/models/auth/admin-update-user.dto'; import { AdminUpdateUserDTO } from 'src/app/models/auth/admin-update-user.dto';
import { AuthRoles } from 'src/app/models/auth/auth-roles.enum'; import { AuthRoles } from 'src/app/models/auth/auth-roles.enum';
@ -21,8 +21,8 @@ import { SpinnerService } from '../spinner/spinner.service';
}) })
export class AuthService { export class AuthService {
invalidLogin!: boolean; private isLoggedIn!: boolean;
isLoggedIn!: boolean; isLoggedIn$ = new Subject<boolean>();
constructor( constructor(
private appsettings: SettingsService, private appsettings: SettingsService,
@ -31,7 +31,9 @@ export class AuthService {
private jwtHelper: JwtHelperService, private jwtHelper: JwtHelperService,
private spinnerService: SpinnerService private spinnerService: SpinnerService
) { ) {
this.isUserLoggedInAsync(); this.isLoggedIn$.subscribe(value => {
this.isLoggedIn = value;
});
} }
/* data requests */ /* data requests */
@ -187,14 +189,20 @@ export class AuthService {
}) })
}).pipe(catchError((error: any) => { }).pipe(catchError((error: any) => {
error.error = null; error.error = null;
this.isLoggedIn$.next(false);
localStorage.removeItem('rjwt');
this.router.navigate(['/auth/login']);
throw error; throw error;
})).subscribe(() => { })).subscribe(() => {
this.isLoggedIn = false; this.isLoggedIn$.next(false);
localStorage.removeItem('jwt'); localStorage.removeItem('jwt');
localStorage.removeItem('rjwt'); localStorage.removeItem('rjwt');
this.router.navigate(['/auth/login']); this.router.navigate(['/auth/login']);
}); });
} }
this.isLoggedIn$.next(false);
localStorage.removeItem('rjwt');
this.router.navigate(['/auth/login']);
return null return null
} }
@ -203,16 +211,20 @@ export class AuthService {
const token = this.getToken(); const token = this.getToken();
if (!token || !token.refreshToken) { if (!token || !token.refreshToken) {
this.isLoggedIn = false; this.isLoggedIn$.next(false);
return false; return false;
} }
const verifiedLogin = await firstValueFrom(await this.verifyLogin()); try {
const verifiedLogin = await firstValueFrom(this.verifyLogin());
if (verifiedLogin) { if (verifiedLogin) {
this.isLoggedIn = true; this.isLoggedIn$.next(true);
return true; return true;
} }
} catch (error: unknown) {
this.isLoggedIn$.next(false);
return false;
}
if (this.isLoggedIn) { if (this.isLoggedIn) {
this.spinnerService.showSpinner(); this.spinnerService.showSpinner();
@ -229,14 +241,12 @@ export class AuthService {
return false; return false;
} }
hasUserPermission(role: AuthRoles): Promise<boolean> { async hasUserPermission(role: AuthRoles): Promise<boolean> {
return this.isUserLoggedInAsync().then(isLoggedIn => { if (!role || !await this.isUserLoggedInAsync()) {
if (!role || !isLoggedIn) {
return false; return false;
} }
const token = this.getDecodedToken(); const token = this.getDecodedToken();
return AuthRoles[token['role']] === AuthRoles[role]; return AuthRoles[token['role']] === AuthRoles[role];
});
} }
getEMailFromDecodedToken(token: { [key: string]: any }): string | null { getEMailFromDecodedToken(token: { [key: string]: any }): string | null {

View File

@ -20,10 +20,10 @@ export class SocketService {
private spinnerService: SpinnerService, private spinnerService: SpinnerService,
private messageService: MessageService, private messageService: MessageService,
) { ) {
this.socket = io(`${settingsService.getApiURL()}`)
} }
startSocket() { startSocket() {
this.socket = io(`${this.settingsService.getApiURL()}`)
this.socket.on('connect', () => { this.socket.on('connect', () => {
if (this.disconnected) { if (this.disconnected) {
if (this.spinnerService.showSpinnerState) { if (this.spinnerService.showSpinnerState) {