Advertisement
Infernale

Untitled

Dec 1st, 2020
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. //users.js in routes/users.js
  2. const express = require('express');
  3. const router = express.Router();
  4. const User = require("../models/user");
  5. //login handle
  6. router.get('/login',(req,res)=>{
  7. res.render('login');
  8. })
  9. router.get('/register',(req,res)=>{
  10. res.render('register')
  11. })
  12. //Register handle
  13. router.post('/login',(req,res)=>{
  14. })
  15. //register post handle
  16. router.post('/register',(req,res)=>{
  17. const {name,email, password, password2} = req.body;
  18. let errors = [];
  19. console.log(' Name ' + name+ ' email :' + email+ ' pass:' + password);
  20. if(!name || !email || !password || !password2) {
  21. errors.push({msg : "Please fill in all fields"})
  22. }
  23. //check if match
  24. if(password !== password2) {
  25. errors.push({msg : "passwords dont match"});
  26. }
  27.  
  28. //check if password is more than 6 characters
  29. if(password.length < 6 ) {
  30. errors.push({msg : 'password atleast 6 characters'})
  31. }
  32. if(errors.length > 0 ) {
  33. res.render('register', {
  34. errors : errors,
  35. name : name,
  36. email : email,
  37. password : password,
  38. password2 : password2})
  39. } else {
  40. //validation passed
  41. User.findOne({email : email}).exec((err,user)=>{
  42. console.log(user);
  43. if(user) {
  44. errors.push({msg: 'email already registered'});
  45. res.render('register',{errors,name,email,password,password2})
  46. } else {
  47. const newUser = new User({
  48. name : name,
  49. email : email,
  50. password : password
  51. });
  52. }
  53. })
  54. }
  55. })
  56. //logout
  57. router.get('/logout',(req,res)=>{
  58. })
  59. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement