simplified auth context provider
This commit is contained in:
@@ -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 (
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { createContext, useContext, useState, useEffect, useCallback } from "react";
|
||||
import { createContext, useContext, useState, useEffect } from "react";
|
||||
import {
|
||||
onAuthStateChanged,
|
||||
signInWithEmailAndPassword,
|
||||
@@ -24,7 +24,11 @@ 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");
|
||||
@@ -34,11 +38,9 @@ export function AuthProvider({ children }) {
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user