Skip to content

Flattening functions #2

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

Merged
merged 4 commits into from
May 5, 2017
Merged
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
53 changes: 34 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,42 @@ const {hashString} = require('./lib/crypto')
const {auditLog} = require('./lib/log')
const {connection} = require('./lib/db')

const authValidate = function _validated({username, password, callback}) {
if (!username || username.length < 4) { return new Error('Invalid username. Required, 4 char minimum.') }
if (!password || password.length < 4) { return new Error('Invalid password. Required, 4 char minimum.') }
if (!callback) { return new Error('Callback arg required!') }
return true
}

function auth({username, password}, callback) {
if (!username || username.length < 4) { return callback(new Error('Invalid username. Required, 4 char minimum.')) }
if (!password || password.length < 4) { return callback(new Error('Invalid password. Required, 4 char minimum.')) }
if (!callback) { return callback(new Error('Callback arg required!')) }
// Get db connection, hopefully cached? Pooled? - returns models
connection.open(function _onConnected(err, {models}) {
let users = null;
let isValid = authValidate()

if (isValid !== true) { return callback(isValid) }

function _findHandler(err, results) {
if (err) return callback(err)
if (!results) {
return callback(new Error('No users matched. Login failed'))
}
// Before returning results, make a non-blocking call to logging function `auditLog`
auditLog({event: 'login', username}, function _noOp() {/* do nothing */})
callback(null, results)
}

function _hashHandler(err, passHash) {
if (err) return callback(err)
users.findOne({username, passHash}, _findHandler)
}

function _onConnected(err, {models}) {
if (err) return callback(err)
// Get reference to `users` query interface
const {users} = models
users = models.users
// Hash the password before querying for user
hashString(password, function _hashHandler(err, passHash) {
if (err) return callback(err)
users.findOne({username, passHash}, function _findHandler(err, results) {
if (err) return callback(err)
if (!results) {
return callback(new Error('No users matched. Login failed'))
}
// Before returning results, make a non-blocking call to logging function `auditLog`
auditLog({event: 'login', username}, function _noOp() {/* do nothing */})
callback(null, results)
})
})
})
hashString(password, _hashHandler)
}

// Get db connection, hopefully cached? Pooled? - returns models
connection.open(_onConnected)
}