Reputation: 981
I am trying to access the auth object in angular2 using firebase but I have become confused. I can see the user auth credentials in the console but the auth variable seems to be null.
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>
The auth
variable appears to be null even though console.log(this.af.auth);
is showing the user credentials and therefore only Please log in
is displayed in the html.
Upvotes: 0
Views: 425
Reputation: 77
this.af.auth.login(value, {provider:AuthProviders.Password, method:AuthMethods.Password}).then((success) =>{
console.log(JSON.stringify(success));
}).catch((err) => {
console.log(err);
});
See the example https://github.com/Govinda-Wagle/Angular2-AngularFire-Blog/blob/master/src/app/Login/login.component.ts
Upvotes: 1