Skip to content

Commit

Permalink
fix: leverage the fastify schema to validate input (#278)
Browse files Browse the repository at this point in the history
* fix: set valid values

* fix: use shorthand

* chore: refactor to use fastify validations

* fix: set lower case restrict org email

* chore: update the error message handling

* fix: use fastify validation for the request body

bonus: fixed how the deletion of files is handled, as currently it did not work as intended

* chore: use fastify validation

* fix: use fastify validation

* fix: use fastify validation
  • Loading branch information
bjarneo authored Mar 9, 2024
1 parent 57f290f commit 4b3fb88
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 198 deletions.
7 changes: 4 additions & 3 deletions src/client/routes/account/account.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ 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
: t('account.account.can_not_update_profile')

);

return;
Expand Down
2 changes: 1 addition & 1 deletion src/client/routes/account/settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Settings = () => {
disable_users: false,
disable_user_account_creation: false,
disable_file_upload: false,
Restrict_organization_email: '',
restrict_organization_email: '',
},
});

Expand Down
6 changes: 3 additions & 3 deletions src/client/routes/home/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ const Home = () => {
const json = await createSecret(body);

if (json.statusCode !== 201) {
if (json.statusCode === 403) {
setError(json.error);
if (json.statusCode === 400) {
setError(json.message);
}

if (json.message === 'request file too large, please check multipart config') {
form.setErrors({ files: 'The file size is too large' });
} else {
form.setErrors({ files: json.error });
form.setErrors({ files: json.message });
}

setCreatingSecret(false);
Expand Down
12 changes: 6 additions & 6 deletions src/client/routes/signup/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ const SignUp = () => {
const onSignUp = async (values) => {
const data = await signUp(values.email, values.username, values.password);

if (data.statusCode === 403) {
setError(data.error);
if ([400, 403].indexOf(data.statusCode) > -1) {
setError(data.message);

setSuccess(false);

return;
}

if (data.error) {
if (data.type && data.message) {
form.setErrors({
username: data.type == 'username' ? data.error : '',
password: data.type == 'password' ? data.error : '',
email: data.type == 'email' ? data.error : '',
username: data.type == 'username' ? data.message : '',
password: data.type == 'password' ? data.message : '',
email: data.type == 'email' ? data.message : '',
});

setSuccess(false);
Expand Down
38 changes: 15 additions & 23 deletions src/server/controllers/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import emailValidator from 'email-validator';
import { compare, hash } from '../helpers/password.js';
import prisma from '../services/prisma.js';

const PASSWORD_LENGTH = 5;

async function account(fastify) {
fastify.get(
'/',
Expand All @@ -29,15 +27,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 +60,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 +74,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
22 changes: 17 additions & 5 deletions src/server/controllers/admin/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,26 @@ async function settings(fastify) {
'/',
{
preValidation: [fastify.authenticate, fastify.admin],
schema: {
body: {
type: 'object',
properties: {
disable_users: { type: 'boolean', default: false },
disable_user_account_creation: { type: 'boolean', default: false },
read_only: { type: 'boolean', default: false },
disable_file_upload: { type: 'boolean', default: false },
restrict_organization_email: { type: 'string', default: '' },
},
},
},
},
async (request) => {
const {
disable_users = false,
disable_user_account_creation = false,
read_only = false,
disable_file_upload = false,
restrict_organization_email = '',
disable_users,
disable_user_account_creation,
read_only,
disable_file_upload,
restrict_organization_email,
} = request.body;

const settings = await prisma.settings.upsert({
Expand Down
Loading

0 comments on commit 4b3fb88

Please sign in to comment.