Angular authenticated HTTP requests (no JWT required).
Inspired by angular2-jwt
npm install angular2-http-auth
This is the configuration file. You can pass it default fixed headers as well as dynamic headers (such as the authorization token).
Make sure to import this module prior doing any authenticated HTTP call.
app.module.ts
is often a good place to import it.
import { AuthHttp, AuthConfig } from 'angular2-http-auth';
export function authHttpServiceFactory(
http: Http,
tokenIsHereService: OptionalServiceThatProvidesTheAuthorizationToken,
) {
return new AuthHttp(new AuthConfig({
headers: {
'Content-Type': 'application/json',
Authorization: () => tokenIsHereService.authorizationKey,
},
}), http);
}
@NgModule({
providers: [
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, OptionalServiceThatProvidesTheAuthorizationToken]
}
]
})
export class AuthHttpModule { }
It is very similar to angular2-jwt's, even though some parameters might be different.
import { HttpModule } from '@angular/http';
import { AuthHttpModule } from './auth-http.module';
@NgModule({
imports: [
...
HttpModule,
AuthHttpModule,
...
],
import { AuthHttp } from 'angular2-http-auth';
...
fetchPrivateStuff(): Observable<Response> {
return this.authHttp.post(`https://my-site.com/privateStuff`, {});
}
Default headers- Default search params
- Default body
- withCredentials (cookies)
MIT © Gerard Rovira Sánchez