Skip to content

Commit

Permalink
chore: refactor to use fastify validations
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Mar 9, 2024
1 parent dd1d943 commit 3e88e7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/client/routes/account/account.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ const Account = () => {
try {
const updatedUserInfo = await updateUser(values);

if (updatedUserInfo.error || [401, 500].includes(updatedUserInfo.statusCode)) {
if (updatedUserInfo.error || [400, 401, 500].includes(updatedUserInfo.statusCode)) {
setError(
updatedUserInfo.error
? updatedUserInfo.error
updatedUserInfo.message
? updatedUserInfo.message
: 'Could not update your user profile'
);

Expand Down
36 changes: 15 additions & 21 deletions src/server/controllers/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ async function account(fastify) {
'/update',
{
preValidation: [fastify.authenticate],
schema: {
body: {
type: 'object',
required: ['currentPassword', 'newPassword', 'confirmNewPassword', 'email'],
properties: {
currentPassword: { type: 'string', default: '' },
newPassword: { type: 'string', maxLength: 50, minLength: 5, default: '' },
confirmNewPassword: { type: 'string', default: '' },
email: { type: 'string', default: '' },
generated: { type: 'boolean', default: false },
},
},
},
},
async (request, reply) => {
const {
currentPassword = '',
newPassword = '',
email = '',
confirmNewPassword = '',
generated = false,
} = request.body;
const { currentPassword, newPassword, email, confirmNewPassword, generated } =
request.body;

const data = {
generated,
Expand All @@ -54,13 +62,6 @@ async function account(fastify) {
}

if (newPassword) {
if (newPassword.length < PASSWORD_LENGTH) {
return reply.code(403).send({
type: 'newPassword',
error: `Password has to be longer than ${PASSWORD_LENGTH} characters`,
});
}

data.password = await hash(newPassword);
}

Expand All @@ -75,13 +76,6 @@ async function account(fastify) {
data.email = email;
}

if (!email && !newPassword) {
return reply.code(412).send({
type: 'no-data',
error: `Could not update your profile. Please set the fields you want to update.`,
});
}

if (newPassword !== confirmNewPassword) {
return reply.code(400).send({
type: 'confirmNewPassword',
Expand Down

0 comments on commit 3e88e7b

Please sign in to comment.