Using Google OAuth in Angular 12 with @angular/fire@^7.0.0 firebase@^9.0.0

After imports angular/file modules in app.module.ts

import { provideFirebaseApp, getApp, initializeApp } from '@angular/fire/app';
import { getFirestore, provideFirestore } from '@angular/fire/firestore';

@NgModule({
  imports: [
    provideFirebaseApp(() => initializeApp({ ... })),
    provideFirestore(() => getFirestore()),
  ],
  ...
})
export class AppModule { }

How can I manage Auth services on a lazy featured module?

Upvotes: 0

Views: 1482

Answers (1)

Jonathan
Jonathan

Reputation: 4699

UPDATE: I wrote an article about it: Angular 12 with Firebase 9


I don't think they've updated their docs yet. I had to figure it out by trial and error:

app.module.ts

provideAuth(() => getAuth()),

auth.service.ts

import {
  Auth,
  signOut,
  signInWithPopup,
  GoogleAuthProvider,
  user
} from '@angular/fire/auth';

...

export class AuthService {

  user$: Observable<any>;

  constructor(private auth: Auth) {
    this.user$ = user(this.auth);
  }

  logout(): void {
    signOut(this.auth);
  }

  login(): void {
    signInWithPopup(this.auth, new GoogleAuthProvider);
  }

  async isLoggedIn(): Promise<boolean> {

    // only use in code, use observable in template
    return !! await this.user$.pipe(take(1)).toPromise();
  }

}

J

Upvotes: 10

Related Questions