From 03861b571833f3ff21174dcf2d09f254f1143242 Mon Sep 17 00:00:00 2001 From: Mann Patel <130435633+MannPatel0@users.noreply.github.com> Date: Fri, 14 Mar 2025 16:14:10 -0600 Subject: [PATCH] Email Auth Finished | SQL code done | --- backend/index.js | 106 +++- frontend/index.html | 24 +- frontend/public/20191227_012601_0000.png | Bin 0 -> 55036 bytes frontend/public/Ucalgary.png | Bin 0 -> 441601 bytes frontend/public/icon.png | Bin 19731 -> 0 bytes frontend/public/icon/apple-touch-icon.png | Bin 0 -> 3064 bytes frontend/public/icon/favicon.ico | Bin 0 -> 5238 bytes frontend/public/icon/icon-192-maskable.png | Bin 0 -> 3420 bytes frontend/public/icon/icon-192.png | Bin 0 -> 7250 bytes frontend/public/icon/icon-512-maskable.png | Bin 0 -> 12773 bytes frontend/public/icon/icon-512.png | Bin 0 -> 24247 bytes frontend/public/market.jpg | Bin 0 -> 120537 bytes .../public/university-of-calgary-logo.png | Bin 0 -> 422661 bytes frontend/src/App.jsx | 505 +++++++++++++----- frontend/src/assets/react.svg | 1 - frontend/src/components/Navbar.jsx | 36 +- frontend/src/pages/Home.jsx | 124 +++-- 17 files changed, 580 insertions(+), 216 deletions(-) create mode 100644 frontend/public/20191227_012601_0000.png create mode 100644 frontend/public/Ucalgary.png delete mode 100644 frontend/public/icon.png create mode 100644 frontend/public/icon/apple-touch-icon.png create mode 100644 frontend/public/icon/favicon.ico create mode 100644 frontend/public/icon/icon-192-maskable.png create mode 100644 frontend/public/icon/icon-192.png create mode 100644 frontend/public/icon/icon-512-maskable.png create mode 100644 frontend/public/icon/icon-512.png create mode 100644 frontend/public/market.jpg create mode 100644 frontend/public/university-of-calgary-logo.png delete mode 100644 frontend/src/assets/react.svg diff --git a/backend/index.js b/backend/index.js index bf23680..17ee0be 100644 --- a/backend/index.js +++ b/backend/index.js @@ -2,6 +2,7 @@ import express, { json } from "express"; import cors from "cors"; import mysql from "mysql2"; import nodemailer from "nodemailer"; + import crypto from "crypto"; import jwt from "jsonwebtoken"; @@ -33,7 +34,7 @@ const transporter = nodemailer.createTransport({ port: 465, auth: { user: "campusplug@zohomailcloud.ca", //Zoho email - pass: "NzaZ7FFKNh18", //Zoho password + pass: "e0YRrNSeJZQd", //Zoho password }, }); @@ -74,7 +75,8 @@ app.post("/send-verification", async (req, res) => { if (results.length > 0) { // Update existing record db_con.query( - "UPDATE AuthVerification SET VerificationCode = ?, Authenticated = FALSE, Date = CURRENT_TIMESTAMP WHERE Email = ?", + `UPDATE AuthVerification SET VerificationCode = ?, Authenticated = FALSE, Date = CURRENT_TIMESTAMP + WHERE Email = ?`, [verificationCode, email], async (err) => { if (err) { @@ -138,7 +140,7 @@ app.post("/verify-code", (req, res) => { // Check verification code db_con.query( - "SELECT * FROM AuthVerification WHERE Email = ? AND VerificationCode = ? AND Authenticated = FALSE AND Date > DATE_SUB(NOW(), INTERVAL 15 MINUTE)", + "SELECT * FROM AuthVerification WHERE Email = ? AND VerificationCode = ? AND Authenticated = 0 AND Date > DATE_SUB(NOW(), INTERVAL 15 MINUTE)", [email, code], (err, results) => { if (err) { @@ -179,11 +181,11 @@ app.post("/verify-code", (req, res) => { }); // Create a users and Complete user registration after verification -app.post("/complete-registration", (req, res) => { +app.post("/complete-signup", (req, res) => { const data = req.body; db_con.query( - `SELECT * FROM AuthVerification WHERE Email = ${data.email} AND Authenticated = 1`, + `SELECT * FROM AuthVerification WHERE Email = '${data.email}' AND Authenticated = 1;`, (err, results) => { if (err) { console.error("Database error:", err); @@ -196,7 +198,7 @@ app.post("/complete-registration", (req, res) => { // Create the user db_con.query( `INSERT INTO User (Name, Email, UCID, Password, Phone, Address) - VALUES (${data.name}, ${data.email}, ${data.UCID}, ${data.password}, ${data.phone}, ${data.address})`, + VALUES ('${data.name}', '${data.email}', '${data.UCID}', '${data.password}', '${data.phone}', '${data.address}')`, (err, result) => { if (err) { console.error("Error creating user:", err); @@ -215,7 +217,7 @@ app.post("/complete-registration", (req, res) => { // Delete verification record db_con.query( - `DELETE FROM AuthVerification WHERE Email = ${data.email}`, + `DELETE FROM AuthVerification WHERE Email = '${data.email}'`, (deleteErr) => { if (deleteErr) { console.error("Error deleting verification:", deleteErr); @@ -223,7 +225,10 @@ app.post("/complete-registration", (req, res) => { res.json({ success: true, message: "User registration completed successfully", - userId: result.insertId, + name: data.name, + email: data.email, + UCID: data.UCID, + phone: data.phone, }); }, ); @@ -243,9 +248,7 @@ function cleanupExpiredCodes() { if (err) { console.error("Error cleaning up expired codes:", err); } else { - console.log( - `Cleaned up ${result.affectedRows} expired verification codes`, - ); + console.log(`Cleaned up ${results} expired verification codes`); } }, ); @@ -253,7 +256,7 @@ function cleanupExpiredCodes() { // Set up a scheduler to run cleanup every hour setInterval(cleanupExpiredCodes, 60 * 60 * 1000); -//TODO: Fetch all users data: +//Fetch all users data: app.get("/fetch_all_users", (req, res) => { db_con.query("SELECT * FROM User;", (err, data) => { if (err) { @@ -264,7 +267,7 @@ app.get("/fetch_all_users", (req, res) => { }); }); -//TODO: Fetch One user Data: +//Fetch One user Data: app.post("/find_user", (req, res) => { const { email, password } = req.body; @@ -310,7 +313,82 @@ app.post("/find_user", (req, res) => { }); //TODO: Update A uses Data: -//TODO: Delete A uses Data: +app.post("/update", (req, res) => { + const { userId, ...updateData } = req.body; + + if (!userId) { + return res.status(400).json({ error: "User ID is required" }); + } + + // Create query dynamically based on provided fields + const updateFields = []; + const values = []; + + Object.entries(updateData).forEach(([key, value]) => { + // Only include fields that are actually in the User table + if (["Name", "Email", "Password", "Phone", "UCID"].includes(key)) { + updateFields.push(`${key} = ?`); + values.push(value); + } + }); + + if (updateFields.length === 0) { + return res.status(400).json({ error: "No valid fields to update" }); + } + + // Add userId to values array + values.push(userId); + + const query = `UPDATE User SET ${updateFields.join(", ")} WHERE UserID = ?`; + + db_con.query(query, values, (err, result) => { + if (err) { + console.error("Error updating user:", err); + return res.status(500).json({ error: "Could not update user" }); + } + + if (result.affectedRows === 0) { + return res.status(404).json({ error: "User not found" }); + } + + res.json({ success: true, message: "User updated successfully" }); + }); +}); + +//Delete A uses Data: +app.post("/delete", (req, res) => { + const { userId } = req.body; + + if (!userId) { + return res.status(400).json({ error: "User ID is required" }); + } + + // Delete from UserRole first (assuming foreign key constraint) + db_con.query("DELETE FROM UserRole WHERE UserID = ?", [userId], (err) => { + if (err) { + console.error("Error deleting user role:", err); + return res.status(500).json({ error: "Could not delete user role" }); + } + + // Then delete from User table + db_con.query( + "DELETE FROM User WHERE UserID = ?", + [userId], + (err, result) => { + if (err) { + console.error("Error deleting user:", err); + return res.status(500).json({ error: "Could not delete user" }); + } + + if (result.affectedRows === 0) { + return res.status(404).json({ error: "User not found" }); + } + + res.json({ success: true, message: "User deleted successfully" }); + }, + ); + }); +}); app.listen(3030, () => { console.log(`Running Backend on http://localhost:3030/`); diff --git a/frontend/index.html b/frontend/index.html index a9abed9..ce26f74 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,13 +1,17 @@ -
- - - -Ba)f<{0JRb4T6{~>QALQ=&DxRD!yPNe Ha#YQ7lVSQ#JY4-
z)75~lbhv*gbt%&l!dX)C<{5r|Ch0qFb@ !7o1kU(xH|2R7|G
zH^+dtK{^LDrbZZV5`n)8cH9iZr}V;CLvW!WWRU=GT!<}Q4E|IZ34w6J@dft(hAi}_
z)=`!^y{B#cZ;q#HEj(3MZe!z8b)q c!>m2B$HD+L
z=I4eckA11Y;z}|AW$g1SJUkrYaewjTK?4l m`U@me%BuXZ=Gbb
z9XXLPM9o%#p2lluMPd~L<96FWT4xQ3Ep(m2Pf*K3!FTMfeBjDS#~)R2oq3KDK6)2|YCAOuu46F}
zjNI%@;g0BM0=h(t{&wvf>NKq2SrN;8?}Voe8-gHxstDQ!`4qlTT&S?6g8PV{L3sd`
z5S524-#hkSnL*$J+=h}3Yy!hKDhLq?W#QrIhX`iXW~D3eor`OoU7w!SmB(1Jcx8@y
z$sJo%wc-}fmXcX_{epPEH
zO-ZKi?&3k9TWVj^ZBMS>qHTPq6gt1kpP6=_6gkeCjDy^_p^n$o%OOfS>_|!FkCP4%<*A}*tt)3W)`L?oL(AcEcK%bX{(Dny$S^r7E2=}c|DRT