Where is the best place to add a global listener initialization call in an Angular app?
Take a look at this code snippet:
export class AuthService {
constructor(
private store: Store<fromAuth.State>,
private afAuth: AngularFireAuth
) {
this.afAuth.auth.onAuthStateChanged(payload => {
if (payload) {
const user: UserBeta = {
uid: payload.uid,
displayName: payload.displayName,
email: payload.email,
emailVerified: payload.emailVerified
};
this.store.dispatch(AuthActions.authenticated({ user }));
} else {
this.store.dispatch(AuthActions.notAuthenticated());
}
});
}
I have added it to the constructor of the AuthService
, but I'm not sure if that's the right approach.
One concern I have is that this code has dependencies on both Ngrx
and AngularFireAuth
.
In this scenario, would it be better to move this logic to the FirebaseModule
(i.e. firebase.module.ts)? If so, how should the call be structured in the new location?