diff --git a/client/src/SubscriptionPlans.jsx b/client/src/SubscriptionPlans.jsx index 7e528eb..4a9e3ee 100644 --- a/client/src/SubscriptionPlans.jsx +++ b/client/src/SubscriptionPlans.jsx @@ -1,8 +1,10 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import axios from 'axios'; import { loadStripe } from '@stripe/stripe-js'; import { url } from './components/Urls'; +import Cookies from 'js-cookie'; +import { decryptValue, encryptionKey } from './components/hashes'; // Asegúrate de que estas variables estén definidas en tu archivo .env o en variables de Vercel const stripePromise = loadStripe(import.meta.env.VITE_STRIPE_PUBLIC_KEY); @@ -17,11 +19,27 @@ const plans = [ const SubscriptionPlans = () => { const navigate = useNavigate(); + const [isLoggedIn, setIsLoggedIn] = useState(false); + + useEffect(() => { + // Check if user is logged in + if (Cookies.get('$3s1.4')) { + const session = decryptValue(Cookies.get('$3s1.4'), encryptionKey); + if (session) { + setIsLoggedIn(true); + } + } + }, []); const handleSelectPlan = async (priceId) => { + if (!isLoggedIn) { + navigate('/login'); + return; + } + const stripe = await stripePromise; try { - const response = await axios.post(`${url}/stripe/create-checkout-session`, {priceId }); + const response = await axios.post(`${url}/stripe/create-checkout-session`, { priceId }); const sessionId = response.data.id; const { error } = await stripe.redirectToCheckout({ sessionId }); if (error) { @@ -40,7 +58,7 @@ const SubscriptionPlans = () => {

{plan.name}

{plan.price}

-
))}