frontend/src/pages/SignIn.jsx

83 lines
2 KiB
React
Raw Normal View History

2025-04-16 12:02:42 -07:00
import React from "react";
import "@fortawesome/fontawesome-free/css/all.min.css";
2025-05-02 17:20:38 -07:00
const authUrl = import.meta.env.VITE_AUTH_URL;
2025-04-16 12:02:42 -07:00
function SignInForm() {
const [state, setState] = React.useState({
2025-05-02 15:07:05 -07:00
assignmentID: "",
2025-04-16 12:02:42 -07:00
password: "",
});
const handleChange = (evt) => {
const value = evt.target.value;
setState({
...state,
[evt.target.name]: value,
});
};
const handleOnSubmit = (evt) => {
evt.preventDefault();
2025-05-02 15:07:05 -07:00
const { assignmentId, password } = state;
console.log(`You are loggind in with email: ${assignmentId} and password: ${password}`);
2025-04-16 12:02:42 -07:00
2025-05-02 15:07:05 -07:00
console.log("Submitting login request with state:", state);
2025-05-02 17:20:38 -07:00
fetch(`${authUrl}/auth/student/login`, {
2025-05-02 15:07:05 -07:00
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(state),
credentials: "include",
})
.then((response) => {
console.log("Received response:", response);
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
console.log("Success:", data);
window.location.href = "/";
})
.catch((error) => {
console.error("Error occurred during login:", error);
alert("Login failed!");
2025-04-16 12:02:42 -07:00
});
};
return (
<div className="form-container sign-in-container">
<form onSubmit={handleOnSubmit}>
<h1>Student</h1>
{/* <div className="social-container">
2025-04-16 12:02:42 -07:00
<a href="#" className="social">
<i className="fab fa-google-plus-g" />
</a>
</div> */}
2025-04-16 12:02:42 -07:00
<input
2025-05-02 15:07:05 -07:00
type="assignmentId"
placeholder="Assignment ID"
name="assignmentId"
value={state.assignmentId}
2025-04-16 12:02:42 -07:00
onChange={handleChange}
/>
<input
type="password"
name="password"
placeholder="Password"
value={state.password}
onChange={handleChange}
/>
2025-04-16 12:02:42 -07:00
<button>Sign In</button>
</form>
</div>
);
}
export default SignInForm;