Issue
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?
Solution
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
Answered By - Jonathan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.