Skip to content
This repository has been archived by the owner on Dec 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #138 from GammaMicrowave/main
Browse files Browse the repository at this point in the history
minor changes in backend
  • Loading branch information
BuddyLongLegs committed Jun 9, 2023
2 parents cab24e5 + c3d93ae commit 7022460
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
7 changes: 4 additions & 3 deletions controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ export async function logIn(req, res) {
secret: jwtToken,
});
} catch (error) {
console.log(error);
return response_500(res, 'Internal server error', error);
}

// return response_200(res, 'Hello there!');
}
export function greet(req, res) {
response_200(res, 'Hello There');
Expand All @@ -57,13 +55,16 @@ export async function signUp(req, res) {
const { name, email, recaptcha_token } = req.body;
if (!(name && email && recaptcha_token && req.body.password))
return response_400(res, 'Some parameters are missing!');

if (req.body.password.length < 6)
return response_400(res, 'Password must be longer than 6 letters');

if (!validator.isEmail(email)) return response_400(res, 'Email is invalid');
const checkUser = await User.findOne({ email });
if (checkUser) return response_400(res, 'Email already in use');
if (!verifycaptcha(recaptcha_token))
return response_400(res, 'Captcha was found incorrect');

const password = await hash_password(req.body.password);
let newUser = User({
email,
Expand Down
40 changes: 22 additions & 18 deletions controllers/form.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ import { generateRandomString } from '../utils/generateRandomString.js';

export async function updateForm(req, res) {
const id = req.params.id;
const request = req.body;

const {
name,
hasRecaptcha,
hasFileField,
schema,
password,
recaptcha_token,
} = req.body;

if (
!(
'name' in request ||
'hasRecaptcha' in request ||
'hasFileField' in request ||
'schema' in request ||
'password' in request ||
'recaptcha_token' in request
)
!name ||
!hasRecaptcha ||
!hasFileField ||
!schema ||
!password ||
!recaptcha_token
) {
response_400(res, 'Fields missing for updation');
}
let { name, hasRecaptcha, hasFileField, schema, password, recaptcha_token } =
request;

if (!verifycaptcha(recaptcha_token))
return response_400(res, 'Captcha not verified');
password = await hash_password(password);
Expand Down Expand Up @@ -93,7 +99,6 @@ export async function createForm(req, res) {
'Number of forms in this project has already reached max limit of 5.',
);
}

try {
let formId = generateRandomString(16);
let submisssionLinkGeneratedAt = Date.now();
Expand Down Expand Up @@ -220,21 +225,21 @@ export async function deleteForm(req, res) {
select: '_id name email passwordHash',
});
if (!form) {
return res.status(400).json({ msg: "Form not found" });
return res.status(400).json({ msg: 'Form not found' });
}
const isOwner = req.user._id === form.project.owner._id;
if (!isOwner) {
return res.status(401).json({ msg: "Unauthorized" });
return res.status(401).json({ msg: 'Unauthorized' });
}
const password = req.body.password;
password = await hash_password(password); // Assuming the password is provided in the request body
if (password !== form.project.owner.passwordHash) {
return res.status(400).json({ msg: "User is not the owner" });
return res.status(400).json({ msg: 'User is not the owner' });
}
await form.deleteOne();
res.status(200).json({ data: form, msg: "Form deleted successfully" });
res.status(200).json({ data: form, msg: 'Form deleted successfully' });
} catch (error) {
res.status(500).json({ msg: "An error occurred while deleting the form" });
res.status(500).json({ msg: 'An error occurred while deleting the form' });
}
}

Expand All @@ -256,4 +261,3 @@ export async function generateSubmissionLink(req, res) {
return response_500(res, 'Server Error', error);
}
}

2 changes: 1 addition & 1 deletion controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import User from '../models/user.model.js';

export function getVerificationLink(req, res) {
if (req.user.verified)
return response_400(res, 'The user is already verified');
return response_200(res, 'The user is already verified');
const payload = {
name: req.user.name,
email: req.user.email,
Expand Down

0 comments on commit 7022460

Please sign in to comment.