Hello everyone!
First of all this is my first month with ionic, i have readed a lot of post but i cant get out this problem.
Im making a login with the jwt authentication method and the get options for sending the key and retrieve the JWT is working well, but i have a problem during the login process when i need to retrieve all the data associated with the given email.
I think my headers are ok.
I don’t understand how to pass the data in the body with the correct method.
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
There is my code:
login(cred) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Key ABCjhdlkewroirrt9845lkdf');
return new Promise(resolve => {
this.http.get(this.link + "/auth/register", {
headers: headers
})
.map(res => res.json())
.subscribe(
data => {
resolve(data)
localStorage.setItem('bearer', (data.jwt)); //where i store the jwt
console.log("auth", data);
},
error => {
this.events.publish('app:toast', JSON.parse(error._body).message);
}
)
})
}
validate(data){
return new Promise(resolve => {
let bearer = (localStorage.getItem('bearer'));
let headers = new Headers({
'content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'authorization': 'Bearer ' + bearer
});
var body = data; //(where i store the email) ;
/**
the data structure is {email: "jsmith@aol.com"}
**/
var options = new RequestOptions({ headers: headers });
return this.http.post(this.link + "/getguest" , body, options)
.subscribe(response => {
if (response) {
resolve(response);
} else {
resolve(false);
}
}, error => {
resolve(false);
this.events.publish('app:toast', "Error while trying to load data");
}
);
With the login() im going to make the call to retrieve the jwt, then i store it in the localStorage e use in the validate() function where with the jwt and the email value im going to ask the server to search the user data associated with the email.
But when i make the http.post call the server return me Missing Parameters. The body data is already an object and i dont need to converto to a json.
Sorry if the english is not perfect .
I hope somehow can help me
Thanks in advance