-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathauth.js
59 lines (51 loc) · 1.9 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*****
See Credits & README: https://github.com/justsml/escape-from-callback-mountain
******/
const Promise = require('bluebird')
const {hashStringAsync} = Promise.promisifyAll(require('./lib/crypto'))
const {logEventAsync} = Promise.promisifyAll(require('./lib/log'))
const {openAsync} = Promise.promisifyAll(require('./lib/db'))
const {TimeoutError,
ValidationError,
NotFoundError} = require('./errors')
const _openHandle = openAsync() // FYI: Promises include memoization (caching) built into same API
module.exports = {auth}
/* auth is our main function */
function auth({username, password,
_onTimeoutError = errorHandler,
_onNotFoundError = errorHandler,
_onValidationError = errorHandler}) {
return Promise.resolve({username, password})
.then(authValidated)
.then(usersModel)
.then(users => {
return hashedPasswordUserQuery({username, password})
.then(users.findOneAsync)
.then(userFound)
})
.catch(TimeoutError, _onTimeoutError)
.catch(NotFoundError, _onNotFoundError)
.catch(ValidationError, _onValidationError)
.catch(errorHandler)
}
function authValidated({username, password}) {
if (!username || username.length < 4) { return Promise.reject(new ValidationError('Invalid username. Required, 4 char minimum.')) }
if (!password || password.length < 4) { return Promise.reject(new ValidationError('Invalid password. Required, 4 char minimum.')) }
return {username, password}
}
function userFound(results) {
return results ? results : Promise.reject(new NotFoundError('No users matched. Login failed'))
}
function hashedPasswordUserQuery({username, password}) {
return hashStringAsync(password)
.then(hashPass => {
return {username, hashPass}
})
}
function usersModel() {
return _openHandle
.then(({models}) => models.users)
}
function errorHandler(err) {
console.error('Failed auth!', err)
}