From 1fce91a8d6e6b298cbd185de5dd1a8268774ee36 Mon Sep 17 00:00:00 2001 From: ygowthamr Date: Mon, 14 Oct 2024 13:27:25 +0530 Subject: [PATCH 1/2] Solved App crashes when entering an existing email ID saved in `info_table` --- file_upload/form_db.js | 148 ++++++++++++++--------- public/form_filling.html | 248 ++++++++++++++++++++------------------- 2 files changed, 221 insertions(+), 175 deletions(-) diff --git a/file_upload/form_db.js b/file_upload/form_db.js index f1e95c0..bda71bd 100644 --- a/file_upload/form_db.js +++ b/file_upload/form_db.js @@ -1,62 +1,96 @@ -const express = require('express') -const mysql = require('mysql') -const db = require('../config/mysql_connection') -const {decodeAccessToken}=require('../login-system/token') +const express = require("express"); +const mysql = require("mysql"); +const db = require("../config/mysql_connection"); +const { decodeAccessToken } = require("../login-system/token"); const app = express(); app.use(express.json()); -const info=(req,res)=>{ - const decodedtoken = decodeAccessToken(req.headers.authorization); - if (!decodedtoken || !decodedtoken.user) { - console.error('Invalid or missing user information in the token'); - return res.status(401).send('Unauthorized'); - } - const userid=decodedtoken.user; - console.log(userid) - const name=req.body.name.trim(); - const email=req.body.email.trim(); - const col_name=req.body.col_name.trim(); - const state=req.body.state; - const course=req.body.course.trim(); - const year=req.body.year; - const dept=req.body.dept.trim(); - db.getConnection(async(err,connection)=>{ - if(err) throw err; - const sql="INSERT INTO info_table VALUES (?,?,?,?,?,?,?)" - const sqlinsert=mysql.format(sql,[userid,name,email,col_name,state,year,course]) - await connection.query(sqlinsert,async (err,result)=>{ - if(err) throw err; - connection.release(); - console.log("Data Saved"); - res.sendStatus(200); - }) - }) -} - -const check=(req,res)=>{ - const decodedtoken = decodeAccessToken(req.headers.authorization); - if (!decodedtoken || !decodedtoken.user) { - console.error('Invalid or missing user information in the token'); - return res.status(401).send('Unauthorized'); - } - const userid=decodedtoken.user; - db.getConnection(async(err,connection)=>{ - if(err) throw err; - const search="SELECT * FROM info_table where id=?" - const searchquery=mysql.format(search,[userid]) - await connection.query(searchquery,async(err,result)=>{ - if(err) throw err; - if(result.length!=0){ - console.log('info checked') - res.sendStatus(201) - connection.release() - } - else{ - connection.release() - } - }) - }) -} +const info = (req, res) => { + const decodedtoken = decodeAccessToken(req.headers.authorization); + if (!decodedtoken || !decodedtoken.user) { + console.error("Invalid or missing user information in the token"); + return res.status(401).send("Unauthorized"); + } + + const userid = decodedtoken.user; + + const name = req.body.name.trim(); + const email = req.body.email.trim(); + const col_name = req.body.col_name.trim(); + const state = req.body.state; + const course = req.body.course.trim(); + const year = req.body.year; + const dept = req.body.dept.trim(); + + db.getConnection(async (err, connection) => { + if (err) throw err; + + // Check if the email already exists in the info_table + const emailCheckQuery = + "SELECT COUNT(*) AS count FROM info_table WHERE email = ?"; + const emailCheckSql = mysql.format(emailCheckQuery, [email]); + + await connection.query(emailCheckSql, async (err, results) => { + if (err) { + connection.release(); + throw err; + } + + // Check if the email already exists + if (results[0].count > 0) { + connection.release(); + return res.sendStatus(400); + } + + // Proceed to insert the data if the email does not exist + const sql = "INSERT INTO info_table VALUES (?,?,?,?,?,?,?)"; + const sqlInsert = mysql.format(sql, [ + userid, + name, + email, + col_name, + state, + year, + course, + ]); + + await connection.query(sqlInsert, async (err, result) => { + if (err) { + connection.release(); + throw err; + } + + connection.release(); + console.log("Data Saved"); + res.sendStatus(200); + }); + }); + }); +}; + +const check = (req, res) => { + const decodedtoken = decodeAccessToken(req.headers.authorization); + if (!decodedtoken || !decodedtoken.user) { + console.error("Invalid or missing user information in the token"); + return res.status(401).send("Unauthorized"); + } + const userid = decodedtoken.user; + db.getConnection(async (err, connection) => { + if (err) throw err; + const search = "SELECT * FROM info_table where id=?"; + const searchquery = mysql.format(search, [userid]); + await connection.query(searchquery, async (err, result) => { + if (err) throw err; + if (result.length != 0) { + console.log("info checked"); + res.sendStatus(201); + connection.release(); + } else { + connection.release(); + } + }); + }); +}; // exporting info -module.exports={info,check}; \ No newline at end of file +module.exports = { info, check }; diff --git a/public/form_filling.html b/public/form_filling.html index 15e9218..6797d8e 100644 --- a/public/form_filling.html +++ b/public/form_filling.html @@ -28,136 +28,148 @@
  • -
    -
    -

    User Form

    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    - - -
    - -
    -
    - - -
    + + Date: Mon, 14 Oct 2024 14:59:26 +0530 Subject: [PATCH 2/2] Solved Hardcoded backend URL in the application --- public/dashboard.html | 10 +- public/fac_login.html | 18 ++-- public/faculty.html | 18 ++-- public/form_filling.html | 6 +- public/login.html | 18 ++-- public/script/approval.js | 4 +- public/script/paper_allotment.js | 4 +- public/script/stk_mainpage.js | 153 +++++++++++++++---------------- public/signup.html | 4 +- public/stk_dashboard.html | 86 ++++++++--------- public/stk_login.html | 24 ++--- public/stk_signup.html | 2 +- stakeholder/allotment.js | 152 +++++++++++++++--------------- views/fac_signup.ejs | 2 +- 14 files changed, 253 insertions(+), 248 deletions(-) diff --git a/public/dashboard.html b/public/dashboard.html index e99986c..d2431c9 100644 --- a/public/dashboard.html +++ b/public/dashboard.html @@ -81,7 +81,7 @@ const state = document.getElementById('state'); const year = document.getElementById('year'); const course = document.getElementById('course'); - const response = await fetch('http://localhost:3000/dashboard', { + const response = await fetch('/dashboard', { headers: { 'Authorization': `Bearer ${token}`, }, @@ -134,7 +134,7 @@ const token = localStorage.getItem('accessToken'); const headers = new Headers(); headers.append('Authorization', `Bearer ${token}`); - const response = await fetch('http://localhost:3000/result', { + const response = await fetch('/result', { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, @@ -152,8 +152,8 @@ const result = document.getElementById('result') result.innerHTML = "Result not declared yet!" result.style.color = "red" - result.style.padding="16px" - result.style.fontSize="18px" + result.style.padding = "16px" + result.style.fontSize = "18px" } else { console.log("an error occured") @@ -162,4 +162,4 @@ - + \ No newline at end of file diff --git a/public/fac_login.html b/public/fac_login.html index ef0be76..3d0a2bd 100644 --- a/public/fac_login.html +++ b/public/fac_login.html @@ -44,14 +44,14 @@

    (for F - + \ No newline at end of file diff --git a/public/faculty.html b/public/faculty.html index 0235aae..b3b759e 100644 --- a/public/faculty.html +++ b/public/faculty.html @@ -127,21 +127,21 @@

    Quality and Guidelines

    - + \ No newline at end of file diff --git a/public/form_filling.html b/public/form_filling.html index 6797d8e..e074a4a 100644 --- a/public/form_filling.html +++ b/public/form_filling.html @@ -120,7 +120,7 @@ headers.append('Authorization', `Bearer ${token}`); try { - const response = await fetch('http://localhost:3000/info', { + const response = await fetch('/info', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -129,8 +129,6 @@ body: JSON.stringify(data), }); - console.log(response.status); - if (response.status == 200) { console.log("Information saved"); result.style.display = "inline-block"; @@ -157,7 +155,7 @@ const token = localStorage.getItem('accessToken'); const headers = new Headers(); headers.append('Authorization', `Bearer ${token}`); - const response = await fetch('http://localhost:3000/form_filling', { + const response = await fetch('/form_filling', { headers: { 'Authorization': `Bearer ${token}`, }, diff --git a/public/login.html b/public/login.html index f7ad952..7463ddc 100644 --- a/public/login.html +++ b/public/login.html @@ -44,14 +44,14 @@

    (for R - + \ No newline at end of file diff --git a/public/script/approval.js b/public/script/approval.js index a52a432..5eee361 100644 --- a/public/script/approval.js +++ b/public/script/approval.js @@ -2,7 +2,7 @@ const fetchuploadfiles = async () => { const token = localStorage.getItem("accessToken"); const headers = new Headers(); headers.append("Authorization", `Bearer ${token}`); - const response = await fetch("http://localhost:3000/api/stk_papers", { + const response = await fetch("/api/stk_papers", { headers: { Authorization: `Bearer ${token}`, }, @@ -116,7 +116,7 @@ const approval = async (id) => { const token = localStorage.getItem("accessToken"); const headers = new Headers(); headers.append("Authorization", `Bearer ${token}`); - const response = await fetch(`http://localhost:3000/approval?id=${id}`, { + const response = await fetch(`/approval?id=${id}`, { headers: { Authorization: `Bearer ${token}`, }, diff --git a/public/script/paper_allotment.js b/public/script/paper_allotment.js index c1c80fa..8ccd76c 100644 --- a/public/script/paper_allotment.js +++ b/public/script/paper_allotment.js @@ -2,7 +2,7 @@ const fetchuploadfiles = async () => { const token = localStorage.getItem("accessToken"); const headers = new Headers(); headers.append("Authorization", `Bearer ${token}`); - const response = await fetch("http://localhost:3000/allotment", { + const response = await fetch("/allotment", { headers: { Authorization: `Bearer ${token}`, }, @@ -109,7 +109,7 @@ const display = async (id) => { // allotment code for faculty const fac_allotment = async (id) => { const email = document.getElementById("fac_mail").value; - const response = fetch(`http://localhost:3000/paper_allot?id=${id}`, { + const response = fetch(`/paper_allot?id=${id}`, { method: "POST", headers: { "Content-Type": "application/json", diff --git a/public/script/stk_mainpage.js b/public/script/stk_mainpage.js index 8e02842..5665b66 100644 --- a/public/script/stk_mainpage.js +++ b/public/script/stk_mainpage.js @@ -1,13 +1,12 @@ -var icon =document.getElementById("icon"); - icon.onclick = function(){ - document.body.classList.toggle("dark-theme"); - if(document.body.classList.contains("dark-theme")){ - icon.src="images/sun.png"; - } - else{ - icon.src="images/moon.png"; - } - } +var icon = document.getElementById("icon"); +icon.onclick = function () { + document.body.classList.toggle("dark-theme"); + if (document.body.classList.contains("dark-theme")) { + icon.src = "images/sun.png"; + } else { + icon.src = "images/moon.png"; + } +}; document.addEventListener("DOMContentLoaded", function () { var currentStep = 1; showStep(currentStep); @@ -15,16 +14,18 @@ document.addEventListener("DOMContentLoaded", function () { document.querySelectorAll(".next-btn").forEach(function (button) { button.addEventListener("click", function () { if (currentStep < document.querySelectorAll(".form-step").length) { - const curStepElement = document.querySelector(`input[name="step${currentStep}"]:checked`); + const curStepElement = document.querySelector( + `input[name="step${currentStep}"]:checked` + ); if (!curStepElement || curStepElement.value === undefined) { - const para = document.createElement('p'); + const para = document.createElement("p"); para.textContent = "*require filled"; - para.style.color='red' - const element=document.getElementById(`step${currentStep}`) + para.style.color = "red"; + const element = document.getElementById(`step${currentStep}`); element.appendChild(para); - setTimeout(()=>{ - para.style.display='none' - },3000) + setTimeout(() => { + para.style.display = "none"; + }, 3000); } else { currentStep++; showStep(currentStep); @@ -49,106 +50,102 @@ document.addEventListener("DOMContentLoaded", function () { document.getElementById("step" + step).style.display = "block"; } - }); const set = async () => { - const level1 = document.querySelector('input[name="step1"]:checked').value - const level2 = document.querySelector('input[name="step2"]:checked').value - const level3 = document.querySelector('input[name="step3"]:checked').value - const level4 = document.querySelector('input[name="step4"]:checked').value - const level5 = document.querySelector('input[name="step5"]:checked') - const topic = document.querySelector('input[type="text"]') - let value1,value2,value3,value4 - switch(level1){ + const level1 = document.querySelector('input[name="step1"]:checked').value; + const level2 = document.querySelector('input[name="step2"]:checked').value; + const level3 = document.querySelector('input[name="step3"]:checked').value; + const level4 = document.querySelector('input[name="step4"]:checked').value; + const level5 = document.querySelector('input[name="step5"]:checked'); + const topic = document.querySelector('input[type="text"]'); + let value1, value2, value3, value4; + switch (level1) { case "high": - value1=4 + value1 = 4; break; case "medium": - value1=3 + value1 = 3; break; case "low": - value1=2 + value1 = 2; break; } - switch(level2){ + switch (level2) { case "high": - value2=4 + value2 = 4; break; case "medium": - value2=3 + value2 = 3; break; case "low": - value2=2 + value2 = 2; break; } - switch(level3){ + switch (level3) { case "high": - value3=4 + value3 = 4; break; case "medium": - value3=3 + value3 = 3; break; case "low": - value3=2 + value3 = 2; break; } - switch(level4){ + switch (level4) { case "high": - value4=4 + value4 = 4; break; case "medium": - value4=3 + value4 = 3; break; case "low": - value4=2 + value4 = 2; break; } const value5 = level5 ? level5.value : undefined; const topicval = topic ? topic.value : undefined; - console.log(value1, value2, value3, value4, value5, topicval) - const token = localStorage.getItem('accessToken'); + console.log(value1, value2, value3, value4, value5, topicval); + const token = localStorage.getItem("accessToken"); const headers = new Headers(); - headers.append('Authorization', `Bearer ${token}`); - const response = await fetch('http://localhost:3000/evaluation', { - method: 'POST', + headers.append("Authorization", `Bearer ${token}`); + const response = await fetch("/evaluation", { + method: "POST", headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}`, + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, }, body: JSON.stringify({ value1, value2, value3, value4, value5, topicval }), - }) + }); if (response.ok) { - console.log('Evaluation Criteria has been set') - const step5 = document.getElementById('step5') - const para = document.createElement('p') - para.textContent = "Evaluation Criteria has been set" - step5.appendChild(para) - const submit = document.querySelectorAll('button') - submit.disabled = true + console.log("Evaluation Criteria has been set"); + const step5 = document.getElementById("step5"); + const para = document.createElement("p"); + para.textContent = "Evaluation Criteria has been set"; + step5.appendChild(para); + const submit = document.querySelectorAll("button"); + submit.disabled = true; setTimeout(() => { - para.style.display = 'none' - }, 3000) - } - else if (response.status = 401) { - console.log("Criteria Already set") - const step5 = document.getElementById('step5') - const para = document.createElement('p') - para.textContent = "Criteria already set" - step5.appendChild(para) + para.style.display = "none"; + }, 3000); + } else if ((response.status = 401)) { + console.log("Criteria Already set"); + const step5 = document.getElementById("step5"); + const para = document.createElement("p"); + para.textContent = "Criteria already set"; + step5.appendChild(para); setTimeout(() => { - para.style.display = 'none' - }, 3000) - } - else { - console.log("An error occured") - const step5 = document.getElementById('step5') - const para = document.createElement('p') - para.textContent = "An error occured" - step5.appendChild(para) + para.style.display = "none"; + }, 3000); + } else { + console.log("An error occured"); + const step5 = document.getElementById("step5"); + const para = document.createElement("p"); + para.textContent = "An error occured"; + step5.appendChild(para); setTimeout(() => { - para.style.display = 'none' - }, 3000) + para.style.display = "none"; + }, 3000); } -} - +}; diff --git a/public/signup.html b/public/signup.html index 90198a6..ef5a2fe 100644 --- a/public/signup.html +++ b/public/signup.html @@ -77,7 +77,7 @@

    (for R } try { - const response = await fetch('http://localhost:3000/create_user', { + const response = await fetch('/create_user', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -119,4 +119,4 @@

    (for R - + \ No newline at end of file diff --git a/public/stk_dashboard.html b/public/stk_dashboard.html index 1e44ac2..7aabc71 100644 --- a/public/stk_dashboard.html +++ b/public/stk_dashboard.html @@ -86,52 +86,54 @@ .card-body h4 { padding: 8px 0px; } + body.dark-mode { - background-color: #121212; - color: #ffffff; - } + background-color: #121212; + color: #ffffff; + } header.dark-mode { - background-color: #1f1f1f; + background-color: #1f1f1f; } section.dark-mode { - background-color: #2c2c2c; + background-color: #2c2c2c; } .card.dark-mode { - background-color: #3a3a3a; - color: #ffffff; + background-color: #3a3a3a; + color: #ffffff; } .card.dark-mode .btn-link { - color: #ffffff; + color: #ffffff; } .card-body.dark-mode strong { - color: rgb(193, 15, 15); + color: rgb(193, 15, 15); } + body { - position: relative; + position: relative; } -
    - +
    - +
    - -
    -
    + +
    +

    Hii...harshoxfordgkp@gmail.com

    - +
    @@ -209,8 +211,8 @@

    - - + + @@ -219,27 +221,27 @@

    crossorigin="anonymous"> - + \ No newline at end of file diff --git a/public/stk_login.html b/public/stk_login.html index 6d45799..3bce404 100644 --- a/public/stk_login.html +++ b/public/stk_login.html @@ -46,14 +46,14 @@

    (for S