Working as a drop-in replacement for the last
This commit is contained in:
@ -7,9 +7,10 @@ import { MatMenuModule } from '@angular/material/menu';
|
||||
import { MatToolbarModule } from '@angular/material/toolbar';
|
||||
import {RouterModule} from '@angular/router';
|
||||
import {HTTP_INTERCEPTORS} from '@angular/common/http';
|
||||
import {HttpAuthInterceptor} from './http-auth-interceptor';
|
||||
import {LoadingBarHttpClientModule} from '@ngx-loading-bar/http-client';
|
||||
import {LoadingBarRouterModule} from '@ngx-loading-bar/router';
|
||||
import {JwtInterceptor} from './jwt.interceptor';
|
||||
import {ErrorInterceptor} from './http-auth-interceptor';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
@ -30,11 +31,10 @@ import {LoadingBarRouterModule} from '@ngx-loading-bar/router';
|
||||
LoadingBarHttpClientModule,
|
||||
LoadingBarRouterModule
|
||||
],
|
||||
providers: [[{
|
||||
provide: HTTP_INTERCEPTORS,
|
||||
useClass: HttpAuthInterceptor,
|
||||
multi: true
|
||||
}]]
|
||||
providers: [
|
||||
{provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true},
|
||||
{provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true},
|
||||
]
|
||||
})
|
||||
export class CoreModule {
|
||||
}
|
||||
|
||||
@ -1,26 +1,24 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
|
||||
|
||||
import {Observable, throwError} from 'rxjs';
|
||||
import {catchError} from 'rxjs/operators';
|
||||
import {ToasterService} from './toaster.service';
|
||||
import {Router} from '@angular/router';
|
||||
import {ConfirmDialogComponent} from '../shared/confirm-dialog/confirm-dialog.component';
|
||||
import { Observable, throwError } from 'rxjs';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
|
||||
import { Router } from '@angular/router';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { ToasterService } from './toaster.service';
|
||||
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
|
||||
|
||||
@Injectable()
|
||||
export class HttpAuthInterceptor implements HttpInterceptor {
|
||||
export class ErrorInterceptor implements HttpInterceptor {
|
||||
constructor(private authService: AuthService, private router: Router, private dialog: MatDialog, private toaster: ToasterService) { }
|
||||
|
||||
constructor(private router: Router, private dialog: MatDialog, private toaster: ToasterService) {
|
||||
}
|
||||
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler):
|
||||
Observable<HttpEvent<any>> {
|
||||
return next.handle(req)
|
||||
.pipe(
|
||||
catchError((error: any) => {
|
||||
if (error.status === 401) {
|
||||
this.toaster.show('Danger', 'User has been logged out');
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
return next.handle(request).pipe(catchError(err => {
|
||||
if (err.status === 401) {
|
||||
console.log('caught 401 in error interceptor');
|
||||
// auto logout if 401 response returned from api
|
||||
this.authService.logout();
|
||||
this.toaster.show('Danger', 'User has been logged out');
|
||||
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
||||
width: '250px',
|
||||
data: {
|
||||
@ -34,9 +32,8 @@ export class HttpAuthInterceptor implements HttpInterceptor {
|
||||
}
|
||||
});
|
||||
}
|
||||
const error = err.error.message || err.statusText;
|
||||
return throwError(error);
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
25
overlord/src/app/core/jwt.interceptor.ts
Normal file
25
overlord/src/app/core/jwt.interceptor.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpRequest, HttpHandler, HttpEvent, HttpInterceptor} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
|
||||
import {AuthService} from '../auth/auth.service';
|
||||
|
||||
@Injectable()
|
||||
export class JwtInterceptor implements HttpInterceptor {
|
||||
constructor(private authService: AuthService) {
|
||||
}
|
||||
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
// add authorization header with jwt token if available
|
||||
const currentUser = this.authService.user;
|
||||
if (currentUser && currentUser.access_token) {
|
||||
request = request.clone({
|
||||
setHeaders: {
|
||||
Authorization: `Bearer ${currentUser.access_token}`
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return next.handle(request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user