user signup
This commit is contained in:
Binary file not shown.
@@ -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
|
||||
// })
|
||||
// }
|
||||
@@ -6,10 +6,18 @@ const pass = ref<string>("");
|
||||
|
||||
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'})
|
||||
});
|
||||
}
|
||||
@@ -23,6 +31,6 @@ function login(e: SubmitEvent) {
|
||||
<label for="pass">Password</label> <br>
|
||||
<input type="password" id="pass" v-model="pass"/> <br>
|
||||
<button type="submit">Sign in</button> <br>
|
||||
<button type="button">Sign up</button>
|
||||
<button type="button" @click="signup">Sign up</button>
|
||||
</form>
|
||||
</template>
|
||||
Reference in New Issue
Block a user