Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // register a new user
- router.post('/register', async (req, res) => {
- console.log(req.body);
- const { fullname, email, password, password2 } = req.body;
- const errors = [];
- // check for required fields
- if (!fullname || !email || !password || !password2) {
- errors.push({ message: 'Please fill in all fields!' });
- }
- // check passwords match
- if (password !== password2) {
- errors.push({ message: 'The passwords do not match!' });
- }
- // check if password isn't too short
- if (password.length < 8) {
- errors.push({ message: 'Password must be at least 8 characters long!' });
- }
- if (errors.length > 0) {
- res.render('register', { errors, fullname, email, password, password2 });
- } else {
- // check if user already exists
- let user = await User.findOne({ email: email });
- if (user) {
- errors.push({ message: 'User already exists. Please login.' }); // pass this error to the login page
- // redirect to login page
- res.redirect('/auth/login');
- } else {
- // register new user
- let newUser = new User({ fullname, email, password });
- const response = await newUser.save();
- res.json(response);
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement