Skip to content

Instantly share code, notes, and snippets.

@HanaanY
Last active May 26, 2017 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HanaanY/164489ccb72716e2275e4de83be6ebd6 to your computer and use it in GitHub Desktop.
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 :(
'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