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

Commit

Permalink
updated form submission controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Manas2403 committed Jun 19, 2023
1 parent e4ede90 commit e8b97cd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion controllers/form.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function createForm(req, res) {
let formId = generateRandomString(16);
let submisssionLinkGeneratedAt = Date.now();
const { hostUrl } = req.body;
let encryptedStr = await encryptString(
let encryptedStr = encryptString(
JSON.stringify({
formId,
submisssionLinkGeneratedAt,
Expand Down
10 changes: 7 additions & 3 deletions controllers/formSubmission.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ import Form from '../models/form.model.js';
export async function createFormSubmission(req, res) {
try {
const encryptedStr = req.query.formRef;
const decryptedStr = await dcryptString(encryptedStr);
const decryptedStr = dcryptString(encryptedStr);
console.log(decryptedStr);
const { formId, submisssionLinkGeneratedAt } = JSON.parse(decryptedStr);
const form = await Form.findOne({ formId: formId });
if (!form) return response_400(res, 'Form not found');
if (form.submisssionLinkGeneratedAt !== submisssionLinkGeneratedAt)
let incomingTime = new Date(form.submisssionLinkGeneratedAt).getTime();
console.log(incomingTime);
console.log(submisssionLinkGeneratedAt);
if (incomingTime !== submisssionLinkGeneratedAt)
return response_400(res, 'Link expired');
const schema = form.schema;
const submissionData = req.body.data;
const submissionData = req.body;
const isValid = validateSchema(schema, submissionData);
if (!isValid) return response_400(res, 'Invalid data');
const submission = await prisma.formSubmission.create({
Expand Down
2 changes: 1 addition & 1 deletion routes/formSubmission.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import verifiedMiddleware from '../middlewares/verify.middleware.js';
import { Router } from 'express';
import { createFormSubmission } from '../controllers/formSubmission.controller.js';
const router = Router();
router.post('/submit', verifiedMiddleware, createFormSubmission);
router.post('/submit', createFormSubmission);
export default router;
37 changes: 19 additions & 18 deletions utils/password.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import dotev from 'dotenv';

dotev.config()
dotev.config();

import crypto from 'crypto';
import jwt from 'jsonwebtoken';
import CryptoJS from "crypto-js";
import CryptoJS from 'crypto-js';


export function getJwt(object,expiresIn='30d'){
const secret = process.env.SECRET;
export function getJwt(object, expiresIn = '30d') {
const secret = process.env.SECRET;
const options = {
algorithm: 'HS256', // Use HS256 algorithm
expiresIn: expiresIn
expiresIn: expiresIn,
};

// Sign the JWT with the payload, secret key, and options
Expand All @@ -20,20 +19,22 @@ export function getJwt(object,expiresIn='30d'){
return token;
}

export async function hash_password(password){
var salt = process.env.SECRET;
var genHash = crypto
.pbkdf2Sync(password, salt, 10000, 64, "sha512")
.toString("hex");
return genHash;
export async function hash_password(password) {
var salt = process.env.SECRET;
var genHash = crypto
.pbkdf2Sync(password, salt, 10000, 64, 'sha512')
.toString('hex');
return genHash;
}

export async function encryptString(str){
const encrypted = await CryptoJS.AES.encrypt(str, process.env.SECRET);
return encrypted;
export function encryptString(str) {
const encrypted = CryptoJS.AES.encrypt(str, process.env.SECRET);
console.log(encrypted.toString());
return encrypted.toString();
}

export async function dcryptString(encryptedStr){
const decrypted =await CryptoJS.AES.decrypt(encryptedStr, process.env.SECRET);
return decrypted;
export function dcryptString(encryptedStr) {
encryptedStr = encryptedStr.replace(/ /g, '+');
const decrypted = CryptoJS.AES.decrypt(encryptedStr, process.env.SECRET);
return decrypted.toString(CryptoJS.enc.Utf8);
}

0 comments on commit e8b97cd

Please sign in to comment.