When I log in, I get the authentication token. I'm using the getHeaders()
function by passing the authentication token I get from the backend as a parameter to insert it in header
.
shared.service.ts
import { Injectable } from '@angular/core';
import { RequestOptions } from '@angular/http';
@Injectable()
export class SharedService {
headers;
url = 'http://localhost:8000/api/';
constructor() {
this.headers = '';
}
setHead(token: string){
this.headers = new Headers({
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json'
});
}
getHead(){
return new RequestOptions({ headers: this.headers });
}
}
app.module.ts
import { SharedService } from './shared.service';
@NgModule({
bootstrap: [AppComponent],
providers: [
...
SharedService,
],
export class AppModule {
}
app.components.ts
constructor(
private authService: NbAuthService,
private sharedService: SharedService
) {
this.authService.onTokenChange()
.subscribe((token: NbAuthJWTToken) => {
if(token.isValid()){
this.sharedService.setHead(token.toString());
}
});
}
However, giving console.log(this.sharedService.getHead())
header
does not return Content-Type: application/json
and Authorization: Bearer ...
. It contains only __proto__
:
console.log return
body : null
headers : Headers
__proto__ : Headers
method : null
params : undefined
responseType : null
search : (...)
url : null
withCredentials : null
__proto__ : Object