I have the following service:
@Injectable({
providedIn: 'root'
})
export class AuthService {
user: Observable<User>;
checkEmailInterval: any;
constructor(
private afAuth: AngularFireAuth,
private afStore: AngularFirestore,
private router: Router
) {
this.user = this.afAuth.authState.pipe(
switchMap(user => {
if (user) {
console.log(user);
if (!user.emailVerified) {
console.log(this.checkEmailInterval);
clearInterval(this.checkEmailInterval);
this.checkEmailInterval = setInterval(this.checkEmailVerified.bind(this), 5000);
}
return this.afStore.doc<User>(`users/${user.uid}`).valueChanges();
}
else {
console.log("NO USER");
return of(null);
}
})
);
}
Its constructor is called twice whenever I open the application. Is this expected behavior? I thought maybe it was calling the constructor for every component that it was injected into, but I'm using the service in 3 components.
I'm really not sure what part of my code to share except this. Any ideas?