I have an Angular pipe which checks if a user is a premium member before navigate to specific routes.
Premium Guard:
return this.afAuth.authState.pipe(
take(1),
switchMap(user => {
return this.db.getUserWithKey(user.uid)
}),
map((profile) => {
if (profile.length && profile[0].premium) {
return true
} else {
location.href = "https://url.com";
return false;
}
})
)
But as not logged in user I get always many errors in console user is null
. How could I check is an user is logged in before?
The following is not working:
return this.afAuth.authState.pipe(
take(1),
switchMap(user => {
if (!user || user == null) {
return false;
}
return this.db.getUserWithKey(user.uid)
}),
map((profile) => {
if (profile.length && profile[0].premium) {
return true
} else {
location.href = "https://url.com";
return false;
}
})
)
Do you have another idea? Thanks in advance!