Created
March 7, 2018 12:43
-
-
Save mphuget/18c07987d56f6b3d6f9db9f372aee595 to your computer and use it in GitHub Desktop.
socnet/signup/controllers/index.js
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
function signup(req, res) { | |
res.render('signup'); | |
} | |
function signupProcess(req, res) { | |
//we create a new User to be saved into the database | |
var User = require('../../user/models/user'); | |
var user = new User(); | |
var utilities = require('../../core/utilities'); | |
//we check whether all the fields were filled | |
//a first check is performed on form but two checks are better than one... | |
if (typeof req.body.firstName === 'string' && | |
typeof req.body.lastName === 'string' && | |
typeof req.body.username === 'string' && | |
typeof req.body.email === 'string' && | |
typeof req.body.password === 'string' && | |
req.body.firstName != '' && | |
req.body.lastName != '' && | |
req.body.username != '' && | |
req.body.email != '' && | |
req.body.password != '' && | |
utilities.validateEmail(req.body.email)) { | |
//we create an object with all the data | |
user.local.firstName = req.body.firstName; | |
user.local.lastName = req.body.lastName; | |
user.local.username = req.body.username; | |
user.local.email = req.body.email; | |
user.local.password = req.body.password; | |
//we check whether this email already exists | |
User.findOne({"local.email": req.body.email }, function(err, existingUser) { | |
if (existingUser) { | |
res.redirect('/signup'); | |
} | |
else { | |
user.save(function(err, admin) { | |
if (err) return next(err); | |
res.redirect('/signin'); | |
}); | |
} | |
}); | |
} | |
//there are errors on the validation | |
//issue an error to the user | |
else { | |
res.status(400).redirect('/signup'); | |
} | |
} | |
module.exports.signup = signup; | |
module.exports.signupProcess = signupProcess; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment