diff --git a/backend/database.db b/backend/database.db index 15be797..321244b 100644 Binary files a/backend/database.db and b/backend/database.db differ diff --git a/backend/index.js b/backend/index.js index 78eaf5f..e3161bd 100644 --- a/backend/index.js +++ b/backend/index.js @@ -36,9 +36,20 @@ app.get('/getRooms', (req, res) => { // res has success or faliure }); -app.get('/createUser', (req, res) => { +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"); }); app.post('/login', async (req, res) => { @@ -60,13 +71,4 @@ app.post('/login', async (req, res) => { app.listen(port, () => { console.log(`Listening on ${port}`); - // db = openDb(); - // console.log("db opened"); }); - -// async function openDb () { -// return open({ -// filename: '/tmp/database.db', -// driver: sqlite3.Database -// }) -// } \ No newline at end of file diff --git a/frontend/src/components/Login.vue b/frontend/src/components/Login.vue index c40141f..8ce391f 100644 --- a/frontend/src/components/Login.vue +++ b/frontend/src/components/Login.vue @@ -6,14 +6,22 @@ const pass = ref(""); function login(e: SubmitEvent) { e.preventDefault(); - console.log(`${email.value} ${pass.value}`); fetch("http://localhost:3000/login", { method: "POST", - body: JSON.stringify({ email: email.value, pass: pass.value}), + body: JSON.stringify({ email: email.value, pass: pass.value }), headers: new Headers({'content-type': 'application/json'}) }); } +function signup(e: Event) { + e.preventDefault(); + fetch("http://localhost:3000/createUser", { + method: "POST", + body: JSON.stringify({ email: email.value, pass: pass.value, name: email.value.split("@")[0] }), + headers: new Headers({'content-type': 'application/json'}) + }); +} + \ No newline at end of file