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
import {Injectable} from '@angular/core'; | |
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http'; | |
@Injectable() | |
export class AuthInterceptor implements HttpInterceptor { | |
constructor(private auth: AuthService) {} | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
// Clone the request to add the new header. | |
const authReq = req.clone({headers: req.headers.set('Authorization', 'my-token')}); | |
// Pass on the cloned request instead of the original request. | |
return next.handle(authReq).do( | |
(event: : HttpEvent<any>) => { | |
if (event instanceof HttpResponse) { | |
// Request successful : You can change response | |
} | |
}).catch(err => { | |
if (err instanceof HttpErrorResponse { | |
if (err.status === 403) { | |
// Error Authorization | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment