Created
July 27, 2020 09:56
-
-
Save pdashford/a705b0e1c6c0d6dba882065774ab50c8 to your computer and use it in GitHub Desktop.
Angular HTTP interceptor to remove 'Authorization' token when accessing external sites
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
export const environment = { | |
api: 'http://yourapi.com' | |
} |
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 { | |
HttpInterceptor, | |
HttpHandler, | |
HttpRequest, | |
HttpEvent} from '@angular/common/http' | |
import { Injectable } from '@angular/core' | |
import { Observable } from 'rxjs' | |
import { environment } from '../environments/environment' | |
@Injectable() | |
export class AppHttpHeaderInterceptor implements HttpInterceptor { | |
constructor() {} | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
const ignore = req.url.startsWith(environment.api) | |
if (ignore) { | |
return next.handle(req) | |
} | |
const cloned = req.clone({ | |
headers: req.headers.delete('Authorization') | |
}) | |
return next.handle(cloned) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment