Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor backend URL to use a centralized configuration #120

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 91 additions & 57 deletions file_upload/form_db.js
Original file line number Diff line number Diff line change
@@ -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};
module.exports = { info, check };
10 changes: 5 additions & 5 deletions public/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
},
Expand Down Expand Up @@ -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}`,
Expand All @@ -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")
Expand All @@ -162,4 +162,4 @@
</script>
</body>

</html>
</html>
18 changes: 9 additions & 9 deletions public/fac_login.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ <h4 style="text-align: center; margin: 0px 0px 10px 0px;font-size: 22px;">(for F
</div>
</div>
<script>
var icon =document.getElementById("icon");
icon.onclick = function(){
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";
if (document.body.classList.contains("dark-theme")) {
icon.src = "images/sun.png";
}
else{
icon.src="images/moon.png";
else {
icon.src = "images/moon.png";
}
}

Expand All @@ -75,8 +75,8 @@ <h4 style="text-align: center; margin: 0px 0px 10px 0px;font-size: 22px;">(for F
alert('Please use a valid email from Gmail, Outlook, or other reputable providers.');
return;
}
const response = await fetch('http://localhost:3000/fac_login', {

const response = await fetch('/fac_login', {
method: "POST",
headers: {
'Content-Type': 'application/json'
Expand Down Expand Up @@ -126,4 +126,4 @@ <h4 style="text-align: center; margin: 0px 0px 10px 0px;font-size: 22px;">(for F
</script>
</body>

</html>
</html>
18 changes: 9 additions & 9 deletions public/faculty.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,21 @@ <h4>Quality and Guidelines</h4>
</div>
</div>
<script>
var icon =document.getElementById("icon");
icon.onclick = function(){
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";
if (document.body.classList.contains("dark-theme")) {
icon.src = "images/sun.png";
}
else{
icon.src="images/moon.png";
else {
icon.src = "images/moon.png";
}
}
const Dis_fac_papers = async () => {
const token = localStorage.getItem('accessToken');
const headers = new Headers();
headers.append('Authorization', `Bearer ${token}`);
const response = await fetch('http://localhost:3000/fac_papers', {
const response = await fetch('/fac_papers', {
headers: {
'Authorization': `Bearer ${token}`,
}
Expand Down Expand Up @@ -197,7 +197,7 @@ <h4>Quality and Guidelines</h4>
// const value5= rating5 ? rating5.value : undefined
console.log(rating1, rating2, rating3, rating4)
const data = { rating1, rating2, rating3, rating4 }
const response = await fetch(`http://localhost:3000/rating?id=${id}`, {
const response = await fetch(`/rating?id=${id}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand Down Expand Up @@ -234,4 +234,4 @@ <h4>Quality and Guidelines</h4>
</script>
</body>

</html>
</html>
20 changes: 17 additions & 3 deletions public/form_filling.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
<div class="form-group">
<label>State</label>
<select name="state" id="state">
<option value="Uttar Pradesh">Uttar Pradesh</option>
<option value="Madhya Pradesh">Madhya Pradesh</option>
<option value="Tamil Nadu">Tamil Nadu</option>
<option value="Maharashtra">Maharashtra</option>
<option value="Jammu & Kashmir">Jammu & Kashmir</option>
<option value="Rajasthan">Rajasthan</option>
<option value="Karnataka">Karnataka</option>
<option value="Andhra Pradesh">Andhra Pradesh</option>
<option value="Arunachal Pradesh">Arunachal Pradesh</option>
<option value="Assam">Assam</option>
Expand Down Expand Up @@ -111,6 +118,9 @@
</p>
<p id="result2" style="display: none; font-size: 20px;color: red;font-weight: bold;">An error occurred
</p>
<p id="result3" style="display: none; font-size: 20px;color: red;font-weight: bold;">The email address you
entered is already associated with an account. Please enter a different email to proceed
</p>
<div class="btn">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
Expand Down Expand Up @@ -138,15 +148,16 @@
const course = document.getElementById('course').value.toUpperCase();
const year = document.getElementById('year').value;
const result = document.getElementById('result');
const result2 = document.getElementById('result2')
const result2 = document.getElementById('result2');
const result3 = document.getElementById('result3')

const data = { name, email, col_name, state, course, year, dept };
const token = localStorage.getItem('accessToken');
const headers = new Headers();
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',
Expand All @@ -162,6 +173,9 @@
window.location.href = 'main_page.html';
}, 2000);
}
else if (response.status == 400) {
result3.style.display = "inline-block";
}
else {
console.log("Submission failed");
result2.style.display = "block";
Expand All @@ -178,7 +192,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}`,
},
Expand Down
18 changes: 9 additions & 9 deletions public/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ <h4 style="text-align: center; margin: 10px 0px 6px 0px;font-size: 22px;">(for R
</div>

<script>
var icon =document.getElementById("icon");
icon.onclick = function(){
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";
if (document.body.classList.contains("dark-theme")) {
icon.src = "images/sun.png";
}
else{
icon.src="images/moon.png";
else {
icon.src = "images/moon.png";
}
}

Expand All @@ -77,7 +77,7 @@ <h4 style="text-align: center; margin: 10px 0px 6px 0px;font-size: 22px;">(for R
}

try {
const response = await fetch('http://localhost:3000/login', {
const response = await fetch('/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -102,7 +102,7 @@ <h4 style="text-align: center; margin: 10px 0px 6px 0px;font-size: 22px;">(for R
setTimeout(() => {
result4.style.display = 'none';
}, 2000);
}
}
else if (response.status === 404) {
result2.style.display = 'block';
console.log('User not found');
Expand All @@ -128,4 +128,4 @@ <h4 style="text-align: center; margin: 10px 0px 6px 0px;font-size: 22px;">(for R
</script>
</body>

</html>
</html>
Loading