访问AngularFire2中的Auth对象

时间:2016-08-31 11:18:38

标签: angular firebase firebase-authentication angularfire2

我试图使用firebase访问angular2中的auth对象,但我已经感到困惑。我可以在控制台中看到用户身份验证凭据,但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);变量也显示为空,因此html中仅显示Please log in

1 个答案:

答案 0 :(得分: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