ADD: register and login on the site filling the db
Some checks failed
/ deploy (push) Failing after 5s

This commit is contained in:
dorian melenotte 2024-10-03 17:00:45 +02:00
parent 40e53df3e8
commit 5f71dcd028
5 changed files with 79 additions and 46 deletions

View File

@ -16,7 +16,9 @@ export default async function login(req, res) {
{ expiresIn: "1h" } { expiresIn: "1h" }
); );
res.status(200).json(token); res.status(200).json(token);
console.log("sign up valid");
} else { } else {
res.status(401).json({ message: "Invalid email or password" }); res.status(401).json({ message: "Invalid email or password" });
console.log("invalid credential");
} }
} }

View File

@ -3,7 +3,7 @@ import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient(); const prisma = new PrismaClient();
export default async function register(req, res) { export default async function register(req, res) {
const { email, pswd, username } = req.body; const { email, username, pswd } = req.body;
try { try {
const newUser = await prisma.user.create({ const newUser = await prisma.user.create({

View File

@ -13,7 +13,9 @@ client.$connect();
app.post("/api/register", register); app.post("/api/register", register);
app.post("/api/login", login); app.post("/api/login", login);
app.listen(3000); app.listen(3000, () => {
console.log(`Server is running on port 3000`);
});
client.$disconnect(); client.$disconnect();
/* function verify_token(req, res, next) { /* function verify_token(req, res, next) {

View File

@ -1,14 +1,16 @@
import { Button, Input, Stack } from "@chakra-ui/react"; import { Button, Input, Stack } from "@chakra-ui/react";
import axios from "axios";
import { useState } from "react"; import { useState } from "react";
export function Register() { export function Register() {
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [username, setUsername] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState("");
const handleSubmit = async () => { const handleSubmit = async (e) => {
e.preventDefault();
if (password !== confirmPassword) { if (password !== confirmPassword) {
alert("Les mots de passe ne correspondent pas"); alert("Les mots de passe ne correspondent pas");
return; return;
@ -16,51 +18,64 @@ export function Register() {
setIsLoading(true); setIsLoading(true);
try { 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"); alert("Inscription réussie");
} catch (error) { } catch (error) {
console.error(error); console.error("Error during registration:", error);
alert("Erreur lors de l'inscription"); alert(`Erreur lors de l'inscription: ${error.message}`);
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }
}; };
return ( return (
<> <Stack spacing={4} align="center" as="form" onSubmit={handleSubmit}>
<h1 style={{ color: "white", fontSize: "30px", textAlign: "center" }}> <Input
Veuillez remplir ce formulaire d'Inscription placeholder="Email"
</h1> size="md"
<Stack spacing={4} align="center"> value={email}
<Input onChange={(e) => setEmail(e.target.value)}
placeholder="Email" />
size="md" <Input
value={email} placeholder="Username"
onChange={(e) => setEmail(e.target.value)} size="md"
/> value={username}
<Input onChange={(e) => setUsername(e.target.value)}
placeholder="Password" />
size="md" <Input
type="password" placeholder="Password"
value={password} size="md"
onChange={(e) => setPassword(e.target.value)} type="password"
/> value={password}
<Input onChange={(e) => setPassword(e.target.value)}
placeholder="Confirm Password" />
size="md" <Input
type="password" placeholder="Confirm Password"
value={confirmPassword} size="md"
onChange={(e) => setConfirmPassword(e.target.value)} type="password"
/> value={confirmPassword}
<Button onChange={(e) => setConfirmPassword(e.target.value)}
isLoading={isLoading} />
loadingText="Chargement" <Button
variant="outline" isLoading={isLoading}
onClick={handleSubmit} loadingText="Chargement"
> variant="outline"
Soumettre type="submit"
</Button> >
</Stack> Register
</> </Button>
</Stack>
); );
} }

View File

@ -1,5 +1,4 @@
import { Button, Input, Stack } from "@chakra-ui/react"; import { Button, Input, Stack } from "@chakra-ui/react";
import axios from "axios";
import { useState } from "react"; import { useState } from "react";
export function SignIn() { export function SignIn() {
@ -9,11 +8,26 @@ export function SignIn() {
const handleSubmit = async () => { const handleSubmit = async () => {
setIsLoading(true); setIsLoading(true);
alert(email);
alert(password);
try { try {
await axios.post("/api/register", { email, password }); const response = await fetch("http://localhost:3000/api/login", {
console.log("gg"); method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email, pswd: password }),
});
/* if (!response.ok) {
throw new Error("Network response was not ok");
} */
const data = await response.json();
alert("Login successful:", data);
} catch (error) { } catch (error) {
console.log("error"); console.error("Error during login:", error);
alert("Error during login: " + error.message);
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }