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

Flattening functions #2

Merged
merged 4 commits into from May 5, 2017
Jump to file or symbol
Failed to load files and symbols.
+34 −19
Diff settings

Always

Just for now

View
@@ -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)
}
ProTip! Use n and p to navigate between commits in a pull request.