One challenge I'm facing is redirecting users to a profile setup page when they first log in. To achieve this, I have implemented an if statement in my code that checks if the user's id exists in a firebase database and is associated with a profile. If not, the user is directed to a profile creation page. Once they create a profile, they are then taken to their profile. However, there seems to be an issue where the profile page continues to load even after the login process has been completed and 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');
})
})
}