Files
Project-Portal/run.js
T
2026-11-10 16:10:12 -06:00

128 lines
3.8 KiB
JavaScript

import express from "express"
import { logger_service } from "./logger.js";
import { evalidator, pvalidator } from "./validator.js";
import path from "node:path";
const app = express()
const port = 5337
app.use(express.json());
app.set("view engine", "ejs");
app.use(express.static(path.resolve("./ui/")));
app.listen(port, () => {
let logger = new logger_service();
logger.get_logger().info(`Example app listening on port ${port}`)
})
async function build_userauth_session(request_body) {
console.log(request_body);
return "./ui/partials/evalidate";
}
app.get("/", (req, res) => {
res.render(path.resolve("./ui/sop"));
});
app.post("/api/accounts/register", async (req, res) => {
let inp = req.body;
let jsonout = JSON.stringify(inp);
let email = inp.email.toLowerCase();
let password = inp.password;
let name = inp.name;
let type = inp.type.toLowerCase();
let logger = new logger_service();
logger.get_logger().info("New registration request: " + jsonout)
// == CASES TO IGNORE ==
// Null Check
let null_present = false;
let jsonData = inp;
for (let key in jsonData) {
if (jsonData[key] === null) {
null_present = true;
break;
}
}
if (null_present || !inp) {
logger.get_logger().info("Could not process registration request: " + jsonout + " Reason: NULL Detected")
res.status(400).send("Empty (null) values received. Ensure all required fields are present and try again.");
return;
}
// Check user type selection
// Exto = External Officer
const types = ["Member", "Exto", "Student", "Faculty", "Employer"];
let found_type_match = false;
for (let i in types) {
if (types[i].toLowerCase() == type) {
found_type_match = true;
break;
}
}
if (!found_type_match) {
logger.get_logger().info("Could not process registration request: " + jsonout + " Reason: Type mismatch with expected values")
res.status(400).send(type + " is not a valid user type selection");
return;
}
// Email Check
let ev = new evalidator();
if (!ev.validateEmailRegex(email) || !ev.validateEmailLength(email)) {
logger.get_logger().info("Could not process registration request: " + jsonout + " Reason: Email was not valid to requirements")
res.status(400).send("Email must be in a valid domain, and be between 6 and 100 characters in length");
return;
}
// Password Length Check
let pv = new pvalidator();
if (!pv.validatePasswordLength(password)) {
logger.get_logger().info("Could not process registration request: " + jsonout + " Reason: Password was not valid to requirements")
res.status(400).send("Password must be between 6 and 128 characters in length");
return;
}
// Name Check
var regName = /^[a-zA-Z]+ [a-zA-Z]+$/;
if (!regName.test(name)) {
logger.get_logger().info("Could not process registration request: " + jsonout + " Reason: Name was not valid to requirements")
res.status(400).send("Invalid name was passed, expected 'First Last' format but got " + name);
return;
}
// The user specified a personal email and is not an employer, do not register
if (!email.endsWith("@uwplatt.edu") && type != "employer") {
logger.get_logger().info("Could not process registration request: " + jsonout + " Reason: Email and type combination not allowed")
res.status(400).send("Only @uwplatt.edu email accounts may be used for type: " + type);
return;
}
// == CASES TO ALLOW ==
// The user is either a CPC Member, student, or faculty
if (email.endsWith("@uwplatt.edu") && type != "employer") {
let page = await build_userauth_session(inp);
res.status(200).render(path.resolve(page));
}
// The user type is considered an employer
if (!email.endsWith("@uwplatt.edu") && type == "employer") {
let page = await build_userauth_session(inp);
res.status(200).render(path.resolve(page));
}
});