Advertisement
Shell_Casing

register new user

Feb 2nd, 2019
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // register a new user
  2. router.post('/register', async (req, res) => {
  3.     console.log(req.body);
  4.     const { fullname, email, password, password2 } = req.body;
  5.     const errors = [];
  6.  
  7.     // check for required fields
  8.     if (!fullname || !email || !password || !password2) {
  9.         errors.push({ message: 'Please fill in all fields!' });
  10.     }
  11.  
  12.     // check passwords match
  13.     if (password !== password2) {
  14.         errors.push({ message: 'The passwords do not match!' });
  15.     }
  16.  
  17.     // check if password isn't too short
  18.     if (password.length < 8) {
  19.         errors.push({ message: 'Password must be at least 8 characters long!' });
  20.     }
  21.  
  22.     if (errors.length > 0) {
  23.         res.render('register', { errors, fullname, email, password, password2 });
  24.     } else {
  25.         let user;
  26.         // check if user already exists
  27.         user = await User.findOne({ email: email });
  28.         if (user) {
  29.             errors.push({ message: 'User already exists. Please login.' });
  30.             // redirect to login page
  31.             res.redirect('/login');
  32.         } else {
  33.             // register new user
  34.             user = new User({ fullname, email, password });
  35.         }
  36.  
  37.         console.log(user);
  38.         res.send('registration is done!');
  39.  
  40.     }
  41.  
  42. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement