I have the following authentication logic
initAuthListener() {
this.afAuth.authState.subscribe(user => {
if (user) {
this.store.dispatch(new Auth.SetAuthenticated());
this.router.navigate(['/training']);
} else {
this.trainingServices.cancelSubscriptions();
this.store.dispatch(new Auth.SetUnauthenticated());
this.router.navigate(['/login']);
}
});
}
As the app.component was
ngOnInit(): void {
this.authService.initAuthListener();
}
Every first call on the page would go to / login, so I passed the verification to training.component, which is where you need to have authorization.
Is it a bad practice? What would be my alternative?
I modified it as follows, apparently it's okay:
initAuthListener() {
this.afAuth.authState.subscribe(user => {
if (user) {
this.store.dispatch(new Auth.SetAuthenticated());
this.router.navigate(['/training']);
} else {
if((this.ROTAS_LIBERADAS.includes(this.router.url))){
this.trainingServices.cancelSubscriptions();
this.store.dispatch(new Auth.SetUnauthenticated());
}else{
this.trainingServices.cancelSubscriptions();
this.store.dispatch(new Auth.SetUnauthenticated());
this.router.navigate(['/login']);
}
}
});
}