Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add login attempts limit Hint and block due time. #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PSheon wants to merge 1 commit into davellanedam:development from PSheon:feat-expose-information
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/controllers/auth/helpers/blockUser.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { addHours } = require('date-fns')
const HOURS_TO_BLOCK = 2

const { formatDuetime } = require('./formatDuetime')
const { buildErrObject } = require('../../../middleware/utils')

/**
Expand All @@ -15,7 +16,12 @@ const blockUser = (user = {}) => {
return reject(buildErrObject(422, err.message))
}
if (result) {
return resolve(buildErrObject(409, 'BLOCKED_USER'))
return resolve(
buildErrObject(
409,
`BLOCKED_USER_UNTIL_${formatDuetime(user.blockExpires)}`
)
)
}
})
})
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/auth/helpers/formatDuetime.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const moment = require('moment')
Copy link
Owner

@davellanedam davellanedam Nov 24, 2020
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need moment, we are already using date-fns, can you please use that instead?

PSheon and imwexpex reacted with thumbs up emoji
/**
* Calculate user block due time.
* @param {Date} blockExpires - user block expires
*/
const formatDuetime = (blockExpires = Date()) =>
moment(blockExpires).format('YYYY_MM_DD_mm_ss')

module.exports = { formatDuetime }
17 changes: 17 additions & 0 deletions app/controllers/auth/helpers/generatePasswordErrorMsg.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const LOGIN_ATTEMPTS_LIMIT = 5

/**
* Generate error message when user tries to login.
* @param {Number} loginAttempts user's login attempts
*/
const generatePasswordErrorMsg = (loginAttempts = 0) =>
parseInt(LOGIN_ATTEMPTS_LIMIT - loginAttempts, 10) > 0
? `WRONG_PASSWORD_${parseInt(
LOGIN_ATTEMPTS_LIMIT - loginAttempts,
10
)}_ATTEMPTS_LEFT`
: 'BLOCKED_USER'

module.exports = {
generatePasswordErrorMsg
}
6 changes: 5 additions & 1 deletion app/controllers/auth/helpers/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const { updatePassword } = require('./updatePassword')
const { userIsBlocked } = require('./userIsBlocked')
const { verificationExists } = require('./verificationExists')
const { verifyUser } = require('./verifyUser')
const { formatDuetime } = require('./formatDuetime')
const { generatePasswordErrorMsg } = require('./generatePasswordErrorMsg')

module.exports = {
blockIsExpired,
Expand All @@ -49,5 +51,7 @@ module.exports = {
updatePassword,
userIsBlocked,
verificationExists,
verifyUser
verifyUser,
formatDuetime,
generatePasswordErrorMsg
}
6 changes: 5 additions & 1 deletion app/controllers/auth/helpers/passwordsDoNotMatch.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { saveLoginAttemptsToDB } = require('./saveLoginAttemptsToDB')
const { blockUser } = require('./blockUser')
const { buildErrObject } = require('../../../middleware/utils')
const { generatePasswordErrorMsg } = require('./generatePasswordErrorMsg')

const LOGIN_ATTEMPTS = 5

/**
Expand All @@ -13,7 +15,9 @@ const passwordsDoNotMatch = async (user = {}) => {
user.loginAttempts += 1
await saveLoginAttemptsToDB(user)
if (user.loginAttempts <= LOGIN_ATTEMPTS) {
return reject(buildErrObject(409, 'WRONG_PASSWORD'))
return reject(
buildErrObject(409, generatePasswordErrorMsg(user.loginAttempts))
)
}

resolve(await blockUser(user))
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/auth/helpers/userIsBlocked.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { formatDuetime } = require('./formatDuetime')
const { buildErrObject } = require('../../../middleware/utils')

/**
Expand All @@ -7,7 +8,12 @@ const { buildErrObject } = require('../../../middleware/utils')
const userIsBlocked = (user = {}) => {
return new Promise((resolve, reject) => {
if (user.blockExpires > new Date()) {
return reject(buildErrObject(409, 'BLOCKED_USER'))
return reject(
buildErrObject(
409,
`USER_BLOCK_UNTIL_${formatDuetime(user.blockExpires)}`
)
)
}
resolve(true)
})
Expand Down
1 change: 1 addition & 0 deletions package.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"express-expeditious": "^5.1.1",
"express-validator": "^6.6.1",
"jsonwebtoken": "^8.5.1",
"moment": "^2.29.1",
"mongoose": "^5.10.14",
"mongoose-paginate-v2": "^1.3.11",
"morgan": "^1.10.0",
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /