在 Angular 12 中使用 Google OAuth 和 @angular/fire@^7.0.0 firebase@^9.0.0 [英] Using Google OAuth in Angular 12 with @angular/fire@^7.0.0 firebase@^9.0.0

查看:28
本文介绍了在 Angular 12 中使用 Google OAuth 和 @angular/fire@^7.0.0 firebase@^9.0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

我认为他们还没有更新他们的文档.我不得不通过反复试验来弄清楚:

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

这篇关于在 Angular 12 中使用 Google OAuth 和 @angular/fire@^7.0.0 firebase@^9.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆