我试图使用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
。
答案 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);
});