1
I’m having a question with the Angular/Ionic I’m using to build an app.
I own the following:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
@Injectable()
export class UsersProvider {
private API_URL = 'http://localhost:8080/v1';
constructor(public http: HttpClient) {
}
apiHeaders() {
const headers = new HttpHeaders();
headers.set('Content-Type', 'application/json');
headers.set('Accept', 'application/json');
return headers;
}
loginUserAccount(email: string, password: string) {
let headers = this.apiHeaders();
return new Promise((resolve, reject) => {
this.http.get(this.API_URL + '/auth/login?email=' + email + '&password=' + password, { headers }).subscribe((result: any) => {
resolve(result)
},
(error) => {
reject(error.error)
})
});
}
And the response of the loginUserAccount() function I get a JSON "auth-jwt" that it is the Token of the user that contains the logged in user information and allows access to the other pages of the app.
My question is how can I set my header "X-Token" with the answer that comes from the API? Searching Google and Stackoverflow itself I read about Ionic Storage to save data and recover when you want but I don’t understand very well how it works and how it would be implemented, is there any other way to save the token or save with Ionic Storage the most correct? If Ionic Storage is better, how can I write "auth-jwt" to it?
At least in the projects that I picked up with JWT and Ionic put the token in Ionic Storage and left it recorded there afterwards when it was necessary to use it I only took from Storage and sent the request header, and when the token expired I did a refresh on it saving the new one in Storage.
– Tadeu Mansi
This is what I read and most commented to use but I haven’t found any example of how to use, have some example or how to adapt my code to use Ionic Storage and consume afterwards?
– Tiago Paza
So it is not very recommended to log in via GET use a POST, then I would do the following in your Preview I would just call the request and handle its return in your controller. After doing this would take the return of it with the token and write to the Token, your token will be saved in the memory of the app there is only you use it when you need in your requests that require login
– Tadeu Mansi