0

I'm logging in a user using signInWithEmailAndPassword() in Firebase version 9.

I'm also setting a user context after a user is logged in:

export const AuthContext = React.createContext();

export const AuthProvider = ({ children }) => {
  const [currentUser, setCurrentUser] = useState(null);

  useEffect(() => {
    const unsubscribe = onAuthStateChanged( auth, (user) => {
      setCurrentUser(user)
    });
    return () => {
      unsubscribe()
    }
  }, []);

  return (
    <AuthContext.Provider
      value={{
        currentUser
      }}
    >
      {children}
    </AuthContext.Provider>
  );
};

Finally I'm wrapping my app and routes on the auth context:

import { AuthProvider } from "./Auth";

const App = () => {
  return (
    <AuthProvider>
      <Router>
        <div>
          <Route exact path="/" component={Home} />
          <Route exact path="/login" component={Login} />
          <Route exact path="/signup" component={SignUp} />
        </div>
      </Router>
    </AuthProvider>
  );
};

export default App;

Everything works fine. I am able to log in and navigate through any private/protected pages. I can access the user context from any page. I can refresh the page in the browser and I am still logged in. I can log out using Firebase signOut() and log back in. All is good.

HOWEVER... where does Firebase and React store the user login? I read that Firebase stores it in Session Storage by default. But when checking Session Storage or Local Storage in the console, there isn't anything saved.

enter image description here

My guess is that Firebase handles it all through onAuthStateChanged() but it is still very mysterious to me... I'm not certain if my code is secure for authentication.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
CyberJ
  • 1,018
  • 1
  • 11
  • 24
  • 2
    It's stored in `IndexedDB` right below session storage. Also checkout [this discussion](https://groups.google.com/g/firebase-talk/c/wgSvjniKPQI) – Dharmaraj Nov 20 '22 at 13:54
  • @Dharmaraj very interesting! Do you know if this is secure as is? – CyberJ Nov 20 '22 at 13:57
  • 1
    Unless someone has physical access to your device and can copy the tokens, everything looks perfectly fine to me. (provided user does not run any malicious scripts in the console). – Dharmaraj Nov 20 '22 at 13:59

0 Answers0