node-express-mongodb-jwt-rest-api-skeleton
Version:
Node.js express.js MongoDB JWT REST API - This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API)
27 lines (23 loc) • 850 B
JavaScript
const { saveLoginAttemptsToDB } = require('./saveLoginAttemptsToDB')
const { blockUser } = require('./blockUser')
const { buildErrObject } = require('../../../middleware/utils')
const LOGIN_ATTEMPTS = 5
/**
* Adds one attempt to loginAttempts, then compares loginAttempts with the constant LOGIN_ATTEMPTS, if is less returns wrong password, else returns blockUser function
* @param {Object} user - user object
*/
const passwordsDoNotMatch = async (user = {}) => {
return new Promise(async (resolve, reject) => {
try {
user.loginAttempts += 1
await saveLoginAttemptsToDB(user)
if (user.loginAttempts <= LOGIN_ATTEMPTS) {
return reject(buildErrObject(409, 'WRONG_PASSWORD'))
}
resolve(await blockUser(user))
} catch (error) {
throw error
}
})
}
module.exports = { passwordsDoNotMatch }