Home > Blockchain >  AngularFire Auth Persistence cannot be invoked without 'new'
AngularFire Auth Persistence cannot be invoked without 'new'

Time:09-23

I'm trying to impliment auth with persistance in angularfire with this code:

constructor(private auth: Auth, private router: Router) {
    if (auth.currentUser) this.router.navigate(this.redirect);
  }

  async loginWithGoogle() {
    const provider = new GoogleAuthProvider();

    try {
      await setPersistence(this.auth, browserLocalPersistence);
      await signInWithPopup(this.auth, provider);
      this.message = 'Sign in successful';
      await this.router.navigate(this.redirect);
    } catch (error: any) {
      console.error(error);
      if (error.code) {
        this.message = `${error.code}: ${error.message}`;
      } else {
        this.message = 'There was a problem signing in. Please try again.';
      }
    }
  }

However, i always get this error, no matter the placement of the setPersistence method:

Class constructor BrowserLocalPersistence cannot be invoked without 'new'

I followed the docs (https://firebase.google.com/docs/auth/web/auth-state-persistence) to the T; what am i doing wrong?

I am using Angular 13, Angularfire 7 and Firebase 9.

CodePudding user response:

I strongly recommend only calling setPersistence if you know you have a specific use-case that requires it. On most browser Firebase will already persist the auth state between reloads, without calling setPersistence.

  • Related