Remember Login with Storage Ionic

Asked

Viewed 128 times

0

Good morning to you all! I’m trying to remember the user data when sign in with Facebook, so I don’t need to always log in again.

On my login page is:

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 don’t understand is, he’s already always targeting Home Page. I even removed "Storage.set", just getting the get on Component and it keeps going to Home page, as if accepting any value. I set my rootPage to Login page, it enters it but quickly goes to Home. Can anyone help me?

1 answer

0

Your Komponent is always targeting to the Homepage. You need to include a if to check whether the 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();

  });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.