-
Notifications
You must be signed in to change notification settings - Fork 2
feature/#38 Added reset password endpoint #39
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ import { | |
mapUserFromApiToModel, | ||
mapUserUpdateFromApiToModel, | ||
} from './user.mappers.js'; | ||
import { validationPostUser, validationUpdateUser } from './validations/index.js'; | ||
import { validationPostUser, validationUpdateUser, validationChangePassword } from './validations/index.js'; | ||
import * as apiModel from './user.api-model.js'; | ||
import * as model from '#dals/user/user.model.js'; | ||
|
||
|
@@ -116,4 +116,38 @@ userApi | |
} catch (error) { | ||
next(error); | ||
} | ||
}) | ||
.post('/change-password', async (req, res, next) => { | ||
try { | ||
const passwordData: apiModel.ChangePasswordParams = req.body; | ||
const validationResult = await validationChangePassword(passwordData); | ||
|
||
if (validationResult.succeded) { | ||
const hashedPassword = await hash(passwordData.nuevaContraseña); | ||
const user = await userRepository.getUser(passwordData.id); | ||
|
||
await userRepository.saveUser({ | ||
...user, | ||
contraseña: hashedPassword, | ||
esContraseñaTemporal: false, | ||
}); | ||
|
||
res.sendStatus(204); | ||
} else { | ||
const statusCode = (() => { | ||
switch (validationResult.error?.error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
case CustomInternalCodes.UserNotFound: | ||
return 422; | ||
case CustomInternalCodes.InvalidPassword: | ||
return 401; | ||
default: | ||
return 400; | ||
} | ||
})(); | ||
|
||
res.status(statusCode).send(validationResult.error); | ||
} | ||
} catch (error) { | ||
next(error); | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './user-validations.js'; | ||
export * from './update-user-validation.js'; | ||
export * from './password-validation.js'; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,24 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { CustomInternalCodes, ValidationInfo } from '#common/custom-error/index.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { verifyHash } from '#common/helpers/index.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { userRepository } from '#dals/user/user.repository.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import * as apiModel from '../user.api-model.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export const validationChangePassword = async ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
passwordData: apiModel.ChangePasswordParams | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
): Promise<ValidationInfo> => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!passwordData.id || !passwordData.contraseñaActual || !passwordData.nuevaContraseña) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return { succeded: false, error: { error: CustomInternalCodes.FieldNotInformed } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const user = await userRepository.getUser(passwordData.id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!user) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return { succeded: false, error: { error: CustomInternalCodes.UserNotFound } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const isValidPassword = await verifyHash(passwordData.contraseñaActual, user.contraseña); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!isValidPassword) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return { succeded: false, error: { error: CustomInternalCodes.InvalidPassword } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return { succeded: true }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+10
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The property
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] This call fetches the user again after validation already retrieved it; you could return the user from the validation step to reduce an extra database query.
Copilot uses AI. Check for mistakes.