-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Corrected passport authentication flow for login route. - Fixed CORS setup to handle credentials in frontend requests. - Implemented session handling for login and signup at (/signup)and(/login). - Updated API endpoints to return appropriate error messages and status codes
- Loading branch information
1 parent
ffb20f3
commit b2000a3
Showing
5 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { useState, ChangeEvent, FormEvent } from "react"; | ||
import axios from "axios"; | ||
import { useNavigate } from "react-router-dom"; // Import the hook for navigation | ||
|
||
interface LoginFormData { | ||
email: string; | ||
password: string; | ||
} | ||
|
||
const Login: React.FC = () => { | ||
const [formData, setFormData] = useState<LoginFormData>({ email: "", password: "" }); | ||
const [message, setMessage] = useState<string>(""); | ||
|
||
const navigate = useNavigate(); // Initialize the navigate hook | ||
|
||
const handleChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
const { name, value } = e.target; | ||
setFormData({ ...formData, [name]: value }); | ||
}; | ||
|
||
const handleSubmit = async (e: FormEvent) => { | ||
e.preventDefault(); | ||
try { | ||
const response = await axios.post( | ||
"http://localhost:5000/api/auth/login", // Backend endpoint | ||
formData, | ||
|
||
); | ||
setMessage(response.data.message); // Show success message from backend | ||
|
||
// Navigate to /home if login is successful | ||
if (response.data.message === 'Login successful') { | ||
navigate("/home"); | ||
} | ||
} catch (error: any) { | ||
setMessage(error.response?.data?.message || "Something went wrong"); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="max-w-md mt-12 mx-auto bg-white p-8 rounded-lg shadow-md"> | ||
<h2 className="text-2xl font-semibold text-center mb-6">Login</h2> | ||
<form onSubmit={handleSubmit} className="space-y-4"> | ||
<div> | ||
<input | ||
type="email" | ||
name="email" | ||
placeholder="Email" | ||
value={formData.email} | ||
onChange={handleChange} | ||
required | ||
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
/> | ||
</div> | ||
<div> | ||
<input | ||
type="password" | ||
name="password" | ||
placeholder="Password" | ||
value={formData.password} | ||
onChange={handleChange} | ||
required | ||
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
/> | ||
</div> | ||
<button | ||
type="submit" | ||
className="w-full bg-blue-500 text-white py-3 rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
> | ||
Login | ||
</button> | ||
</form> | ||
{message && <p className="text-center text-red-500 mt-4">{message}</p>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Login; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { useState, ChangeEvent, FormEvent } from "react"; | ||
import axios from "axios"; | ||
import { useNavigate } from "react-router-dom"; | ||
|
||
interface SignUpFormData { | ||
username: string; | ||
email: string; | ||
password: string; | ||
} | ||
|
||
const SignUp: React.FC = () => { | ||
const [formData, setFormData] = useState<SignUpFormData>({ username: "", email: "", password: "" }); | ||
const [message, setMessage] = useState<string>(""); | ||
|
||
const navigate = useNavigate(); | ||
|
||
const handleChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
const { name, value } = e.target; | ||
setFormData({ ...formData, [name]: value }); | ||
}; | ||
|
||
const handleSubmit = async (e: FormEvent) => { | ||
e.preventDefault(); | ||
try { | ||
const response = await axios.post( | ||
"http://localhost:5000/api/auth/signup", // Backend endpoint | ||
formData // Include cookies for session | ||
); | ||
setMessage(response.data.message); // Show success message from backend | ||
|
||
// Navigate to login page after successful signup | ||
if (response.data.message === 'User created successfully') { | ||
navigate("/login"); | ||
} | ||
} catch (error: any) { | ||
setMessage(error.response?.data?.message || "Something went wrong"); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="mt-12 max-w-md mx-auto bg-white p-8 rounded-lg shadow-md"> | ||
<h2 className="text-2xl font-semibold text-center mb-6">Sign Up</h2> | ||
<form onSubmit={handleSubmit} className="space-y-4"> | ||
<div> | ||
<input | ||
type="text" | ||
name="username" | ||
placeholder="Username" | ||
value={formData.username} | ||
onChange={handleChange} | ||
required | ||
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
/> | ||
</div> | ||
<div> | ||
<input | ||
type="email" | ||
name="email" | ||
placeholder="Email" | ||
value={formData.email} | ||
onChange={handleChange} | ||
required | ||
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
/> | ||
</div> | ||
<div> | ||
<input | ||
type="password" | ||
name="password" | ||
placeholder="Password" | ||
value={formData.password} | ||
onChange={handleChange} | ||
required | ||
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
/> | ||
</div> | ||
<button | ||
type="submit" | ||
className="w-full bg-blue-500 text-white py-3 rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
> | ||
Sign Up | ||
</button> | ||
</form> | ||
{message && <p className="text-center text-red-500 mt-4">{message}</p>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default SignUp; |