Skip to content

Commit

Permalink
Merge pull request #119 from ygowthamr/duplicateEmailCrash
Browse files Browse the repository at this point in the history
Solved App crashes when entering an existing email ID saved in `info_table`
  • Loading branch information
Harshdev098 authored Oct 14, 2024
2 parents 49ad776 + 01ad4e6 commit b8cd9e7
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 69 deletions.
140 changes: 83 additions & 57 deletions file_upload/form_db.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,88 @@
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 };
32 changes: 20 additions & 12 deletions public/form_filling.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,24 @@
<div class="form-group">
<label>State</label>
<select name="state" id="state">
<option value="Andaman and Nicobar Islands">Andaman and Nicobar Islands</option>
<option value="Andhra Pradesh">Andhra Pradesh</option>
<option value="Arunachal Pradesh">Arunachal Pradesh</option>
<option value="Assam">Assam</option>
<option value="Bihar">Bihar</option>
<option value="Chandigarh">Chandigarh</option>
<option value="Chhattisgarh">Chhattisgarh</option>
<option value="Dadra and Nagar Haveli and Daman and Diu">Dadra and Nagar Haveli and Daman and Diu</option>
<option value="Delhi">Delhi</option>
<option value="Goa">Goa</option>
<option value="Gujarat">Gujarat</option>
<option value="Haryana">Haryana</option>
<option value="Himachal Pradesh">Himachal Pradesh</option>
<option value="Jharkhand">Jharkhand</option>
<option value="Karnataka">Karnataka</option>
<option value="Kerala">Kerala</option>
<option value="Ladakh">Ladakh</option>
<option value="Lakshadweep">Lakshadweep</option>
<option value="Madhya Pradesh">Madhya Pradesh</option>
<option value="Maharashtra">Maharashtra</option>
<option value="Manipur">Manipur</option>
Expand All @@ -68,6 +74,7 @@
<option value="Nagaland">Nagaland</option>
<option value="Odisha">Odisha</option>
<option value="Punjab">Punjab</option>
<option value="Puducherry">Puducherry</option>
<option value="Rajasthan">Rajasthan</option>
<option value="Sikkim">Sikkim</option>
<option value="Tamil Nadu">Tamil Nadu</option>
Expand All @@ -76,15 +83,7 @@
<option value="Uttar Pradesh">Uttar Pradesh</option>
<option value="Uttarakhand">Uttarakhand</option>
<option value="West Bengal">West Bengal</option>
<option value="Andaman and Nicobar Islands">Andaman and Nicobar Islands</option>
<option value="Chandigarh">Chandigarh</option>
<option value="Dadra and Nagar Haveli and Daman and Diu">Dadra and Nagar Haveli and Daman and Diu
</option>
<option value="Delhi">Delhi</option>
<option value="Lakshadweep">Lakshadweep</option>
<option value="Ladakh">Ladakh</option>
<option value="Puducherry">Puducherry</option>
<option value="Jammu and Kashmir">Jammu and Kashmir</option>

</select>
</div>
<div class="form-group">
Expand All @@ -111,6 +110,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,7 +140,8 @@
const course = document.getElementById('course').value.toUpperCase();
const year = document.getElementById('year').value;
const result = document.getElementById('result');
const result2 = document.getElementById('result2')
const result3 = document.getElementById('result3')
const result2 = document.getElementById('result2');

const data = { name, email, col_name, state, course, year, dept };
const token = localStorage.getItem('accessToken');
Expand All @@ -154,6 +157,8 @@
},
body: JSON.stringify(data),
});

console.log(response.status);

if (response.status == 200) {
console.log("Information saved");
Expand All @@ -162,6 +167,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 @@ -172,7 +180,7 @@
} catch (error) {
console.error("Error occurred", error);
}
});
})

async function usercheck() {
const token = localStorage.getItem('accessToken');
Expand All @@ -193,4 +201,4 @@
</section>
</body>

</html
</html

0 comments on commit b8cd9e7

Please sign in to comment.