generated from WildCodeSchool/create-js-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c44c9f
commit 87e6745
Showing
6 changed files
with
142 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Import access to database tables | ||
const tables = require("../tables"); | ||
|
||
const login = async (req, res, next) => { | ||
try { | ||
// Fetch all users from the database | ||
const user = await tables.user.readByEmail(req.body.email); | ||
|
||
if (user == null || user.password !== req.body.password) { | ||
res.sendStatus(422); | ||
} else { | ||
// Respond with the users in JSON format | ||
res.json(user); | ||
} | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
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
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,68 @@ | ||
import { useRef } from "react"; | ||
import { useNavigate, useOutletContext } from "react-router-dom"; | ||
|
||
function Login() { | ||
// Références pour les champs email et mot de passe | ||
const emailRef = useRef(); | ||
const passwordRef = useRef(); | ||
|
||
const { setUser } = useOutletContext(); | ||
|
||
// Hook pour la navigation | ||
const navigate = useNavigate(); | ||
|
||
// Gestionnaire de soumission du formulaire | ||
const handleSubmit = async (event) => { | ||
event.preventDefault(); | ||
|
||
try { | ||
// Appel à l'API pour demander une connexion | ||
const response = await fetch( | ||
`${import.meta.env.VITE_BACKEND_URL}/api/login`, | ||
{ | ||
method: "post", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
email: emailRef.current.value, | ||
password: passwordRef.current.value, | ||
}), | ||
} | ||
); | ||
|
||
// Redirection vers la page de connexion si la création réussit | ||
if (response.status === 200) { | ||
const user = await response.json(); | ||
|
||
setUser(user); | ||
|
||
navigate("/"); | ||
} else { | ||
// Log des détails de la réponse en cas d'échec | ||
console.info(response); | ||
} | ||
} catch (err) { | ||
// Log des erreurs possibles | ||
console.error(err); | ||
} | ||
}; | ||
|
||
// Rendu du composant formulaire | ||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<div> | ||
{/* Champ pour l'email */} | ||
<label htmlFor="email">email</label>{" "} | ||
<input ref={emailRef} type="email" id="email" /> | ||
</div> | ||
<div> | ||
{/* Champ pour le mot de passe */} | ||
<label htmlFor="password">password</label>{" "} | ||
<input type="password" id="password" ref={passwordRef} /> | ||
</div> | ||
{/* Bouton de soumission du formulaire */} | ||
<button type="submit">Send</button> | ||
</form> | ||
); | ||
} | ||
|
||
export default Login; |