closer to user login/register
This commit is contained in:
+2
-1
@@ -8,9 +8,10 @@
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"pm2": "^4.5.5"
|
||||
},
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.17.1",
|
||||
"nanoid": "^3.1.22",
|
||||
"pg": "^8.5.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const express = require('express');
|
||||
const {nanoid} = require('nanoid');
|
||||
|
||||
const { Pool } = require('pg');
|
||||
const db = new Pool({
|
||||
@@ -16,6 +17,7 @@ const port = 80;
|
||||
db.query("CREATE TABLE IF NOT EXISTS bin (id VARCHAR PRIMARY KEY);"
|
||||
+ "CREATE TABLE IF NOT EXISTS note (id VARCHAR PRIMARY KEY, text TEXT, modified TIMESTAMPTZ);"
|
||||
+ "CREATE TABLE IF NOT EXISTS bin_note (bin_id VARCHAR REFERENCES bin (id), note_id VARCHAR REFERENCES note (id), PRIMARY KEY (bin_id, note_id));"
|
||||
+ "CREATE TABLE IF NOT EXISTS user_ (id VARCHAR PRIMARY KEY, username TEXT, password TEXT);" // table 'user' is already taken
|
||||
);
|
||||
|
||||
|
||||
@@ -81,6 +83,38 @@ router.post('/save', (req, res)=>{
|
||||
// {status: 'ok'}
|
||||
});
|
||||
|
||||
const login_check_stmt =
|
||||
"SELECT id, password FROM user_ AS u"
|
||||
+" WHERE u.username = $1";
|
||||
const register_stmt =
|
||||
"INSERT INTO user_ (id, username, password) VALUES ($1, $2, $3)";
|
||||
router.post('/login', (req, res)=>{
|
||||
const {username, password_from_client} = req.body;
|
||||
db.query(login_check_stmt, [username])
|
||||
.then(result=>{
|
||||
// if there is such a username:
|
||||
if(result.rows.length > 0){
|
||||
const row = result.rows[0];
|
||||
const id=row.id, password_from_db=row.password;
|
||||
// if the passwords match:
|
||||
if(password_from_client === password_from_db){
|
||||
res.json({success:true, user:{id, username}, session_id:nanoid()});
|
||||
}
|
||||
else{
|
||||
res.json({success: false});
|
||||
}
|
||||
}
|
||||
// if no such user exists:
|
||||
else{
|
||||
const id = nanoid();
|
||||
db.query(register_stmt, [id, username, password_from_client])
|
||||
.then(r=>{
|
||||
res.json({success: true, user:{id, username}, session_id:nanoid()});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.use('/', express.json(), router);
|
||||
|
||||
|
||||
|
||||
@@ -966,6 +966,11 @@ mute-stream@~0.0.4:
|
||||
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
|
||||
integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==
|
||||
|
||||
nanoid@^3.1.22:
|
||||
version "3.1.22"
|
||||
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.22.tgz#b35f8fb7d151990a8aebd5aa5015c03cf726f844"
|
||||
integrity sha512-/2ZUaJX2ANuLtTvqTlgqBQNJoQO398KyJgZloL0PZkC0dpysjncRUPsFe3DUPzz/y3h+u7C46np8RMuvF3jsSQ==
|
||||
|
||||
needle@2.4.0:
|
||||
version "2.4.0"
|
||||
resolved "https://registry.yarnpkg.com/needle/-/needle-2.4.0.tgz#6833e74975c444642590e15a750288c5f939b57c"
|
||||
|
||||
Reference in New Issue
Block a user