Skip to content

Commit

Permalink
more code cleanup, and another async/await example
Browse files Browse the repository at this point in the history
  • Loading branch information
justsml committed Jul 30, 2017
1 parent 4715e94 commit 92f9221
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/images/photos/credits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Credit: [By Brickset](https://www.flickr.com/photos/brickset/19522560551/in/gallery-ryanthescooterguy-72157651051047402/)
Credit: [By Sonja](https://www.flickr.com/photos/8308527@N02/5650500453/)
Binary file added docs/images/photos/huge-lego-wall.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/photos/legos-organized-drawers.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions src/auth.callbacks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// EXAMPLE: CALLBACKS
const {hashString} = require('./lib/crypto')
const {auditLog} = require('./lib/log')
const {logEvent} = require('./lib/log')
const {getModels} = require('./lib/db')

function auth(username, password, callback) {
Expand All @@ -13,13 +13,13 @@ function auth(username, password, callback) {
const {users} = models
hashString(password, function _hashed(err, hash) {
if (err) return callback(err)
users.findOne({username, password: hash}, function _find(err, results) {
users.findOne({username, password: hash}, function _find(err, user) {
if (err) return callback(err)
if (!results) {
if (!user) {
return callback(new Error('No users matched. Login failed'))
}
auditLog({event: 'login', username}, function _noOp() {/* do nothing */})
callback(null, results)
logEvent({event: 'login', username}, function _noOp() {/* do nothing */})
callback(null, user)
})
})
})
Expand Down
32 changes: 32 additions & 0 deletions src/auth.fp.await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// EXAMPLE: FUNCTIONAL PROMISES
const Promise = require('bluebird')
const {hashString} = require('./lib/crypto')
const {logEventAsync} = require('./lib/log')
const {getModel} = require('./lib/db')

module.exports = {auth}

async function auth({username, password}) {
if (_isInputValid({username, password})) {
logEventAsync({event: 'login', username})()
const user = await _loginUser({username, password})
if (user && user._id) {
return user
}
throw new Error('User Not found!')
}
}

async function _loginUser({username, password}) {
let query = {
username,
password: await hashString(password)
}
return await getModel('users').findOneAsync(query)
}

function _isInputValid({username, password}) {
if (!username || username.length < 1) throw new Error('Invalid username.')
if (!password || password.length < 6) throw new Error('Invalid password.')
return {username, password}
}

0 comments on commit 92f9221

Please sign in to comment.