Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add password authentication #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions node/authenticate/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var cli = require('cli');
var db = require('./database');
var User = require('./user');
var path = 'database.json';
var bcrypt = require('bcrypt');

cli.parse({
add : ['a', 'Adds a new user'],
Expand All @@ -18,15 +19,21 @@ cli.main(function(args, options) {
return new Promise(function (fulfill, reject) {
if (options.add) {
if(database.users[username]) reject('User Exists');
//TODO: the following line adds a new user to the database
// you may with to change how this works for your program.
database.users[username] = new User({ username: username });

database.save(path).then(fulfill, function () { reject("Could not save database."); });
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(password, salt);

database.users[username] = new User({ username: username, password: hash });

database.save(path);//.then(fulfill, function () { console.dir('fails'); reject("Could not save database."); });
} else {//Authenticate mode
//TODO: Add some form of authentication to determine if correct
// credentials were presented.
if (!database.users[username]) reject("Invalid user");
var user = database.users[username];

if (!user)
reject("Invalid user");

if (!bcrypt.compareSync(password, user.password))
reject("Login failed");
}

fulfill();
Expand Down
10 changes: 8 additions & 2 deletions node/authenticate/database.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
var Promise = require('promise');
var readFile = Promise.denodeify(require('fs').readFile);
var writeFile = Promise.denodeify(require('fs').readFile);
var writeFile = Promise.denodeify(require('fs').writeFile);

var fs = require('fs');

var Database = function Database (data) {
this.users = data && data.users || {};
this.save = function save(path){
return writeFile(path, JSON.stringify(this));
// this doesn't write out the file
//return writeFile(path, JSON.stringify(this));

// this works
fs.writeFileSync(path, JSON.stringify(this));
}
};

Expand Down
3 changes: 3 additions & 0 deletions node/authenticate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"dependencies": {
"cli": "~0.6.5",
"promise": "~6.0.1"
},
"devDependencies": {
"bcrypt": "^0.8.0"
}
}
1 change: 1 addition & 0 deletions node/authenticate/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = function User (options) {
this.username = options.username;
this.password = options.password;
}