frontend/src/pages/SignIn.jsx

80 lines
1.9 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({
qrNumber: "",
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();
const { qrNumber, password } = state;
console.log(
`You are loggind in with email: ${qrNumber} 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",
2025-05-02 15:07:05 -07:00
},
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();
2025-05-02 15:07:05 -07:00
})
.then((data) => {
console.log("Success:", data);
window.location.href = "/";
2025-05-02 15:07:05 -07:00
})
.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>
2025-04-16 12:02:42 -07:00
<input
type="number"
placeholder="Assignment QR Number"
name="qrNumber"
value={state.qrNumber}
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;