simplified auth context provider

This commit is contained in:
KS Jannette
2026-05-12 04:53:53 -04:00
parent f52f7dc89f
commit 8b2db08db1
2 changed files with 26 additions and 33 deletions

View File

@@ -11,9 +11,7 @@ import AlertHistory from "./pages/alertHistory/AlertHistory";
import Account from "./pages/user_account/Account"; import Account from "./pages/user_account/Account";
export default function App() { export default function App() {
const { currentUser, isSubscribed, loading } = useAuth(); const { currentUser, isSubscribed } = useAuth();
if (loading) return null;
if (!currentUser) { if (!currentUser) {
return ( return (

View File

@@ -1,4 +1,4 @@
import { createContext, useContext, useState, useEffect, useCallback } from "react"; import { createContext, useContext, useState, useEffect } from "react";
import { import {
onAuthStateChanged, onAuthStateChanged,
signInWithEmailAndPassword, signInWithEmailAndPassword,
@@ -24,21 +24,23 @@ export function AuthProvider({ children }) {
const [isSubscribed, setIsSubscribed] = useState(false); const [isSubscribed, setIsSubscribed] = useState(false);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const fetchAccount = useCallback(async () => { const resetAccountState = () => {
setIsSubscribed(false);
};
const fetchAccount = async () => {
try { try {
const data = await getAccount(); const data = await getAccount();
setUserTier(data.subscription_tier || "free"); setUserTier(data.subscription_tier || "free");
setTierLimits(data.tier_limits || DEFAULT_TIER_LIMITS); setTierLimits(data.tier_limits || DEFAULT_TIER_LIMITS);
setIsSubscribed( setIsSubscribed(
data.subscription_status === "active" || data.subscription_status === "active" ||
data.subscription_status === "trialing", data.subscription_status === "trialing",
); );
} catch { } catch {
setUserTier("free"); resetAccountState();
setTierLimits(DEFAULT_TIER_LIMITS);
setIsSubscribed(false);
} }
}, []); };
useEffect(() => { useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, async (user) => { const unsubscribe = onAuthStateChanged(auth, async (user) => {
@@ -46,50 +48,39 @@ export function AuthProvider({ children }) {
if (user) { if (user) {
await fetchAccount(); await fetchAccount();
} else { } else {
setUserTier("free"); resetAccountState();
setTierLimits(DEFAULT_TIER_LIMITS);
setIsSubscribed(false);
} }
setLoading(false); setLoading(false);
}); });
return unsubscribe; return () => unsubscribe();
}, [fetchAccount]); }, []);
async function signup(email, password) { const signup = (email, password) => createUserWithEmailAndPassword(auth, email, password);
const cred = await createUserWithEmailAndPassword(auth, email, password);
return cred.user;
}
async function login(email, password) { const login = (email, password) => signInWithEmailAndPassword(auth, email, password);
const cred = await signInWithEmailAndPassword(auth, email, password);
return cred.user;
}
async function logout() { const logout = () => signOut(auth);
await signOut(auth);
}
async function sendEmailVerification() { const sendEmailVerification = () => {
if (auth.currentUser) { if (auth.currentUser) {
await firebaseSendEmailVerification(auth.currentUser); return firebaseSendEmailVerification(auth.currentUser);
} }
} };
async function reloadUser() { const reloadUser = async () => {
if (auth.currentUser) { if (auth.currentUser) {
await auth.currentUser.reload(); await auth.currentUser.reload();
setCurrentUser({ ...auth.currentUser }); setCurrentUser({ ...auth.currentUser });
return auth.currentUser; return auth.currentUser;
} }
return null; return null;
} };
const value = { const value = {
currentUser, currentUser,
userTier, userTier,
tierLimits, tierLimits,
isSubscribed, isSubscribed,
loading,
signup, signup,
login, login,
logout, logout,
@@ -98,7 +89,11 @@ export function AuthProvider({ children }) {
refreshAccount: fetchAccount, refreshAccount: fetchAccount,
}; };
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; return (
<AuthContext.Provider value={value}>
{!loading && children}
</AuthContext.Provider>
);
} }
export function useAuth() { export function useAuth() {