Angular 7 Http Authorization Interceptor
npm install a4-http-authorization-interceptor
- Add
HttpAuthorizationModule
to imports of theapp.module.ts
.
...
import { HttpAuthorizationModule } from 'a4-http-authorization';
...
@NgModule({
declarations: [
AppComponent
],
imports: [
...,
HttpAuthorizationModule,
...
],
...
})
constructor(private httpAuthorizationService: HttpAuthorizationService) { }
this.httpAuthorizationService.setTokenCookieName('oauth-cookie');
this.httpAuthorizationService.setHttpAuthorizationCallback(req => {
// your logic here
return true; // return true to allow injector to inject the authorization header
// return false and injector will not inject the authorization header
});
const header = this.httpAuthorizationService.getAuthorizationHeader();
This will return Bearer <token>
if cookie is found, otherwise it returns an empty string ('').