From 851be74bd910d099c23e05e3530c06668d0d997b Mon Sep 17 00:00:00 2001 From: benhuangbmj Date: Sat, 26 Apr 2025 22:35:56 -0400 Subject: [PATCH 1/2] chore: create the env template --- .env.template | 1 + 1 file changed, 1 insertion(+) create mode 100644 .env.template diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..04fba11 --- /dev/null +++ b/.env.template @@ -0,0 +1 @@ +VITE_API_BASE=http://localhost:5140/ \ No newline at end of file From cfadc03555adeb3b4cac8270c3b8d341aed7c434 Mon Sep 17 00:00:00 2001 From: benhuangbmj Date: Sat, 26 Apr 2025 22:49:01 -0400 Subject: [PATCH 2/2] change: replace the api base by the env --- .../AuthenticationTitle.tsx | 86 ++++++++++--------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/src/components/AuthenticationTitle/AuthenticationTitle.tsx b/src/components/AuthenticationTitle/AuthenticationTitle.tsx index 8840b5e..1f8ed80 100644 --- a/src/components/AuthenticationTitle/AuthenticationTitle.tsx +++ b/src/components/AuthenticationTitle/AuthenticationTitle.tsx @@ -1,19 +1,19 @@ import { - Anchor, - Button, - Checkbox, - Container, - Group, - Paper, - PasswordInput, - Text, - TextInput, - Title, - } from '@mantine/core'; -import { Link, useNavigate } from 'react-router-dom'; -import { useState } from 'react'; -import classes from './AuthenticationTitle.module.css'; -import { useAuth } from '../../AuthContext.tsx'; + Anchor, + Button, + Checkbox, + Container, + Group, + Paper, + PasswordInput, + Text, + TextInput, + Title, +} from "@mantine/core"; +import { Link, useNavigate } from "react-router-dom"; +import { useState } from "react"; +import classes from "./AuthenticationTitle.module.css"; +import { useAuth } from "../../AuthContext.tsx"; interface LoginRequest { email: string; @@ -22,32 +22,38 @@ interface LoginRequest { export function AuthenticationTitle() { const { login } = useAuth(); // Assuming `useAuth` provides a `login` function - const [email, setEmail] = useState(''); - const [password, setPassword] = useState(''); - const [error, setError] = useState(''); + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + const [error, setError] = useState(""); const navigate = useNavigate(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); - setError(''); + setError(""); const loginRequest: LoginRequest = { email, password }; - const response = await fetch('https://cvx.jordonbyers.com/login?useCookies=true', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - credentials: 'include', - body: JSON.stringify(loginRequest), - }); + const response = await fetch( + new URL("login?useCookies=true", import.meta.env.VITE_API_BASE), + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + credentials: "include", + body: JSON.stringify(loginRequest), + } + ); if (response.ok) { // Fetch user profile after login - const profileResponse = await fetch('https://cvx.jordonbyers.com/profile', { - credentials: 'include', - }); - + const profileResponse = await fetch( + new URL("profile", import.meta.env.VITE_API_BASE), + { + credentials: "include", + } + ); + if (profileResponse.ok) { const userProfile = await profileResponse.json(); const user = { @@ -56,35 +62,31 @@ export function AuthenticationTitle() { lastName: userProfile.lastName, avatarUrl: userProfile.avatarUrl, }; - + login(user); // Store user in AuthContext - navigate('/'); + navigate("/"); } else { - setError('Failed to fetch user profile'); + setError("Failed to fetch user profile"); } } else { - setError('Invalid email or password'); + setError("Invalid email or password"); } }; - return ( Welcome back! - Do not have an account yet?{' '} - - Create account - + Do not have an account yet? Create account
setEmail(event.currentTarget.value)} @@ -117,4 +119,4 @@ export function AuthenticationTitle() { ); -} \ No newline at end of file +}