diff --git a/frontend/src/app/_components/table/table.component.ts b/frontend/src/app/_components/table/table.component.ts index 027fe04..9d18ed1 100644 --- a/frontend/src/app/_components/table/table.component.ts +++ b/frontend/src/app/_components/table/table.component.ts @@ -38,7 +38,6 @@ export class TableComponent implements OnInit, OnDestroy { loadTableData() { if(!this.sourceType) this.sourceType = "list"; this.api.get(this.sourceType).then((data: any) => { - console.log(data); this.data = data.filter((row: any) => typeof row.hidden !== 'undefined' ? !row.hidden : true); if(this.sourceType === 'list') { this.api.availableUsers = this.data.filter((row: any) => row.available).length; @@ -91,9 +90,7 @@ export class TableComponent implements OnInit, OnDestroy { } deleteService(id: number) { - console.log(id); this.translate.get(['table.yes_remove', 'table.cancel', 'table.remove_service_confirm', 'table.remove_service_text']).subscribe((res: { [key: string]: string; }) => { - console.log(res); Swal.fire({ title: res['table.remove_service_confirm'], text: res['table.remove_service_confirm_text'], diff --git a/frontend/src/app/_guards/authorize.guard.ts b/frontend/src/app/_guards/authorize.guard.ts index 7ea10c6..451b278 100644 --- a/frontend/src/app/_guards/authorize.guard.ts +++ b/frontend/src/app/_guards/authorize.guard.ts @@ -1,13 +1,23 @@ import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; -import { Observable } from 'rxjs'; +import { Observable, Subject } from 'rxjs'; +import { debounceTime } from 'rxjs/operators'; import { AuthService } from '../_services/auth.service'; @Injectable({ providedIn: 'root' }) export class AuthorizeGuard implements CanActivate { + //https://stackoverflow.com/a/48848557 + public loader$ = new Subject(); + public loader = false; + constructor(private authService: AuthService, private router: Router) { + this.loader$.pipe( + debounceTime(250) + ).subscribe((loader) => { + this.loader = loader; + }); } checkAuthAndRedirect( @@ -29,11 +39,18 @@ export class AuthorizeGuard implements CanActivate { state: RouterStateSnapshot ): Observable | Promise | boolean | UrlTree { if(this.authService.authLoaded) { + this.loader$.next(false); + console.log("Auth already loaded, checking if profile id exists"); return this.checkAuthAndRedirect(route, state); } else { + this.loader$.next(true); + console.log("Auth not loaded, waiting for authChanged"); return new Observable((observer) => { this.authService.authChanged.subscribe({ - next: () => { observer.next(this.checkAuthAndRedirect(route, state)); } + next: () => { + this.loader$.next(false); + observer.next(this.checkAuthAndRedirect(route, state)); + } }) }); } diff --git a/frontend/src/app/_providers/auth-interceptor.provider.ts b/frontend/src/app/_providers/auth-interceptor.provider.ts index 1b5e02f..9a72243 100644 --- a/frontend/src/app/_providers/auth-interceptor.provider.ts +++ b/frontend/src/app/_providers/auth-interceptor.provider.ts @@ -1,30 +1,25 @@ import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpErrorResponse } from '@angular/common/http'; -import { AuthService } from '../_services/auth.service'; import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; +import { Router } from "@angular/router"; @Injectable() export class AuthInterceptor implements HttpInterceptor { - constructor(/*private auth: AuthService*/) { } - - //TODO: fix interceptor and logout (client-side only) if 401 error + constructor(private router: Router) { } intercept(req: HttpRequest, next: HttpHandler): Observable> { - return next.handle(req); - /* return next.handle(req).pipe(catchError(error => { console.log(error); if (error instanceof HttpErrorResponse && !req.url.includes('login') && !req.url.includes('me') && !req.url.includes('logout')) { if(error.status === 400) { - this.auth.logout(); + this.router.navigate(["logout"]); return throwError(() => error); } else if (error.status === 401) { - this.auth.logout(); + this.router.navigate(["logout"]); } } return throwError(() => error); })); - */ } } diff --git a/frontend/src/app/_routes/login/login.component.ts b/frontend/src/app/_routes/login/login.component.ts index a729693..3b37858 100644 --- a/frontend/src/app/_routes/login/login.component.ts +++ b/frontend/src/app/_routes/login/login.component.ts @@ -39,6 +39,12 @@ export class LoginComponent { if(this.authService.isAuthenticated()) { this.router.navigate(this.redirectParamsList); + } else { + this.authService.authChanged.subscribe({ + next: () => { + this.router.navigate(this.redirectParamsList); + } + }); } } diff --git a/frontend/src/app/app.component.html b/frontend/src/app/app.component.html index 6a2688e..03a7147 100644 --- a/frontend/src/app/app.component.html +++ b/frontend/src/app/app.component.html @@ -8,7 +8,7 @@
-
+
diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index 8b0b2c4..f08ffab 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -6,6 +6,7 @@ import { Router, RouteConfigLoadStart, RouteConfigLoadEnd } from '@angular/route import { ApiClientService } from './_services/api-client.service'; import { ModalAlertComponent } from 'src/app/_components/modal-alert/modal-alert.component'; import { BsModalService } from 'ngx-bootstrap/modal'; +import { AuthorizeGuard } from './_guards/authorize.guard'; @Component({ selector: 'app-root', @@ -25,7 +26,8 @@ export class AppComponent { private locationBackService: LocationBackService, private router: Router, private api: ApiClientService, - private modalService: BsModalService + private modalService: BsModalService, + public guard: AuthorizeGuard ) { this.revision_datetime_string = new Date(versions.revision_timestamp).toLocaleString(undefined, { day: '2-digit', month: '2-digit', year: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' }); }