Skip to content
This repository has been archived by the owner on Nov 14, 2017. It is now read-only.

Commit

Permalink
My laptop monitor is flickering and I'm worried that it might crap ou…
Browse files Browse the repository at this point in the history
…t! Still working on authentication and passing information between pages.
  • Loading branch information
dsimmons committed Aug 6, 2011
1 parent 15a9883 commit a0463c3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
11 changes: 6 additions & 5 deletions app.js
Expand Up @@ -73,11 +73,12 @@ app.configure('production', function(){
});

// Routes
require('./controllers/index.js');
require('./controllers/login.js');
require('./controllers/signup.js');
require('./controllers/user.js');
require('./controllers/admin.js');
//require('./controllers/index.js');
//require('./controllers/login.js');
//require('./controllers/signup.js');
//require('./controllers/user.js');
//require('./controllers/admin.js');
require('./controllers/routes.js');

// Libraries
auth = require('./lib/auth.js');
Expand Down
61 changes: 56 additions & 5 deletions routes.js → controllers/routes.js
Expand Up @@ -52,7 +52,7 @@ function checkSession(req, res, next) {
req.session.error = 'Access denied!';
res.redirect('/login');
}
};
}


function checkAdmin(req, res, next) {
Expand All @@ -67,7 +67,7 @@ function checkAdmin(req, res, next) {
req.session.error = 'User not authenticated, please log in.';
res.redirect('/login');
}
};
}


/****************
Expand All @@ -94,16 +94,67 @@ app.post('/login', function(req, res) {
});
});

function validateForm(req, res, next) {
var form = req.body;
var origin = (req.header('Referer').indexOf('/signup') >= 0) ? 'signup' : 'user';
var regex_normal = /\b\w{1,15}\b/;
var regex_password = /[a-zA-Z0-9!@$%^&*-+,._]{6,35}/;
var regex_email = /\b[\w.-]+@{1}\w+\.[a-zA-Z]{2,4}\b/;

// These field lengths (boundaries) are semi-arbitrariy atm.
var validationList = {
'firstName': function() {
return regex_normal.test(form.firstName);
}(),
'lastName': function() {
return regex_normal.test(form.lastName);
}(),
'username': function() {
return regex_normal.test(form.username);
}(),
'passMatch': function() {
return (form.password === form.password2);
}(),
'password': function() {
return regex_password.test(form.password);
}(),
'email': function() {
return regex_email.test(form.email);
}(),
}
if (origin === 'user') {
validationList['newPass'] = function() {
return (form.newPass != undefined) && (regex_password.test(form.newPass));
}()
}
var valid = true;
for (var i in validationList) {
if (!validationList[i]) {
valid = false;
break;
}
}
console.log(validationList);
console.log('valid: ' + valid);
console.log(origin);
if (valid)
next();

}


// 2 possibilities (any user db addition/modification):
// 1) Register -> new user is inserted
// 2) Edit Profile -> user fields are updated
app.post('/user', function(req, res) {
// Either way, validate user input first.
app.post('/user', validateForm, function(req, res) {
var form = req.body;

auth.createUser(req.body.firstName, req.body.lastName, req.body.username,
req.body.password, req.body.email, function(err, user) {
auth.createUser(form.firstName, form.lastName, form.username,
form.password, form.email, function(err, user) {

if ((err) || (!user)) {
console.log(err);
req.session.error = 'Unable to create new user.';
res.redirect('back');

Expand Down
1 change: 1 addition & 0 deletions views/user.jade
@@ -1,2 +1,3 @@
h1 Edit Profile
include includes/userForm

0 comments on commit a0463c3

Please sign in to comment.