🚑 Fix logout in case of 401 error
continuous-integration/drone/push Build is passing Details

master
parent 1de36ac40b
commit c6919379b6

@ -5,12 +5,11 @@ import {
HttpEvent,
HttpErrorResponse,
} from '@angular/common/http';
import { Observable, from, throwError } from 'rxjs';
import { Observable, from, of, throwError } from 'rxjs';
import { catchError, switchMap } from 'rxjs/operators';
import { inject } from '@angular/core';
import { CookiesService } from './services/cookies/cookies.service';
import { ModalService } from './services/modal/modal.service';
import { Router } from '@angular/router';
import { AuthService } from './services/auth/auth.service';
export const AuthInterceptor: HttpInterceptorFn = (
req: HttpRequest<unknown>,
@ -22,9 +21,7 @@ export const AuthInterceptor: HttpInterceptorFn = (
}
const cookiesService = inject(CookiesService);
const router = inject(Router);
const modalService = inject(ModalService);
const authService = inject(AuthService);
return from(cookiesService.getValidToken()).pipe(
switchMap((token) => {
const authReq = token
@ -37,10 +34,7 @@ export const AuthInterceptor: HttpInterceptorFn = (
}),
catchError((err: HttpErrorResponse) => {
if (err.status === 401) {
cookiesService.clearSession();
router.navigate(['/']).then(() => {
modalService.openModal('login-modal');
});
authService.logout(false);
}
return throwError(() => err);
})

@ -56,15 +56,24 @@ export class AuthService {
);
}
logout(): void {
this.pushService.unsubscribe().finally(() => {
logout(withPush: boolean = true): void {
if (withPush) {
this.pushService.unsubscribe().finally(() => {
this.isAdminSubject.next(false);
this.username$.next('');
this.userIdSubject.next('');
this.isLoggedIn$.next(false);
this.cookiesService.clearSession();
this.router.navigate(['/']);
});
} else {
this.isAdminSubject.next(false);
this.username$.next('');
this.userIdSubject.next('');
this.isLoggedIn$.next(false);
this.cookiesService.clearSession();
this.router.navigate(['/']);
});
}
}
register(username: string, password: string): Observable<AuthResponse> {

Loading…
Cancel
Save