I am trying to send post request so I am retrieving my stored token but I cannot use it in my request function it return [object object]
Code
token: any; //to be used in post request
constructor(
private http: HttpClient,
private env: EnvService,
private storage: NativeStorage,
) {
this.storage.getItem('token').then((token) => {
this.token = token; //set token value
console.log('token', token); // will get the token in console
});
}
store(
name: String,
description: String,
phone: String,
province_id: String,
kota_id: String,
address: String,
logo: String,
banner: String,
) {
const headers = new HttpHeaders({
'Accept': 'application/json, text/plain',
'Content-Type': 'application/json',
'Authorization': 'Bearer' + " " + this.token //return [object object]
});
console.log('header', headers);
return this.http.post(this.env.STORES_URL,
{ name: name, description: description, phone: phone, province_id: province_id, kota_id: kota_id, address: address, logo: logo, banner: banner }, { headers: headers }
)
}
PS: I've commented each part so you can understand better where the problem comes from.
any idea?
Update
I've been playing with this code and if I use
'Authorization': 'Bearer' + " " + JSON.stringify(this.token)
I will get something like:
Authorization: Bearer {"success":{"token":"eyJ0eXAiOiJK....
Still need to go to step forward into token array {"success":{"token":"
Any idea now?**
Update 2
regarding to comment request here is how my token stores in storage (when user loggin)
login(email: String, password: String) {
return this.http.post(this.env.BASE_URL + '/login',
{ email: email, password: password }
).pipe(
tap(token => {
console.log(token);
this.storage.setItem('token', token)
.then(
() => {
console.log('Token Stored');
},
error => console.error('Error storing item', error)
);
this.token = token;
this.isLoggedIn = true;
return token;
}),
);
}