Skip to content

Instantly share code, notes, and snippets.

@rgwozdz
Created May 30, 2018 21:10
Show Gist options
  • Save rgwozdz/74f691c054994e99f2735f5ff324f3fc to your computer and use it in GitHub Desktop.
Save rgwozdz/74f691c054994e99f2735f5ff324f3fc to your computer and use it in GitHub Desktop.
Koop-docs: authenticate-example
/**
* 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