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 diff --git a/src/components/AuthenticationTitle/AuthenticationTitle.tsx b/src/components/AuthenticationTitle/AuthenticationTitle.tsx index 5edceec..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,36 +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( new URL("login?useCookies=true", import.meta.env.VITE_API_BASE), - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - credentials: 'include', - body: JSON.stringify(loginRequest), - }); + { + 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( new URL("profile", import.meta.env.VITE_API_BASE), - { - credentials: 'include', - }); - + { + credentials: "include", + } + ); + if (profileResponse.ok) { const userProfile = await profileResponse.json(); const user = { @@ -60,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)} @@ -121,4 +119,4 @@ export function AuthenticationTitle() { ); -} \ No newline at end of file +}