Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. async function login(req, res) {
  2. const { password, email } = req.body;
  3. if (!email || !password) {
  4. //Le cas où l'email ou bien le password ne serait pas soumit ou nul
  5. return res.status(400).json({
  6. text: "Requête invalide"
  7. });
  8. }
  9. try {
  10. // On check si l'utilisateur existe en base
  11. const findUser = await User.findOne({ email });
  12. if (!findUser)
  13. return res.status(401).json({
  14. text: "L'utilisateur n'existe pas"
  15. });
  16. if (!findUser.authenticate(password))
  17. return res.status(401).json({
  18. text: "Mot de passe incorrect"
  19. });
  20. return res.status(200).json({
  21. token: findUser.getToken(),
  22. text: "Authentification réussi"
  23. });
  24. } catch (error) {
  25. return res.status(500).json({
  26. error
  27. });
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement