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

Commit

Permalink
Signing up as a new user and logging in works with sessions + hashed …
Browse files Browse the repository at this point in the history
…password authentication!
  • Loading branch information
dsimmons committed Aug 5, 2011
1 parent 52d2042 commit 49a45d3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
1 change: 1 addition & 0 deletions app.js
Expand Up @@ -65,6 +65,7 @@ app.configure('production', function(){
require('./controllers/index.js');
require('./controllers/login.js');
require('./controllers/signup.js');
require('./controllers/logout.js');

// Libraries
auth = require('./lib/auth.js');
Expand Down
11 changes: 4 additions & 7 deletions controllers/login.js
Expand Up @@ -3,22 +3,19 @@ app.get('/login', function(req, res) {
});

app.post('/login', function(req, res) {

auth.login(req.body.username, req.body.password, function(err, user) {
if (err) console.log(err);

if (user) {
if ((err) || (!user)) {
req.session.error = 'Authentication failed. Check username and password.';
res.redirect('back');
} else if (user) {
req.session.regenerate(function() {
req.session.cookie.maxAge = 1000 * 60 * 60; //check -- 1 min?
req.session.user = user;

res.redirect('/');
});
} else {
req.session.error = 'Authentication failed. Check username and password.';
res.redirect('back');
}

});

});
10 changes: 6 additions & 4 deletions lib/auth.js
Expand Up @@ -29,20 +29,22 @@
}

exports.login = function(username, password, fn) {
User.findOne({'username': username}, function(err, user) {
User.findOne({'username':username}, function(err, user) {
if (err) {
return fn(new Error('[login] Retrieving ' + username + ' failed'));
}

if (user) {
console.log(user.password === Hash.sha256(password));
if (user.password === Hash.sha256(password)) {
return fn(null, user);
} else {
return fn(new Error('[login] Incorrect password for ' +username));
}
console.log(user.password);
console.log(Hash.sha256(password));
} else {
return fn(new Error('[login] User ' + username + ' not found!'));
}
});
return fn(null, null);
}

})();
4 changes: 2 additions & 2 deletions views/signup.jade
Expand Up @@ -9,10 +9,10 @@ h4
form(method='post', action='/signup')
p
label First Name:
input(type='text', name='username')
input(type='text', name='firstName')
p
label Last Name:
input(type='text', name='username')
input(type='text', name='lastName')
p
label Username:
input(type='text', name='username')
Expand Down

0 comments on commit 49a45d3

Please sign in to comment.