Hello friends, my doubts are kind of simple for many, but for me this is complicated. I wanted to do a check if the token that is stored in the localstorage is the same or if it exists in my database. I'm using Ionic 3, and using Wordpress wp-rest api as backend and using a jwt plugin to do the authentication.
If anyone can guide me or show the way how to solve this .. Check if the token or user exists on my system if there is no remove the localstorage token.
Thank you so much.
Follow my provider to do the User:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import 'rxjs/add/operator/toPromise';
import { Injectable } from '@angular/core';
import { Api } from '../api/api';
import { Storage } from '@ionic/storage';
import { environment } from '../../environment';
@Injectable()
export class User {
private API_URL: string = 'https://api.corretoradinamica.com/wp-json/wp/v2/';
public Users: any = [];
api_url = environment.site_url + environment.jwt_url;
// _user: any;
public storage: Storage;
constructor(public http: HttpClient, public api: Api) { }
postLogin(username, password) {
let data = {
username: username,
password: password,
};
let headers = new HttpHeaders();
headers.set('Contetn-Type', 'application/json');
return this.http.post(this.api_url, data, { headers: headers })
}
get(query: string = '') {
return this.http.get(this.API_URL + query);
}
getUsers() {
this.get('users').subscribe((data => {
this.Users = data;
}));
}
}
Login.ts
import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { NavController, ToastController, NavParams, MenuController} from 'ionic-angular';
import { MainPage } from '../';
import { User } from './../../providers/user/user';
@Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
// The account fields for the login form.
// If you're using the username field with or without email, make
// sure to add it to the type
// account: { email: string, password: string } = {
// email: '[email protected]',
// password: 'test',
// };
username;
password;
// Our translated text strings
private loginErrorString: string;
private loginSuccess: string;
constructor(public navCtrl: NavController,
public User: User,
public toastCtrl: ToastController,
public translateService: TranslateService,
public navParams: NavParams,
public menu: MenuController) {
//mensagem Erro login
this.translateService.get('LOGIN_ERROR').subscribe((value) => {
this.loginErrorString = value;
});
//mensagem login ok
this.translateService.get('LOGIN_SUCCESS').subscribe((value) =>{
this.loginSuccess = value;
});
if(localStorage.getItem('wpToken')){
this.navCtrl.setRoot(MainPage);
}
}
doLogin() {
console.log(this.username, this.password);
this.User.postLogin(this.username, this.password)
.subscribe((resp) => {
console.log(resp);
//localStorage.setItem('wpToken', JSON.stringify(resp));
localStorage.setItem('wpToken', JSON.stringify(resp));
let toast = this.toastCtrl.create({
message: this.loginSuccess,
duration: 5000,
position: 'top'
});
this.navCtrl.push(MainPage);
}, (err) => {
this.navCtrl.push(LoginPage);
// Unable to log in
let toast = this.toastCtrl.create({
message: this.loginErrorString,
duration: 5000,
position: 'top'
});
toast.present();
});
}
userIsLogged(){
console.log(this.username, this.password);
}
ionViewDidEnter() {
// the root left menu should be disabled on the tutorial page
this.menu.enable(false);
}
ionViewWillLeave() {
// enable the root left menu when leaving the tutorial page
this.menu.enable(true);
}
}