Remember Login with Storage Ionic

0

Good morning everyone! I'm trying to remember the user's data when I sign in with Facebook, so I do not need to log in again every time.

On my login page you are:

loginWithFB(userData) {

 this.fb.login(['email', 'public_profile']).then((response: FacebookLoginResponse) => {
   this.fb.api('me?fields=id,name,email,first_name,picture.width(720).height(720).as(picture_small)', []).then(profile => {
     this.userData = { email: profile['email'], first_name: profile['first_name'], picture: profile['picture_small']['data']['url'], username: profile['name'] }

     // Set user to storage
     this.nativeStorage.set('fbstorage', this.userData)

     if (this.userData != "") {
       this.navCtrl.setRoot(HomePage, { 'logfb': this.userData })
       this.events.publish('loginfacebook', this.userData, Date.now());
       //  this.navCtrl.push(HomePage, {
       //   'logfb': this.userData
       // });
     } else {
       let toast = this.toastCtrl.create({ duration: 3000, position: 'bottom' });
       toast.setMessage('Não foi possível logar com Facebook');
       toast.present();
     }
   })

 });

}

And in my Component

initializeApp() {
this.platform.ready().then(() => {
  // Here we will check if the user is already logged in
  // because we don't want to ask users to log in each time they open the app
  let env = this;
  this.nativeStorage.get('fbstorage')
    .then(function (data) {
      // user is previously logged and we have his data
      // we will let him access the app
      env.nav.setRoot(HomePage);
      env.splashScreen.hide();
    }, function (error) {
      //we don't have the user data so we will ask him to log in
      env.nav.setRoot(LogoPage);
      env.splashScreen.hide();
    });
  this.statusBar.styleDefault();

});
}

initializeApp () is being loaded at the beginning of the constructor. What I do not understand is: he is already always directing to the Home Page. I got to remove the "storage.set", just getting the get in the component and it keeps going to the home page, as if accepting any value. I set my rootPage to Login page, it goes into it but quickly goes to Home. Can someone help me?

    
asked by anonymous 14.12.2018 / 14:34

1 answer

0

Your component is always pointing to HomePage. You must include if to check that fbstorage is not empty.

initializeApp() {
  this.platform.ready().then(() => {
    let env = this;
    this.nativeStorage.get('fbstorage')
    .then(function (data) {
      if(data){
        env.nav.setRoot(HomePage);
        env.splashScreen.hide();
      }
      else{
        env.nav.setRoot(LogoPage);
      }
    }, function (error) {
      env.nav.setRoot(LogoPage);
      env.splashScreen.hide();
    });
    this.statusBar.styleDefault();

  });
}
    
15.12.2018 / 08:20