angular - Angular2-Token not sending headers -
i finished setting angular2-token auth , see in docs, should sending client uid expiry , token in headers of every request, i'm noticing getting default sign in response on back-end.
my angular(4) service simple.
export class clientservice { constructor(private http: http) { } private clientsurl = 'baseurl/clients'; getclients() : observable<client[]> { return this.http.get(this.clientsurl) .map((res: response) => res.json()) .catch((error:any) => observable.throw(error.json().error || 'server error')); }; and in component:
export class clientcomponent implements oninit { constructor(private clientservice: clientservice) { } clients: client[]; ngoninit() { this.getclients(); } getclients() { this.clientservice.getclients() .subscribe( clients => this.clients = clients, err => { console.log(err); } ); } } i have generic model including timestamps + id because i'm unsure how handle response.
export class client { constructor( id: number, name: string, status: string, logo: string, user_id: number, created_at: date, updated_at: date ){} } i have tested endpoint in postman , response expect. send access_token client , uid in headers, , auth's no problem.
when check network don't see headers being passed in request.
get /clients http/1.1 host: baseurl connection: keep-alive pragma: no-cache cache-control: no-cache accept: application/json, text/plain, */* origin: http://localhost:8080 user-agent: mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, gecko) chrome/60.0.3112.90 safari/537.36 referer: http://localhost:8080/clients accept-encoding: gzip, deflate, br accept-language: en-us,en;q=0.8 i looking how prepend them -every- single call, think angular2-token supposed solve explained in this issue
am going improperly, or have make sort of interceptor prepend headers?
updated code
thanks comment below, realized need pass headers. have modified work snippet below, angular2-token supposed automatically send headers. should follow jwt-token logic or angular2-token?
getclients() : observable<client[]> { let headers = new headers({ 'content-type': 'application', 'access-token': localstorage.getitem('accesstoken'), 'client': localstorage.getitem('client'), 'uid':localstorage.getitem('uid') }); let options = new requestoptions({ headers: headers}); return this.http.get(this.clientsurl, options) .map((res: response) => res.json()) .catch((error:any) => observable.throw(error.json().error || 'server error')); };
in options set withcredentials true
let options = new requestoptions({ headers: headers}); options.withcredentials = true;///////////////////add also append headers 1 one
let headers = new headers({ 'content-type': 'application', }); headers.append('access-token', localstorage.getitem('accesstoken')); headers.append('client', localstorage.getitem('client')) headers.append('uid', localstorage.getitem('uid'))
Comments
Post a Comment