Problems With Authentication Flow In Firebase App - After Login, Active User Initially Returns As Null

- 1 answer

I have a React/Firebase app where I'm extracting the current active user from firebase, and passing it to the rest of the app as context.
The user is set to context in the FirebaseContext.js file, and the problem is this: after I log in and console log the user object, it is logged as null. However, when I make a change in the FirebaseContext.js file and save it, the user object returns the correct object.
It's as if the code in FirebaseContext.js is not executed on login, and I'm not sure why. My understanding is that once a user logs in, their log in status is available to the entire app - or perhaps I've misunderstood. Suggestions on how to get login to work?

The structure of the project is roughly as follows:

|      |_Header.js
|   | 
|   |_login.js


import { useEffect,useState } from 'react'
import { auth } from "./firebaseConfig";
import { createContext } from "react";

export const FirebaseContext = createContext()

const user = auth.currentUser

console.log(`context  user: ${user}`)

const FirebaseContextProvider = (props) => {
            <FirebaseContext.Provider value = {{user}}>

export default FirebaseContextProvider

Here's the login component Login.js

import { useState,useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth'

const Login = () => {

    const auth = getAuth()
    const user = auth.currentUser
    console.log(`this is user login ${user}`)

    const [{ email, password },setFormDetails] = useState({ email:'', password:''})
    const navigate = useNavigate()

    const handleChange = (e) => {
        const { name, value } =
        setFormDetails(prevForm => ({
            [name]: value

const handleSubmit =   (e) => {
    const auth = getAuth()
    .then(() => {
        console.log('logged in')
    .catch(err => {

    useEffect(() => {
        document.title = 'Login - Wellyadvisor'

        <div className = 'signup-container'>
            <form onSubmit = {handleSubmit} method = 'POST'>
                    type = 'text'
                    placeholder = 'Email'
                    aria-label = 'Enter your email'
                    name = 'email'
                    onChange = {handleChange}
                    type = 'password'
                    placeholder = 'Password'
                    aria-label = 'Enter your password'
                    name = 'password'
                    onChange = {handleChange}
                <button type ='submit'>

export default Login


When you run:

const user = auth.currentUser

That is a synchronous calls that simply returns the value of the property when that line of code executes.

But the user's authentication state is an interactive process with many moving parts, such as your call to signInWithEmailAndPassword, but also the SDK itself, which automatically refreshes the sign-in state every hour, and signs the user out when for example you've disabled their account.

For this reason you should use an auth state listener, as shown in the first code sample in the documentation on getting the current user:

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    var uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...

If you use this approach, and store the user in the state, you can simply refresh the state when the login state changes, which will then reactively update the UI.