Skip to content

Instantly share code, notes, and snippets.

@indigo0086
Created February 2, 2014 03:45
Show Gist options
  • Save indigo0086/8762765 to your computer and use it in GitHub Desktop.
Save indigo0086/8762765 to your computer and use it in GitHub Desktop.
Very simple user authentication using async
var async = require('async');
module.exports = {
userCredentials: [
{
username: 'testUser1',
password: 'pa$$word'
},
{
username: 'testUser2',
password: 's0mep4$$w0rd'
}],
isUserAuthenticated: function(username, password) {
var scope = this;
process.nextTick(function() {
async.detect(
scope.userCredentials,
function(item, callback) {
var isCorrect = item.username === username &&
item.password === password;
callback(isCorrect);
},
function(result) {
if(!result) {
throw "Username and password incorrect";
}
console.log("Username and password are correct");
}
);
})
}
}
var async = require('async'),
auth = require('./auth.js'),
stdin = process.stdin, stdout = process.stdout;
process.stdin.resume();
process.stdin.setEncoding('utf8');
async.waterfall([
function(callback) {
stdout.write("Please enter your username: ");
stdin.once('data', function(data) {
username = data.toString().trim();
if(username === '') {
callback("Username cannot be blank");
}
callback(null, username);
})
},
function(username, callback) {
stdout.write("Please enter your password: ");
stdin.once('data', function(data) {
var password = data.toString().trim();
if(password === '') {
callback("Password cannot be blank");
}
callback(null, {
username: username,
password: password
});
})
}
], function(err, credentials) {
if(err) {
throw err;
}
auth.isUserAuthenticated.call(auth, credentials.username, credentials.password);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment