0

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?

Jesper
  • 2,644
  • 4
  • 30
  • 65
  • Take a look at [this answer](https://stackoverflow.com/a/51502913/1009922). – ConnorsFan Dec 08 '18 at 01:36
  • @ConnorsFan I have seen that one. I actually had the service in my Core module as well, but I removed it and it didn't do anything. – Jesper Dec 08 '18 at 01:38
  • Does `AuthService` appear in any `providers` list in your project? – ConnorsFan Dec 08 '18 at 01:39
  • @ConnorsFan I just went through it again and I can't find it anywhere. – Jesper Dec 08 '18 at 01:45
  • For anyone else ending up here. Check for circular dependencies. I was injecting my service in another service which caused it to construct twice. – span May 27 '19 at 11:10

1 Answers1

-2

You can try moving this logic inside ngOnInit. As per angular all injectable services using providedIn property as 'root' will be considered as singleton class and it will be instantiated once.

@Injectable({
  providedIn: 'root'
})

export class AuthService implements OnInit {

  user: Observable<User>;
  checkEmailInterval: any;

  constructor(
    private afAuth: AngularFireAuth,
    private afStore: AngularFirestore,
    private router: Router
  ) {}

  ngOnInit(){
    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);
        }
      })
    );
  }
}
Suresh Kumar Ariya
  • 9,516
  • 1
  • 18
  • 27
  • 1
    This does not answer the question, and additionally, services don't have an ngOnInit hook afaik – Paul H Nov 03 '21 at 15:47