fixed login race condition

This commit is contained in:
KS Jannette
2026-05-12 05:14:12 -04:00
parent 8b2db08db1
commit c4371f7886

View File

@@ -1,4 +1,4 @@
import { createContext, useContext, useState, useEffect } from "react"; import { createContext, useContext, useSyncExternalStore } from "react";
import { import {
onAuthStateChanged, onAuthStateChanged,
signInWithEmailAndPassword, signInWithEmailAndPassword,
@@ -17,48 +17,67 @@ const DEFAULT_TIER_LIMITS = {
allowed_channels: ["email"], allowed_channels: ["email"],
}; };
export function AuthProvider({ children }) { // External auth store - lives outside React
const [currentUser, setCurrentUser] = useState(null); const createAuthStore = () => {
const [userTier, setUserTier] = useState("free"); let state = {
const [tierLimits, setTierLimits] = useState(DEFAULT_TIER_LIMITS); currentUser: null,
const [isSubscribed, setIsSubscribed] = useState(false); userTier: "free",
const [loading, setLoading] = useState(true); tierLimits: DEFAULT_TIER_LIMITS,
isSubscribed: false,
loading: true,
};
const listeners = new Set();
const resetAccountState = () => { const notify = () => listeners.forEach((fn) => fn());
setIsSubscribed(false);
const setState = (partial) => {
state = { ...state, ...partial };
notify();
}; };
const fetchAccount = async () => { const fetchAccount = async () => {
try { try {
const data = await getAccount(); const data = await getAccount();
setUserTier(data.subscription_tier || "free"); setState({
setTierLimits(data.tier_limits || DEFAULT_TIER_LIMITS); userTier: data.subscription_tier || "free",
setIsSubscribed( tierLimits: data.tier_limits || DEFAULT_TIER_LIMITS,
data.subscription_status === "active" || isSubscribed:
data.subscription_status === "trialing", data.subscription_status === "active" ||
); data.subscription_status === "trialing",
});
} catch { } catch {
resetAccountState(); setState({ isSubscribed: false });
} }
}; };
useEffect(() => { // Set up Firebase listener once, outside React
const unsubscribe = onAuthStateChanged(auth, async (user) => { onAuthStateChanged(auth, async (user) => {
setCurrentUser(user); setState({ loading: true, currentUser: user });
if (user) { if (user) {
await fetchAccount(); await fetchAccount();
} else { } else {
resetAccountState(); setState({ isSubscribed: false, userTier: "free", tierLimits: DEFAULT_TIER_LIMITS });
} }
setLoading(false); setState({ loading: false });
}); });
return () => unsubscribe();
}, []); return {
subscribe: (listener) => {
listeners.add(listener);
return () => listeners.delete(listener);
},
getSnapshot: () => state,
refreshAccount: fetchAccount,
};
};
const authStore = createAuthStore();
export function AuthProvider({ children }) {
const state = useSyncExternalStore(authStore.subscribe, authStore.getSnapshot);
const signup = (email, password) => createUserWithEmailAndPassword(auth, email, password); const signup = (email, password) => createUserWithEmailAndPassword(auth, email, password);
const login = (email, password) => signInWithEmailAndPassword(auth, email, password); const login = (email, password) => signInWithEmailAndPassword(auth, email, password);
const logout = () => signOut(auth); const logout = () => signOut(auth);
const sendEmailVerification = () => { const sendEmailVerification = () => {
@@ -70,28 +89,24 @@ export function AuthProvider({ children }) {
const reloadUser = async () => { const reloadUser = async () => {
if (auth.currentUser) { if (auth.currentUser) {
await auth.currentUser.reload(); await auth.currentUser.reload();
setCurrentUser({ ...auth.currentUser });
return auth.currentUser; return auth.currentUser;
} }
return null; return null;
}; };
const value = { const value = {
currentUser, ...state,
userTier,
tierLimits,
isSubscribed,
signup, signup,
login, login,
logout, logout,
sendEmailVerification, sendEmailVerification,
reloadUser, reloadUser,
refreshAccount: fetchAccount, refreshAccount: authStore.refreshAccount,
}; };
return ( return (
<AuthContext.Provider value={value}> <AuthContext.Provider value={value}>
{!loading && children} {!state.loading && children}
</AuthContext.Provider> </AuthContext.Provider>
); );
} }