Last active
May 26, 2017 00:19
-
-
Save HanaanY/164489ccb72716e2275e4de83be6ebd6 to your computer and use it in GitHub Desktop.
line 16 goes through even though I 'm seeing it hit line 20 first :(
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
'use strict'; | |
var Promise = require('bluebird'); | |
var sqlite3 = require('sqlite3'); | |
var db = new sqlite3.Database('testDB.db'); | |
var validator = require('validator'); | |
var has = require('has'); | |
exports.index = function(req, res){ | |
res.render('register', {title: 'register'}); | |
}; | |
exports.registerUser = function(req, res){ | |
console.log(req.body); | |
var validate = Promise.promisify(validateRegistration); | |
validate(req.body) | |
.then(insertNewUser(req)) | |
.then(res.redirect('/register')) | |
.catch(function(errors){ | |
flagValidationError(errors); | |
}); | |
}; | |
function flagValidationError(errors) { | |
for (var key in errors) { | |
console.log(errors[key]); | |
} | |
} | |
function insertNewUser(req) { | |
var resultArray = []; | |
db.serialize(function(){ | |
db.run("INSERT INTO Person (email, username, password) VALUES (?, ?, ?)", [req.body.email, req.body.username, req.body.password]); | |
db.each("SELECT * FROM Person", function(err, row){ | |
resultArray.push(row); | |
console.log(row); | |
}); | |
}); | |
} | |
function validateRegistration(data, callback) { | |
let errors = []; | |
console.log(data); | |
for (var key in data){ | |
if (has(data, key)) { | |
if (validator.isEmpty(key)) { | |
var flag = {key: new Error('field can not be empty')}; | |
errors.push(flag); | |
} | |
} | |
} | |
if (!validator.isEmail(data.email)) { | |
errors.push({"email": new Error('Invalid email')}); | |
} | |
if (!validator.isAlphanumeric(data.password)) { | |
errors.push({"password": new Error('password must be alphanumeric')}); | |
} | |
if (!validator.isLength(data.password, 8)) { | |
errors.push({"password": new Error('password must be at least 8 characters')}); | |
} | |
if (!validator.equals(data.password, data.confirmPassword)){ | |
errors.push({"confirmPassword": new Error('passwords must match')}); | |
} | |
if (errors.length === 0) callback(null); | |
callback(errors); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment