From e46aad592f1b16e7a9777e2a52bb0646aa6233a6 Mon Sep 17 00:00:00 2001 From: UBA GCOEN <124521344+UBA-GCOEN@users.noreply.github.com> Date: Sun, 1 Oct 2023 19:09:24 +0530 Subject: [PATCH] Delete server/src/api/controllers/userFaculty.js --- server/src/api/controllers/userFaculty.js | 265 ---------------------- 1 file changed, 265 deletions(-) delete mode 100644 server/src/api/controllers/userFaculty.js diff --git a/server/src/api/controllers/userFaculty.js b/server/src/api/controllers/userFaculty.js deleted file mode 100644 index 7f738bb3d..000000000 --- a/server/src/api/controllers/userFaculty.js +++ /dev/null @@ -1,265 +0,0 @@ -import userFacultyModel from "../models/userFacultyModel.js" -import bcrypt from 'bcrypt' -import generateToken from "../middlewares/generateToken.js" - -import session from "express-session" -import * as dotenv from "dotenv"; -dotenv.config(); - -/** - * Route: /userFaculty - * Desc: to show or access user Faculty - */ -export const userFaculty = async (req, res) => { - res.status(200).json({message:"Show user Faculty signin/signup page"}) - -} - - - -/** - * Route: /userFaculty/signup - * Desc: Faculty user sign up - */ -export const signup = async (req, res) => { - const { - name, - email, - password, - confirmPassword, - branch, - subjects, - designation, - education, - bio, //optional - intrest, //optional - mobile //optional - } = req.body - - - //check if any field is not empty - if (!name || !email || !password || !confirmPassword) { - return res.status(404).json({ - success: false, - message: "Please Fill all the Details.", - }); - } - - //password and email constrains - const passwordRegex = - /^(?=.*[a-z])(?=.*[A-Z])(?=.*[@$%#^&*])(?=.*[0-9]).{8,}$/; - - const emailDomains = [ - "@gmail.com", - "@yahoo.com", - "@hotmail.com", - "@aol.com", - "@outlook.com", - ]; - - - - //check name length - if (name.length < 2) { - return res - .status(404) - .json({ message: "Name must be atleast 2 characters long." }); - } - - - - // check email format - if (!emailDomains.some((v) => email.indexOf(v) >= 0)) { - return res.status(404).json({ - message: "Please enter a valid email address", - })}; - - - // check password format - if (!passwordRegex.test(password)) { - return res.status(404).json({ - message: "Password must be at least 8 characters long and include at least 1 uppercase letter, 1 lowercase letter, 1 symbol (@$%#^&*), and 1 number (0-9)", - }); - } - - - // check password match - if(password != confirmPassword){ - res.json({msg:"Password does not match"}) - } - - - // check password match - if(password != confirmPassword){ - res.json({msg:"Password does not match"}) - } - - - - /** - * checking field types - * to avoid sql attacks - */ - if (typeof name !== "string") { - res.status(400).json({ status: "error" }); - return; - } - - if (typeof email !== "string") { - res.status(400).json({ status: "error" }); - return; - } - - if (typeof branch !== "string") { - res.status(400).json({ status: "error" }); - return; - } - - if (typeof intrest !== "string") { - res.status(400).json({ status: "error" }); - return; - } - - if (typeof subjects !== "string") { - res.status(400).json({ status: "error" }); - return; - } - - if (typeof password !== "string" || typeof confirmPassword !== "string") { - res.status(400).json({ status: "error" }); - return; - } - - if (typeof designation !== "string") { - res.status(400).json({ status: "error" }); - return; - } - - if (typeof mobile !== "number") { - res.status(400).json({ status: "error" }); - return; - } - - if (typeof bio !== "string") { - res.status(400).json({ status: "error" }); - return; - } - - if (typeof education !== "string") { - res.status(400).json({ status: "error" }); - return; - } - - - - - const oldUser = await userFacultyModel.findOne({ email }); - try{ - if(!oldUser){ - - - // hash password with bcrypt - const hashedPassword = await bcrypt.hash(password, 12) - - // create userFaculty in database - const result = userFacultyModel.create({ - name, - email, - password: hashedPassword, - branch, - subjects, - designation, - education, - bio, - intrest, - mobile - }); - - if(result){ - res.json({msg: "user Faculty added successfully"}) - } - } - else{ - res.json({msg: "user already exist"}) - } - } - catch(err){ - console.log(err) - } - -} - - - - -/** - * Route: /userFaculty/signin - * Desc: user Faculty sign in - */ -export const signin = async (req, res) => { - const {email, password} = req.body - - //sql injection validation - if(typeof email !== 'string'){ - console.log("invalid email") - return - } - - try { - - const oldUser = await userFacultyModel.findOne({email}) - - const SECRET = process.env.FACULTY_SECRET - - if(oldUser){ - - const isPasswordCorrect = await bcrypt.compare(password, oldUser.password); - - if(isPasswordCorrect){ - - const token = generateToken(oldUser, SECRET); - - req.session.user = { - token: token, - user: oldUser - } - - res.status(200).json({ - success: true, - result: oldUser, - token, - csrfToken: req.csrfToken, - msg: "Faculty is logged in successfully" - }); - - } - else{ - req.session.destroy(err => { - if (err) { - console.error("Error destroying session:", err); - res.status(500).send("Internal Server Error"); - } - }); - res.json({ msg: "Incorrect password" }) - } - } - else{ - req.session.destroy(err => { - if (err) { - console.error("Error destroying session:", err); - res.status(500).send("Internal Server Error"); - } - }); - res.json({ msg:"User Faculty does not exist" }) - } - - } catch (error) { - res.send(error) - } -} - - - - - -