This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds token to API requests - Redirects to home page in case of auth failure
- Loading branch information
1 parent
c135ed2
commit 3eb24a3
Showing
4 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/angular/planit/src/app/core/interceptors/AuthErrorInterceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/angular/planit/src/app/core/interceptors/AuthInterceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
]; |