jrocc
jrocc

Reputation: 1346

Ionic Angular if statement being called on different page

I'm trying to make a user go to a profile set up page when they first login to their account. I'm doing this with an if statement that checks if the user's id is in a firebase database and its associated with a profile. If not the user is taken to a profile make page then once they make a profile they are sent to their profile and if they have a profile they are taken directly to their profile.

The problem is once they make a profile the profile page is still getting loaded from the if statement in the login page even after it was closed. Why is this happening?

login.ts

login(user: User){
 this.afAuth.auth.signInWithEmailAndPassword(user.email,user.password)
 .then(res => {
   //check if user has made profile if not send to profile setup page
    let user = firebase.auth().currentUser;
    if(user.emailVerified){
      this.afAuth.authState.take(1).subscribe(data => {
        let profileCollection = this.afs.collection('profiles').doc(`${data.uid}`).valueChanges();
        profileCollection.subscribe(userProfile => {
          if (userProfile == null){
            console.log(userProfile);
            this.navCtrl.setRoot('ProfileSetupPage');
          }else{
            console.log('LoginPage Load Tabs Page');
            this.navCtrl.setRoot('ProfilePage');
          }
        })
      });
    }
 }

profile-setup.ts

 createProfile(){
   if(this.profileForm.valid){
     this.afAuth.authState.take(1).subscribe(auth => {
       this.afs.collection('profiles').doc(`${auth.uid}`).set(this.profile)
         .then(res => {
           this.navCtrl.setRoot('ProfilePage');
         })
     })
   }

Upvotes: 0

Views: 408

Answers (1)

Alex Mounir
Alex Mounir

Reputation: 1241

I believe the subscribe of the login is still active and accepting events, so you need to unsubscribe in the login.ts as you redirect or in the profile-setup.ts as you initialize.

Upvotes: 1

Related Questions