Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * User Registration
- * Sends a response object with success true if user was registered.
- * Sends a response object with success false if user couldn't be registered.
- */
- router.post('/', function(req, res, next) {
- var userInfo = req.body;
- userInfo.email = sanitize(userInfo.email);
- userInfo.password = sanitize(userInfo.password);
- if(!validEmail(userInfo.email) || !validPassword(userInfo.password)) { //If the request info is invalid
- sendJsonResponse(res, false, error, 400);
- }
- else {
- //Check if e-mail already exists
- User.find({email: userInfo.email}, function(err, docs) {
- if(err) {
- console.log(err);
- sendJsonResponse(res, false, "There was an error processing your request.", 400);
- }
- else if(docs.length > 0) { //If e-mail was found in database, generate error
- sendJsonResponse(res, false, "This e-mail already exists, please login or use another e-mail.", 409);
- }
- else {
- //Create user based on data submitted.
- var user = new User({email: sanitize(userInfo.email), password: bcrypt.hashSync(sanitize(userInfo.password), 10), permission_level: 'REGULAR'});
- //Save it in the database.
- user.save(function (err) {
- if (err) {
- sendJsonResponse(res, false, "There was an error processing your request.", 400);
- }
- else { //If everything is fine
- //Create token to login the registered user
- var token = jwt.sign(user, config.secret, {
- expiresIn: '24h' // expires in 24 hours
- });
- sendJsonResponse(res, true, "You have been registered!", 200, token);
- }
- });
- }
- });
- }
- error = "";
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement