0

我正在尝试使用 firebase 访问 angular2 中的 auth 对象,但我感到困惑。我可以在控制台中看到用户身份验证凭据,但身份验证变量似乎为空。

app.component.ts

import { Component } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
import { AuthProviders, AuthMethods } from 'angularfire2';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

    title = 'app works!';

    constructor(public af: AngularFire) {

    this.af.auth.subscribe(auth => console.log(auth)); //nothing in the console

    console.log(this.af.auth); // shows AngularFireAuth object with user credentials but says it is private

  }
          login() {
                console.log("logging in...");
                this.af.auth.login({
                        provider: AuthProviders.Google,
                        method: AuthMethods.Popup,
                });
              }

}

app.component.html

<div *ngIf="auth">You are logged in</div>
<div *ngIf="!auth">Please log in</div>

auth即使显示用户凭据,该变量也似乎为空console.log(this.af.auth);,因此仅Please log in显示在 html 中。

4

1 回答 1

1
 this.af.auth.login(value, {provider:AuthProviders.Password, method:AuthMethods.Password}).then((success) =>{
        console.log(JSON.stringify(success));
    }).catch((err) => {
        console.log(err);
    });

请参阅示例 https://github.com/Govinda-Wagle/Angular2-AngularFire-Blog/blob/master/src/app/Login/login.component.ts

于 2017-01-20T04:28:42.140 回答