Да, вы можете использовать HttpInterceptor
в Angular, где вы можете проверить авторизацию, например
import {Injectable} from '@angular/core';
import {HttpInterceptor, HttpRequest, HttpHandler, HttpEvent} from '@angular/common/http';
import {Observable, from} from 'rxjs';
import {switchMap} from 'rxjs/operators';
import {AuthService} from './auth.service';
@Injectable()
export class BearerInterceptor implements HttpInterceptor {
constructor(protected authService: AuthService) {}
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return from(this.authService.isLoggedIn())
.pipe(
switchMap(isLoggedIn => {
if (isLoggedIn) {
return this.authService.addTokenToHeader(req.headers)
.pipe(
switchMap(headersWithBearer => {
const requestWithBearer = req.clone({headers: headersWithBearer});
return next.handle(requestWithBearer);
})
);
}
return next.handle(req);
})
);
}
}