zl程序教程

您现在的位置是:首页 >  其它

当前栏目

[AngularFire2] Auth with Firebase auth -- email

-- with email auth firebase
2023-09-14 08:59:19 时间

First, you need to enable the email auth in Firebase console.

 

Then implement the auth service:

  login(email, password) {

    return this.fromFirebaseAuthPromise(this.auth$.login({
      email, password
    },{
      method: AuthMethods.Password,
      provider: AuthProviders.Password
    }));
  }

  fromFirebaseAuthPromise(promise) {
    const subject = new Subject<any>();

    promise.then((res) => {
      subject.next(res);
      subject.complete();
    }, err => {
      subject.error(err);
      subject.complete();
    });

    return subject.asObservable();
  }

Because login method return Promise, we need to convert it to Observable. The way we do it is using 'subject'.

 

Controller:

  login(){
    const formValue = this.form.value;

    this.authService.login(formValue.email, formValue.password)
      .subscribe((res) => {
         this.router.navigate(['/home']);
      })
  }