Skip to content
Open
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
11 changes: 10 additions & 1 deletion routes/resetPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ import challengeUtils = require('../lib/challengeUtils')
const users = require('../data/datacache').users
const security = require('../lib/insecurity')

function setResetRequestCookie(res: Response) {
res.cookie('resetRequest', '1', { maxAge: 600000, httpOnly: true, sameSite: 'Strict' })
}

module.exports = function resetPassword () {
return ({ body, connection }: Request, res: Response, next: NextFunction) => {
const email = body.email
const answer = body.answer
const newPassword = body.new
const repeatPassword = body.repeat
if (!email || !answer) {
setResetRequestCookie(res)
next(new Error('Blocked illegal activity by ' + connection.remoteAddress))
} else if (!newPassword || newPassword === 'undefined') {
setResetRequestCookie(res)
res.status(401).send(res.__('Password cannot be empty.'))
} else if (newPassword !== repeatPassword) {
setResetRequestCookie(res)
res.status(401).send(res.__('New and repeated password do not match.'))
} else {
SecurityAnswerModel.findOne({
Expand All @@ -37,6 +44,7 @@ module.exports = function resetPassword () {
UserModel.findByPk(data.UserId).then((user: UserModel | null) => {
user?.update({ password: newPassword }).then((user: UserModel) => {
verifySecurityAnswerChallenges(user, answer)
setResetRequestCookie(res)
res.json({ user })
}).catch((error: unknown) => {
next(error)
Expand All @@ -45,9 +53,10 @@ module.exports = function resetPassword () {
next(error)
})
} else {
res.status(401).send(res.__('Wrong answer to security question.'))
next(new Error('Blocked illegal activity by ' + connection.remoteAddress))
}
}).catch((error: unknown) => {
setResetRequestCookie(res)
next(error)
})
}
Expand Down