diff --git a/back-end/api/login.js b/back-end/api/login.js index dcbda50..07b7997 100644 --- a/back-end/api/login.js +++ b/back-end/api/login.js @@ -16,7 +16,9 @@ export default async function login(req, res) { { expiresIn: "1h" } ); res.status(200).json(token); + console.log("sign up valid"); } else { res.status(401).json({ message: "Invalid email or password" }); + console.log("invalid credential"); } } diff --git a/back-end/api/register.js b/back-end/api/register.js index 5d030d1..98e80c1 100644 --- a/back-end/api/register.js +++ b/back-end/api/register.js @@ -3,7 +3,7 @@ import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); export default async function register(req, res) { - const { email, pswd, username } = req.body; + const { email, username, pswd } = req.body; try { const newUser = await prisma.user.create({ diff --git a/back-end/index.js b/back-end/index.js index 0b9a79c..64adec2 100644 --- a/back-end/index.js +++ b/back-end/index.js @@ -13,7 +13,9 @@ client.$connect(); app.post("/api/register", register); app.post("/api/login", login); -app.listen(3000); +app.listen(3000, () => { + console.log(`Server is running on port 3000`); +}); client.$disconnect(); /* function verify_token(req, res, next) { diff --git a/front_end/src/files/Register.jsx b/front_end/src/files/Register.jsx index f8f6637..b04de64 100644 --- a/front_end/src/files/Register.jsx +++ b/front_end/src/files/Register.jsx @@ -1,14 +1,16 @@ import { Button, Input, Stack } from "@chakra-ui/react"; -import axios from "axios"; import { useState } from "react"; export function Register() { const [isLoading, setIsLoading] = useState(false); const [email, setEmail] = useState(""); + const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); - const handleSubmit = async () => { + const handleSubmit = async (e) => { + e.preventDefault(); + if (password !== confirmPassword) { alert("Les mots de passe ne correspondent pas"); return; @@ -16,51 +18,64 @@ export function Register() { setIsLoading(true); try { - await axios.post("/api/register", { email, password }); + const response = await fetch("http://localhost:3000/api/register", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ email, username, pswd: password }), + }); + + if (!response.ok) { + const errorData = await response.json(); + throw new Error(`Network response was not ok: ${errorData.message}`); + } + alert("Inscription réussie"); } catch (error) { - console.error(error); - alert("Erreur lors de l'inscription"); + console.error("Error during registration:", error); + alert(`Erreur lors de l'inscription: ${error.message}`); } finally { setIsLoading(false); } }; return ( - <> -