Skip to content

Instantly share code, notes, and snippets.

@pdashford
Created July 27, 2020 09:56
Show Gist options
  • Save pdashford/a705b0e1c6c0d6dba882065774ab50c8 to your computer and use it in GitHub Desktop.
Save pdashford/a705b0e1c6c0d6dba882065774ab50c8 to your computer and use it in GitHub Desktop.
Angular HTTP interceptor to remove 'Authorization' token when accessing external sites
export const environment = {
api: 'http://yourapi.com'
}
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