Calvin Care
Calvin Care

Reputation: 81

firebase currently signed-in user is returning null

As per google docs, I've tried the below method to retrieve the current user details, but i'm getting null response from firebase v9.

Below is my code

  ngOnInit(): void {
const app = initializeApp(environment.firebaseConfig);
const db = getDatabase(app);
const dbRef = ref(getDatabase(app));
const auth = getAuth();

onAuthStateChanged(auth, (user) => {
  if (user) {
    console.log(user);
    const uid = user.uid;
    console.log(uid);
  }
});
onAuthStateChanged(auth, (user) => {
  if (user) {
    console.log(user);
    const uid = user.uid;
    console.log(uid);
  }
});

}

I'm using ngx-auth-firebaseui for authentication for login in my angular app.

Its not happening after login, When I directly visits the profile page, I'm getting null for currentuser,

enter image description here

Upvotes: 0

Views: 100

Answers (1)

Calvin Care
Calvin Care

Reputation: 81

I found the solutions, this might help others

I've imported AuthProcessService from ngx-auth-firebaseui

this.auth.user$.subscribe(res => {
  if(res) {
    console.log(res);
  }
});

Now I can get the current logged in user

Upvotes: 1

Related Questions