Struggling to retrieve the current user information from Firebase google login using angularfire. The issue is why angularfire won't store the current user info. Although the value of this.user
is returned within the block, it shows as null
outside of it. Looking for some guidance on this.
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FlashMessagesService } from 'angular2-flash-messages';
import { FirebaseService } from '../../services/firebase.service';
import * as firebase from 'firebase';
import { AngularFire, FirebaseAuthState } from 'angularfire2';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
public user: FirebaseAuthState;
public userProfiles: any;
public displayName: string;
constructor(
public af:AngularFire,
public flashMessage:FlashMessagesService,
private firebaseService: FirebaseService,
private router:Router
) {
this.af.auth.subscribe(user => {
if(user) {
// user logged in
this.user = user;
console.log('if');
console.log(this.user);
}
else {
// user not logged in
// this.user = ;
console.log('else');
}
})
console.log(this.user);
}
ngOnInit() {
this.af.auth.subscribe((state: FirebaseAuthState) => {
this.userProfiles = state;
this.displayName = this.userProfiles.auth.displayName;
// console.log(this.userProfiles);
});
console.log(this.firebaseService.getCurrentUser());
}
login(){
this.af.auth.login();
}
logout(){
this.af.auth.logout();
this.flashMessage.show('You are logged out',
{cssClass: 'alert-success', timeout: 3000});
}
username(){
}
}