I am currently working on retrieving the current user and attempting to assign the user values to a getter. In the constructor, I can see in the console that it is returning "email verified" as true. However, when trying to set it in the getter, I am encountering an error message that reads "Cannot read property 'emailVerified' of undefined." Below is the code snippet for reference:
import { Injectable } from "@angular/core";
import { AngularFireAuth } from "@angular/fire/auth";
import * as firebase from "firebase/app";
//import 'rxjs/add/operator/toPromise';
@Injectable({
providedIn: "root"
})
export class AuthService {
userData: any; // Store information of logged-in user
constructor(public afAuth: AngularFireAuth) {
/* Check if user is logged in and initialize to null when logged out */
this.afAuth.authState.subscribe(user => {
if (user) {
this.userData = user;
console.log(this.userData, this.userData.emailVerified)
} else {
this.userData = null;
}
})
}
// Returns true if user is logged in and email is verified
get isLoggedIn(): boolean {
const user = this.userData;
return ( user !== null && user.emailVerified !== false ) ? true : false;
}
}