I need an Authorization header to be appended to each and every HTTP request done by my Angular2 app.
The first request would be the login request, which does not have an authorization header. Once the login in is successful, a token is returned from the server and I need that to be appended as an authorization header to each and every subsequent HTTP requests.
I am getting the the token from the server without any problem. I can log in successfully using my angular2 app. But the problem is, the token is not attached to subsequent request headers.
This is my implementation.
I have created a CustomRequestOptions class extending the BaseRequestOptions class.
@Injectable()
export class CustomRequestOptions extends BaseRequestOptions {
constructor(private _globals: Globals) {
super();
this.headers.append('Content-Type', 'application/json');
}
addHeader(headerName: string, headerValue: string) {
console.log("Custom req options, setting the customer header "+headerName+" , "+headerValue);
this.headers.append(headerName, headerValue);
}
}
I have added that to @NgModule in order for this CustomRequestOptions to be global.(to be visible across all the components)
@NgModule({
imports: [ BrowserModule, HttpModule, routing, FormsModule ],
declarations: [ AppComponent, TendersComponent, TenderCategoriesComponent, TenderDetailsComponent, PageNotFoundComponent, ConvertUrlPipe ],
providers : [ Globals,
{provide: RequestOptions, useClass: CustomRequestOptions}
],
bootstrap: [ AppComponent ]
})
Now, in my component class, I am calling the addHeader method in CustomRequestOptions.
export class LoginComponent {
private _token: string;
private _returnMsg: string;
constructor(private _usersService: UsersService, private _globals: Globals, private _customRequestOptions: CustomRequestOptions) {
}
onLoginFormSubmit(formData: any) {
this._usersService.login(formData.emailAddress, formData.password).subscribe(
returnRes => {
this._returnMsg = returnRes.returnMsg;
this._token = returnRes.token;
if (this._returnMsg == "SUCCESS") {
//sucess
console.log("AUTH SUCCESS");
let x = "Bearer " + this._token;
this._customRequestOptions.addHeader("Authorization", x);
this.getUserRoles(); //this call needs authorization header.
}
}
);
}
}
Even though I set the header using this._customRequestOptions.addHeader("Authorization", x);, the next call, which needs Authorization header in order to work properly, fails. In server side,I can see, that part is null.
What is wrong here?