| @@ -126,5 +126,17 @@ module.exports.prototype = { | |||
| setStatus: function(code) { | |||
| this.statusCode = code; | |||
| }, | |||
| login: function(username, id) { | |||
| this.session.loggedIn = true; | |||
| this.session.username = username; | |||
| this.session.userId = id; | |||
| }, | |||
| logout: function() { | |||
| this.session.loggedIn = false; | |||
| delete this.session.username; | |||
| delete this.session.userId; | |||
| } | |||
| } | |||
| @@ -38,7 +38,8 @@ var endpoints = { | |||
| "/api/image_create": "api/image_create.node.js", | |||
| "/api/collection_create": "api/collection_create.node.js", | |||
| "/api/account_create": "api/account_create.node.js", | |||
| "/api/account_login": "api/account_login.node.js" | |||
| "/api/account_login": "api/account_login.node.js", | |||
| "/api/account_logout": "api/account_logout.node.js" | |||
| } | |||
| var loaded = loader.load(endpoints, conf); | |||
| @@ -17,7 +17,7 @@ | |||
| </div> | |||
| <div class="submit-container"> | |||
| <button type="submit" class="btn btn-default">Log In</button> | |||
| <a class="btn btn-default register" href="/register">Register</a> | |||
| <a class="register" href="/register">Register</a> | |||
| </div> | |||
| </form> | |||
| </li></ul> | |||
| @@ -27,9 +27,7 @@ module.exports = function(ctx) { | |||
| if (err) | |||
| return ctx.fail(err); | |||
| ctx.session.loggedIn = true; | |||
| ctx.session.userId = res.rows[0].id; | |||
| ctx.session.username = ctx.postData.username; | |||
| ctx.login(ctx.postData.username, res.rows[0].id); | |||
| ctx.succeed({ | |||
| id: res.rows[0].id | |||
| @@ -23,9 +23,7 @@ module.exports = function(ctx) { | |||
| var user = res.rows[0]; | |||
| ctx.session.loggedIn = true; | |||
| ctx.session.userId = user.id; | |||
| ctx.session.username = user.username; | |||
| ctx.login(user.username, user.id); | |||
| if (!user) | |||
| return ctx.fail("Wrong username or password."); | |||
| @@ -1,47 +0,0 @@ | |||
| var scrypt = require("scrypt"); | |||
| module.exports = function(ctx) { | |||
| ctx.getPostData(function(err, data) { | |||
| if (err) | |||
| return ctx.fail(err); | |||
| if (!data.username || !data.password) | |||
| return ctx.fail("You must provide a username and a password."); | |||
| ctx.db.query( | |||
| "SELECT id, username, pass_hash "+ | |||
| "FROM users "+ | |||
| "WHERE username=$1", | |||
| [data.username], | |||
| queryCallback | |||
| ); | |||
| }); | |||
| function queryCallback(err, res) { | |||
| if (err) | |||
| return ctx.fail(err); | |||
| var user = res.rows[0]; | |||
| ctx.session.loggedIn = true; | |||
| ctx.session.userId = user.id; | |||
| ctx.session.username = user.username; | |||
| if (!user) | |||
| return ctx.fail("Wrong username or password."); | |||
| scrypt.verify( | |||
| new Buffer(user.pass_hash, "hex"), | |||
| new Buffer(ctx.postData.data.password), | |||
| function(err, success) { | |||
| if (success) { | |||
| ctx.succeed({ | |||
| id: user.id | |||
| }) | |||
| } else { | |||
| ctx.fail("Wrong username or password."); | |||
| } | |||
| } | |||
| ); | |||
| } | |||
| } | |||
| @@ -0,0 +1,6 @@ | |||
| var scrypt = require("scrypt"); | |||
| module.exports = function(ctx) { | |||
| ctx.logout(); | |||
| ctx.succeed(); | |||
| } | |||