|
|
|
@@ -1,4 +1,4 @@
|
|
|
|
import { createContext, useContext, useState, useEffect, useCallback } from "react";
|
|
|
|
import { createContext, useContext, useSyncExternalStore } from "react";
|
|
|
|
import {
|
|
|
|
import {
|
|
|
|
onAuthStateChanged,
|
|
|
|
onAuthStateChanged,
|
|
|
|
signInWithEmailAndPassword,
|
|
|
|
signInWithEmailAndPassword,
|
|
|
|
@@ -17,88 +17,98 @@ 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 fetchAccount = useCallback(async () => {
|
|
|
|
const notify = () => listeners.forEach((fn) => fn());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const setState = (partial) => {
|
|
|
|
|
|
|
|
state = { ...state, ...partial };
|
|
|
|
|
|
|
|
notify();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 === "active" ||
|
|
|
|
data.subscription_status === "trialing",
|
|
|
|
data.subscription_status === "trialing",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
} catch {
|
|
|
|
setUserTier("free");
|
|
|
|
setState({ isSubscribed: false });
|
|
|
|
setTierLimits(DEFAULT_TIER_LIMITS);
|
|
|
|
|
|
|
|
setIsSubscribed(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Set up Firebase listener once, outside of 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 {
|
|
|
|
setUserTier("free");
|
|
|
|
setState({ isSubscribed: false, userTier: "free", tierLimits: DEFAULT_TIER_LIMITS });
|
|
|
|
setTierLimits(DEFAULT_TIER_LIMITS);
|
|
|
|
}
|
|
|
|
setIsSubscribed(false);
|
|
|
|
setState({ loading: false });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setLoading(false);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return unsubscribe;
|
|
|
|
|
|
|
|
}, [fetchAccount]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function signup(email, password) {
|
|
|
|
return {
|
|
|
|
const cred = await createUserWithEmailAndPassword(auth, email, password);
|
|
|
|
subscribe: (listener) => {
|
|
|
|
return cred.user;
|
|
|
|
listeners.add(listener);
|
|
|
|
}
|
|
|
|
return () => listeners.delete(listener);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
getSnapshot: () => state,
|
|
|
|
|
|
|
|
refreshAccount: fetchAccount,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async function login(email, password) {
|
|
|
|
const authStore = createAuthStore();
|
|
|
|
const cred = await signInWithEmailAndPassword(auth, email, password);
|
|
|
|
|
|
|
|
return cred.user;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function logout() {
|
|
|
|
export function AuthProvider({ children }) {
|
|
|
|
await signOut(auth);
|
|
|
|
const state = useSyncExternalStore(authStore.subscribe, authStore.getSnapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function sendEmailVerification() {
|
|
|
|
const signup = (email, password) => createUserWithEmailAndPassword(auth, email, password);
|
|
|
|
|
|
|
|
const login = (email, password) => signInWithEmailAndPassword(auth, email, password);
|
|
|
|
|
|
|
|
const logout = () => signOut(auth);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 });
|
|
|
|
|
|
|
|
return auth.currentUser;
|
|
|
|
return auth.currentUser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const value = {
|
|
|
|
const value = {
|
|
|
|
currentUser,
|
|
|
|
...state,
|
|
|
|
userTier,
|
|
|
|
|
|
|
|
tierLimits,
|
|
|
|
|
|
|
|
isSubscribed,
|
|
|
|
|
|
|
|
loading,
|
|
|
|
|
|
|
|
signup,
|
|
|
|
signup,
|
|
|
|
login,
|
|
|
|
login,
|
|
|
|
logout,
|
|
|
|
logout,
|
|
|
|
sendEmailVerification,
|
|
|
|
sendEmailVerification,
|
|
|
|
reloadUser,
|
|
|
|
reloadUser,
|
|
|
|
refreshAccount: fetchAccount,
|
|
|
|
refreshAccount: authStore.refreshAccount,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
|
|
|
return (
|
|
|
|
|
|
|
|
<AuthContext.Provider value={value}>
|
|
|
|
|
|
|
|
{!state.loading && children}
|
|
|
|
|
|
|
|
</AuthContext.Provider>
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function useAuth() {
|
|
|
|
export function useAuth() {
|
|
|
|
|