Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
Implement HTTP AuthInterceptor
Browse files Browse the repository at this point in the history
- Adds token to API requests
- Redirects to home page in case of auth failure
  • Loading branch information
CloudNiner authored and maurizi committed Jul 12, 2019
1 parent c135ed2 commit 3eb24a3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/angular/planit/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ import { SuggestedActionService } from './core/services/suggested-action.service
import { UserService } from './core/services/user.service';
import { WeatherEventService } from './core/services/weather-event.service';

import { httpInterceptorProviders } from './core/interceptors';

import {
AccordionModule,
AlertModule,
Expand Down Expand Up @@ -162,6 +164,7 @@ const AGM_CONFIG = {
exports: [],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
providers: [
httpInterceptorProviders,
AccountCreateService,
ActionCategoryService,
ActionTypeService,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Injectable } from '@angular/core';
import {
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';

import { Observable, throwError } from 'rxjs';
import { AuthService } from '../services/auth.service';
import { catchError } from 'rxjs/operators';

/** Pass untouched request through to the next request handler. */
@Injectable()
export class AuthErrorInterceptor implements HttpInterceptor {

constructor(private authService: AuthService) {}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(catchError(err => {
if (err.status === 401) {
this.authService.logout();
}
return throwError(err);
}));
}
}
22 changes: 22 additions & 0 deletions src/angular/planit/src/app/core/interceptors/AuthInterceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from '@angular/core';
import {
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';

import { Observable } from 'rxjs';
import { AuthService } from '../services/auth.service';

/** Pass untouched request through to the next request handler. */
@Injectable()
export class AuthInterceptor implements HttpInterceptor {

constructor(private authService: AuthService) {}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authToken = this.authService.getToken();
const authRequest = req.clone({ setHeaders: {
Authorization: `Token ${authToken}`
}})
return next.handle(authRequest);
}
}
9 changes: 9 additions & 0 deletions src/angular/planit/src/app/core/interceptors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { HTTP_INTERCEPTORS } from '@angular/common/http';

import { AuthInterceptor } from './AuthInterceptor';
import { AuthErrorInterceptor } from './AuthErrorInterceptor';

export const httpInterceptorProviders = [
{provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true},
{provide: HTTP_INTERCEPTORS, useClass: AuthErrorInterceptor, multi: true}
];

0 comments on commit 3eb24a3

Please sign in to comment.