This commit is contained in:
KS Jannette
2026-03-29 16:21:48 -04:00
parent 06d7cd4169
commit 0f770742ef
5 changed files with 133 additions and 80 deletions

View File

@@ -1,54 +1,94 @@
import { createContext, useReducer, useContext } from "react";
import shopReducer, { initialState } from "./shopReducer";
import { createContext, useContext, useState, useEffect, useCallback } from "react";
import {
onAuthStateChanged,
signInWithEmailAndPassword,
createUserWithEmailAndPassword,
signOut,
} from "firebase/auth";
import { auth } from "../firebase/config";
import { getAccount } from "../api/account";
const AuthContext = createContext(initialState);
const AuthContext = createContext(undefined);
export const AuthProvider = ({ children }) => {
const [state, dispatch] = useReducer(shopReducer, initialState);
const addAuthUser = (args) => {
const updatedUser = state.user.concat(product);
updatePrice(updatedCart);
dispatch({
type: "ADD_AUTH_USER",
payload: {
args: newUser
}
});
};
const logoutAuthUser = (args) => {
const updatedUser = state.user.filter(
(currentUser) => currentUser.name !== args.name
);
updateUser(updatedCart);
dispatch({
type: "REMOVE_AUTH_USER",
payload: {
args: updatedCart
}
});
};
const value = {
authUser: state.name,
authUserTier: state.tier,
addAuthUser,
logoutAuthUser
};
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
const DEFAULT_TIER_LIMITS = {
max_addresses: 1,
max_alert_types: 1,
allowed_channels: ["email"],
};
const useAuth = () => {
const context = useContext(ShopContext);
export function AuthProvider({ children }) {
const [currentUser, setCurrentUser] = useState(null);
const [userTier, setUserTier] = useState("free");
const [tierLimits, setTierLimits] = useState(DEFAULT_TIER_LIMITS);
const [isSubscribed, setIsSubscribed] = useState(false);
const [loading, setLoading] = useState(true);
if (context === undefined) {
throw new Error("authUser must be used within AuthContext");
const fetchAccount = useCallback(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",
);
} catch {
setUserTier("free");
setTierLimits(DEFAULT_TIER_LIMITS);
setIsSubscribed(false);
}
}, []);
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, async (user) => {
setCurrentUser(user);
if (user) {
await fetchAccount();
} else {
setUserTier("free");
setTierLimits(DEFAULT_TIER_LIMITS);
setIsSubscribed(false);
}
setLoading(false);
});
return unsubscribe;
}, [fetchAccount]);
async function signup(email, password) {
const cred = await createUserWithEmailAndPassword(auth, email, password);
return cred.user;
}
async function login(email, password) {
const cred = await signInWithEmailAndPassword(auth, email, password);
return cred.user;
}
async function logout() {
await signOut(auth);
}
const value = {
currentUser,
userTier,
tierLimits,
isSubscribed,
loading,
signup,
login,
logout,
refreshAccount: fetchAccount,
};
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}
export function useAuth() {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error("useAuth must be used within an AuthProvider");
}
return context;
};
}
export default useAuth;

View File

@@ -0,0 +1,44 @@
import { createContext, useReducer, useContext } from "react";
const initialState = {
email: "",
password: "",
};
const ACTION_TYPES = {
SET_USER_PROPERTIES: "SET_USER_PROPERTIES",
CLEAR_USER_PROPERTIES: "CLEAR_USER_PROPERTIES",
};
function userPropertiesReducer(state, action) {
switch (action.type) {
case ACTION_TYPES.SET_USER_PROPERTIES:
return { ...state, ...action.payload };
case ACTION_TYPES.CLEAR_USER_PROPERTIES:
return { ...initialState };
default:
return state;
}
}
const UserPropertiesContext = createContext(undefined);
export function UserPropertiesProvider({ children }) {
const [state, dispatch] = useReducer(userPropertiesReducer, initialState);
return (
<UserPropertiesContext.Provider value={{ state, dispatch, ACTION_TYPES }}>
{children}
</UserPropertiesContext.Provider>
);
}
export function useUserProperties() {
const context = useContext(UserPropertiesContext);
if (context === undefined) {
throw new Error(
"useUserProperties must be used within a UserPropertiesProvider",
);
}
return context;
}