From 1cc47a416ae5ab8240fc34198278dc188e207366 Mon Sep 17 00:00:00 2001 From: haseebzaki-07 Date: Thu, 7 Nov 2024 21:09:16 +0530 Subject: [PATCH] add dynamic Api calls --- frontend/src/components/EmailVerification.jsx | 6 +++++- frontend/src/components/LoginPage.jsx | 8 ++++++-- frontend/src/components/Profile.jsx | 8 ++++++-- frontend/src/components/SignUpPage.jsx | 7 +++++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/EmailVerification.jsx b/frontend/src/components/EmailVerification.jsx index fe7cc3fa..3b2bee33 100644 --- a/frontend/src/components/EmailVerification.jsx +++ b/frontend/src/components/EmailVerification.jsx @@ -8,10 +8,14 @@ const AccountVerificationPage = () => { const [email, setEmail] = useState(""); const [otp, setOtp] = useState(""); const [isVerified, setIsVerified] = useState(false); + const ApiUrl = process.env.NODE_ENV === 'production' + ? 'https://agrotech-ai-11j3.onrender.com' + : 'http://localhost:8080'; + const handleVerifyAccount = async () => { try { - await axios.post("https://agro-tech-ai-backend-teal.vercel.app/auth/verify-emailotp", { email, otp }); + await axios.post(`${ApiUrl}/auth/verify-emailotp`, { email, otp }); toast.success("Account verified successfully. Redirecting to login..."); setTimeout(() => setIsVerified(true), 2000); // Redirect after a short delay } catch (error) { diff --git a/frontend/src/components/LoginPage.jsx b/frontend/src/components/LoginPage.jsx index 036e9853..b31c187d 100644 --- a/frontend/src/components/LoginPage.jsx +++ b/frontend/src/components/LoginPage.jsx @@ -14,13 +14,17 @@ const LoginPage = () => { const [showPassword, setShowPassword] = useState(false); const [rememberMe, setRememberMe] = useState(false); const { isLoggedIn, login } = useAuth(); + const ApiUrl = process.env.NODE_ENV === 'production' + ? 'https://agrotech-ai-11j3.onrender.com' + : 'http://localhost:8080'; + // Handle standard email/password login const handleSignIn = async (e) => { e.preventDefault(); try { const response = await axios.post( - "https://agro-tech-ai-backend-teal.vercel.app/auth/signin", + `${ApiUrl}/auth/signin`, { email, password, @@ -36,7 +40,7 @@ const LoginPage = () => { // Google Login handler const handleGoogleSignIn = () => { - window.location.href = "https://agro-tech-ai-backend-teal.vercel.app/auth/google"; + window.location.href = `${ApiUrl}/auth/google`; }; if (isLoggedIn) { diff --git a/frontend/src/components/Profile.jsx b/frontend/src/components/Profile.jsx index 59e763ea..3e7c3092 100644 --- a/frontend/src/components/Profile.jsx +++ b/frontend/src/components/Profile.jsx @@ -14,12 +14,16 @@ const Profile = () => { const [profilePicture, setProfilePicture] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); + const ApiUrl = process.env.NODE_ENV === 'production' + ? 'https://agrotech-ai-11j3.onrender.com' + : 'http://localhost:8080'; + useEffect(() => { const token = localStorage.getItem('auth'); if (token) { const { userId } = JSON.parse(atob(token.split('.')[1])); - axios.get(`https://agro-tech-ai-backend-teal.vercel.app/api/users/${userId}`, { + axios.get(`${ApiUrl}/api/users/${userId}`, { headers: { Authorization: `Bearer ${token}`, } @@ -56,7 +60,7 @@ const Profile = () => { const token = localStorage.getItem('auth'); const { userId } = JSON.parse(atob(token.split('.')[1])); - axios.put(`https://agro-tech-ai-backend-teal.vercel.app/api/users/${userId}`, formData, { + axios.put(`${ApiUrl}/api/users/${userId}`, formData, { headers: { Authorization: `Bearer ${token}`, } diff --git a/frontend/src/components/SignUpPage.jsx b/frontend/src/components/SignUpPage.jsx index 20a07ff9..3925f435 100644 --- a/frontend/src/components/SignUpPage.jsx +++ b/frontend/src/components/SignUpPage.jsx @@ -18,6 +18,9 @@ const SignUpPage = () => { const [isNumber, setIsNumber] = useState(false); const [isSpecialChar, setIsSpecialChar] = useState(false); const [isMinLength, setIsMinLength] = useState(false); + const ApiUrl = process.env.NODE_ENV === 'production' + ? 'https://agrotech-ai-11j3.onrender.com' + : 'http://localhost:8080'; const validatePassword = (input) => { setPassword(input); @@ -52,7 +55,7 @@ const SignUpPage = () => { try { const response = await axios.post( - "https://agro-tech-ai-backend-teal.vercel.app/auth/signup", + `${ApiUrl}/auth/signup`, { firstName, lastName, @@ -70,7 +73,7 @@ const SignUpPage = () => { } }; const handleGoogleSignIn = () => { - window.location.href = "https://agro-tech-ai-backend-teal.vercel.app/auth/google"; + window.location.href = `${ApiUrl}/auth/google`; }; return (