Resolve merge conflicts
This commit is contained in:
@@ -11,7 +11,9 @@ import AlertHistory from "./pages/alertHistory/AlertHistory";
|
||||
import Account from "./pages/user_account/Account";
|
||||
|
||||
export default function App() {
|
||||
const { currentUser, isSubscribed } = useAuth();
|
||||
const { currentUser, isSubscribed, loading } = useAuth();
|
||||
|
||||
if (loading) return null;
|
||||
|
||||
if (!currentUser) {
|
||||
return (
|
||||
|
||||
@@ -1,140 +1,94 @@
|
||||
/**
|
||||
* AuthContext - Firebase Authentication State Management
|
||||
*
|
||||
* Provides authentication state, tier info, and methods throughout the app
|
||||
*/
|
||||
|
||||
import { createContext, useContext, useEffect, useState, useCallback } from "react";
|
||||
import { createContext, useContext, useState, useEffect, useCallback } from "react";
|
||||
import {
|
||||
createUserWithEmailAndPassword,
|
||||
signInWithEmailAndPassword,
|
||||
signOut,
|
||||
onAuthStateChanged,
|
||||
onAuthStateChanged,
|
||||
signInWithEmailAndPassword,
|
||||
createUserWithEmailAndPassword,
|
||||
signOut,
|
||||
} from "firebase/auth";
|
||||
import { auth } from "../firebase/config";
|
||||
import { getAccount } from "../api/account";
|
||||
|
||||
const AuthContext = createContext();
|
||||
const AuthContext = createContext(undefined);
|
||||
|
||||
const DEFAULT_TIER_LIMITS = {
|
||||
max_addresses: 1,
|
||||
max_alert_types: 1,
|
||||
allowed_channels: ["email"],
|
||||
max_addresses: 1,
|
||||
max_alert_types: 1,
|
||||
allowed_channels: ["email"],
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to access auth context
|
||||
* @returns {Object} Auth context value
|
||||
*/
|
||||
export function useAuth() {
|
||||
const context = useContext(AuthContext);
|
||||
if (!context) {
|
||||
throw new Error("useAuth must be used within AuthProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* AuthProvider - Wraps app and provides auth state + tier info
|
||||
*/
|
||||
export function AuthProvider({ children }) {
|
||||
const [currentUser, setCurrentUser] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
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);
|
||||
|
||||
const [userTier, setUserTier] = useState("free");
|
||||
const [tierLimits, setTierLimits] = useState(DEFAULT_TIER_LIMITS);
|
||||
const [addressCount, setAddressCount] = useState(0);
|
||||
const [subscriptionStatus, setSubscriptionStatus] = useState("none");
|
||||
|
||||
const refreshAccount = useCallback(async () => {
|
||||
try {
|
||||
const data = await getAccount();
|
||||
setUserTier(data.subscription_tier || "free");
|
||||
setTierLimits(data.tier_limits || DEFAULT_TIER_LIMITS);
|
||||
setAddressCount(data.address_count || 0);
|
||||
setSubscriptionStatus(data.subscription_status || "none");
|
||||
} catch {
|
||||
// account fetch can fail during onboarding before subscription is active
|
||||
}
|
||||
}, []);
|
||||
|
||||
async function signup(email, password) {
|
||||
try {
|
||||
setError(null);
|
||||
const result = await createUserWithEmailAndPassword(
|
||||
auth,
|
||||
email,
|
||||
password,
|
||||
);
|
||||
return result.user;
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
throw err;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}, []);
|
||||
|
||||
async function login(email, password) {
|
||||
try {
|
||||
setError(null);
|
||||
const result = await signInWithEmailAndPassword(
|
||||
auth,
|
||||
email,
|
||||
password,
|
||||
);
|
||||
return result.user;
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
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 logout() {
|
||||
try {
|
||||
setError(null);
|
||||
setUserTier("free");
|
||||
setTierLimits(DEFAULT_TIER_LIMITS);
|
||||
setAddressCount(0);
|
||||
setSubscriptionStatus("none");
|
||||
await signOut(auth);
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
async function signup(email, password) {
|
||||
const cred = await createUserWithEmailAndPassword(auth, email, password);
|
||||
return cred.user;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = onAuthStateChanged(auth, (user) => {
|
||||
setCurrentUser(user);
|
||||
setLoading(false);
|
||||
if (user) {
|
||||
refreshAccount();
|
||||
}
|
||||
});
|
||||
return unsubscribe;
|
||||
}, [refreshAccount]);
|
||||
async function login(email, password) {
|
||||
const cred = await signInWithEmailAndPassword(auth, email, password);
|
||||
return cred.user;
|
||||
}
|
||||
|
||||
const isSubscribed =
|
||||
subscriptionStatus === "active" || subscriptionStatus === "trialing";
|
||||
async function logout() {
|
||||
await signOut(auth);
|
||||
}
|
||||
|
||||
const value = {
|
||||
currentUser,
|
||||
signup,
|
||||
login,
|
||||
logout,
|
||||
error,
|
||||
loading,
|
||||
userTier,
|
||||
tierLimits,
|
||||
addressCount,
|
||||
subscriptionStatus,
|
||||
isSubscribed,
|
||||
refreshAccount,
|
||||
};
|
||||
const value = {
|
||||
currentUser,
|
||||
userTier,
|
||||
tierLimits,
|
||||
isSubscribed,
|
||||
loading,
|
||||
signup,
|
||||
login,
|
||||
logout,
|
||||
refreshAccount: fetchAccount,
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={value}>
|
||||
{!loading && children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
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;
|
||||
|
||||
44
frontend/src/contexts/UserPropertiesContext.jsx
Normal file
44
frontend/src/contexts/UserPropertiesContext.jsx
Normal 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;
|
||||
}
|
||||
@@ -2,15 +2,18 @@ import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import { BrowserRouter } from "react-router-dom";
|
||||
import { AuthProvider } from "./contexts/AuthContext";
|
||||
import { UserPropertiesProvider } from "./contexts/UserPropertiesContext";
|
||||
import App from "./App";
|
||||
import "./index.css";
|
||||
|
||||
ReactDOM.createRoot(document.getElementById("root")).render(
|
||||
<React.StrictMode>
|
||||
<BrowserRouter>
|
||||
<AuthProvider>
|
||||
<App />
|
||||
</AuthProvider>
|
||||
<UserPropertiesProvider>
|
||||
<AuthProvider>
|
||||
<App />
|
||||
</AuthProvider>
|
||||
</UserPropertiesProvider>
|
||||
</BrowserRouter>
|
||||
</React.StrictMode>,
|
||||
);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState } from "react";
|
||||
import { useNavigate, useSearchParams } from "react-router-dom";
|
||||
import { useAuth } from "../../contexts/AuthContext";
|
||||
import { useUserProperties } from "../../contexts/UserPropertiesContext";
|
||||
import {
|
||||
createCheckoutSession,
|
||||
activateFreeTier,
|
||||
@@ -21,7 +22,7 @@ export default function Subscribe() {
|
||||
const success = queryParameters?.get("payment")
|
||||
const session_id = queryParameters?.get("session_id")
|
||||
|
||||
console.log('success, session_id_________________________------------>', success, session_id)
|
||||
console.log('success, session_id ------>', success, session_id)
|
||||
|
||||
function forward(success, session_id) {
|
||||
success && session_id ?
|
||||
|
||||
Reference in New Issue
Block a user