Skip to content

Commit f40d71b

Browse files
committed
Added defaulting on helpers
1 parent fde5693 commit f40d71b

30 files changed

+64
-56
lines changed

app/controllers/auth/helpers/blockIsExpired.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const LOGIN_ATTEMPTS = 5
44
* Checks that login attempts are greater than specified in constant and also that blockexpires is less than now
55
* @param {Object} user - user object
66
*/
7-
const blockIsExpired = (user) =>
8-
user.loginAttempts > LOGIN_ATTEMPTS && user.blockExpires <= new Date()
7+
const blockIsExpired = ({ loginAttempts = 0, blockExpires = '' }) =>
8+
loginAttempts > LOGIN_ATTEMPTS && blockExpires <= new Date()
99

1010
module.exports = { blockIsExpired }

app/controllers/auth/helpers/blockUser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { buildErrObject } = require('../../../middleware/utils')
77
* Blocks a user by setting blockExpires to the specified date based on constant HOURS_TO_BLOCK
88
* @param {Object} user - user object
99
*/
10-
const blockUser = (user) => {
10+
const blockUser = (user = {}) => {
1111
return new Promise((resolve, reject) => {
1212
user.blockExpires = addHours(new Date(), HOURS_TO_BLOCK)
1313
user.save((err, result) => {

app/controllers/auth/helpers/checkLoginAttemptsAndBlockExpires.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { buildErrObject } = require('../../../middleware/utils')
55
*
66
* @param {Object} user - user object.
77
*/
8-
const checkLoginAttemptsAndBlockExpires = (user) => {
8+
const checkLoginAttemptsAndBlockExpires = (user = {}) => {
99
return new Promise((resolve, reject) => {
1010
// Let user try to login again after blockexpires, resets user loginAttempts
1111
if (blockIsExpired(user)) {

app/controllers/auth/helpers/checkPermissions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const { itemNotFound, buildErrObject } = require('../../../middleware/utils')
66
* @param {Object} data - data object
77
* @param {*} next - next callback
88
*/
9-
const checkPermissions = (data, next) => {
9+
const checkPermissions = ({ id = '', roles = [] }, next) => {
1010
return new Promise((resolve, reject) => {
11-
User.findById(data.id, (err, result) => {
11+
User.findById(id, (err, result) => {
1212
itemNotFound(err, result, 'NOT_FOUND')
13-
if (data.roles.indexOf(result.role) > -1) {
13+
if (roles.indexOf(result.role) > -1) {
1414
return resolve(next())
1515
}
1616
return reject(buildErrObject(401, 'UNAUTHORIZED'))

app/controllers/auth/helpers/findForgotPassword.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { itemNotFound } = require('../../../middleware/utils')
55
* Checks if a forgot password verification exists
66
* @param {string} id - verification id
77
*/
8-
const findForgotPassword = (id) => {
8+
const findForgotPassword = (id = '') => {
99
return new Promise((resolve) => {
1010
ForgotPassword.findOne(
1111
{

app/controllers/auth/helpers/findUser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { itemNotFound } = require('../../../middleware/utils')
55
* Finds user by email
66
* @param {string} email - user´s email
77
*/
8-
const findUser = (email) => {
8+
const findUser = (email = '') => {
99
return new Promise((resolve) => {
1010
User.findOne(
1111
{

app/controllers/auth/helpers/findUserById.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { itemNotFound } = require('../../../middleware/utils')
55
* Finds user by ID
66
* @param {string} id - user´s id
77
*/
8-
const findUserById = (userId) => {
8+
const findUserById = (userId = '') => {
99
return new Promise((resolve) => {
1010
User.findById(userId, (err, item) => {
1111
itemNotFound(err, item, 'USER_DOES_NOT_EXIST')

app/controllers/auth/helpers/findUserToResetPassword.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { itemNotFound } = require('../../../middleware/utils')
55
* Finds user by email to reset password
66
* @param {string} email - user email
77
*/
8-
const findUserToResetPassword = (email) => {
8+
const findUserToResetPassword = (email = '') => {
99
return new Promise((resolve) => {
1010
User.findOne(
1111
{

app/controllers/auth/helpers/forgotPasswordResponse.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
* Builds an object with created forgot password object, if env is development or testing exposes the verification
33
* @param {Object} item - created forgot password object
44
*/
5-
const forgotPasswordResponse = (item) => {
5+
const forgotPasswordResponse = ({ email = '', verification = '' }) => {
66
let data = {
77
msg: 'RESET_EMAIL_SENT',
8-
email: item.email
8+
email
99
}
1010
if (process.env.NODE_ENV !== 'production') {
1111
data = {
1212
...data,
13-
verification: item.verification
13+
verification
1414
}
1515
}
1616
return data

app/controllers/auth/helpers/generateToken.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { encrypt } = require('../../../middleware/auth')
55
* Generates a token
66
* @param {Object} user - user object
77
*/
8-
const generateToken = (user) => {
8+
const generateToken = (user = '') => {
99
// Gets expiration time
1010
const expiration =
1111
Math.floor(Date.now() / 1000) + 60 * process.env.JWT_EXPIRATION_IN_MINUTES

0 commit comments

Comments
 (0)