Files
whatsfreeinmyfree/backend/index.js
2026-03-19 00:32:48 +00:00

81 lines
2.2 KiB
JavaScript

import e from "express";
import cors from "cors";
import bodyParser from "body-parser";
import bcrypt from "bcrypt";
import Database from "better-sqlite3";
const app = e();
const port = 3000;
const db = new Database("./database.db");
app.use(cors());
app.use(e.json());
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send("Nothing Here");
});
app.get('/currentRooms', (req, res) => {
// req has no data
// res has all room at current time
let stmt = db.prepare(`SELECT * FROM TimeSlots`);
let records = stmt.all();
res.status(200).json({records: records});
console.log("currentRooms");
});
app.get('/addTimeslot', (req, res) => {
// req has roomid userid starttime and end time
// res has success or faliure
});
app.get('/addRoom', (req, res) => {
// req has userid and roomname
// res has success or faliure
});
app.get('/getRooms', (req, res) => {
// req has no data
// res has success or faliure
});
app.post('/createUser', async (req, res) => {
// req has email pass(hashed) and name
// res has success or fail
const body = req.body;
let stmt = db.prepare(`SELECT * FROM Users WHERE Email='${body.email}';`);
let storedRecord = stmt.get();
if (storedRecord) {
res.status(400).send("account with that email already exists");
return;
}
const generatedHash = await bcrypt.hash(body.pass, 10);
stmt = db.prepare(`INSERT INTO Users (Email, Pass, Username, Submissions) VALUES ('${body.email}', '${generatedHash}', '${body.name}', 0)`);
stmt.run();
res.status(200).send("account created");
console.log("sign up");
});
app.post('/login', async (req, res) => {
// req has email and pass(hashed)
// res has success or fail
const body = req.body;
const stmt = db.prepare(`SELECT Pass FROM Users WHERE Email='${body.email}';`);
let storedHash = stmt.get();
if (!storedHash) {
res.status(400).send("problem with email");
return;
}
if (await bcrypt.compare(body.pass, storedHash.Pass)){
res.send(200);
console.log("login");
} else {
res.status(400).send("incorrect password");
}
});
app.listen(port, () => {
console.log(`Listening on ${port}`);
});