Created
May 30, 2018 21:10
-
-
Save rgwozdz/74f691c054994e99f2735f5ff324f3fc to your computer and use it in GitHub Desktop.
Koop-docs: authenticate-example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Authenticate a user's submitted credentials | |
* @param {string} username requester's username | |
* @param {strting} password requester's password | |
* @returns {Promise} | |
*/ | |
function authenticate (username, password) { | |
return new Promise((resolve, reject) => { | |
// Validate user's credentials | |
validateCredentials(username, password, _userStoreFilePath) | |
.then(valid => { | |
// If credentials were not valid, reject | |
if (!valid) { | |
let err = new Error('Invalid credentials.') | |
err.code = 401 | |
reject(err) | |
} | |
// Create access token and wrap in response object | |
let expires = Date.now() + (_tokenExpirationMinutes * 60 * 1000) | |
let json = { | |
token: jwt.sign({exp: Math.floor(expires / 1000), sub: username}, _secret), | |
expires | |
} | |
resolve(json) | |
}) | |
.catch(err => { | |
reject(err) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment