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";
export default function App() {
const { currentUser, isSubscribed, loading } = useAuth();
if (loading) return null;
const { currentUser, isSubscribed } = useAuth();
if (!currentUser) {
return (

View File

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