I'm pretty new to Angular..
I have a AuthService in my web app that authenticate users via Angularfire2 (firebase) and insert the email and a flag (indicating whether user filled the profile data form) into a firebase database.
My route guard checks whether the user is authenticated and whether user filled in the the profile data form if not, i redirect to the profile data form. If the user already completed the form, i re direct to hope page.
I obtain whether the user completed the basic data form via UserData service:
constructor(private afd: AngularFireDatabase, private authService: AuthService) {
}
isProfileComplete (): Observable<any> {
return this.afd.list(`/users/${this.authService.uid}`)
.snapshotChanges()
}
Then in the authGuard i do the logic to direct the user:
constructor(private authService: AuthService,
private router: Router,
private usrData: UserDataService) {}
canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.authService.isAuthenticated()) {
console.log(this.usrData.isProfileComplete())
this.usrData.isProfileComplete()
.map(userInfos => {
console.log(userInfos)
userInfos.forEach(item => {
console.log(userInfos)
if ((item.payload.key === 'profileComplete') && (item.payload.val() === true)) {
console.log('user is logged in & data form completed')
return true;
} else if ((item.payload.key === 'profileComplete') && (item.payload.val() !== true)) {
this.router.navigate(['data_form']);
console.log('user logged in not completed the form')
return false;
}
})
})
} else {
this.router.navigate(['login']);
console.log('user is not logged in ')
return false;
}
}
I think the code is not reaching the map section of the observable to read the values for the key profileComplete
Could someone kindly point me at the right direction please?
Thank you