This commit is contained in:
xfarrow 2023-10-11 12:37:20 +02:00
parent 0fbe12b5d4
commit 711e6d210a
12 changed files with 165 additions and 147 deletions

View File

@ -1,143 +0,0 @@
/*
** This code is part of Blink
** licensed under GPLv3
*/
// require() always returns a function
const express = require('express');
const bcrypt = require('bcrypt');
const { Pool } = require('pg');
const crypto = require('crypto');
// We can do express() because the express
// module exports a function. Exporting a function
// means making a JavaScript function defined in one
// module available for use in another module.
const app = express();
const port = 3000;
// Middleware which parses JSON for POST requests
app.use(express.json());
const pool = new Pool({
user: 'postgres',
host: 'localhost',
database: 'Blink',
password: 'postgres',
port: 5432,
max: 10,
idleTimeoutMillis: 30000,
});
// Define a route to get all items
app.get('/api/items', (req, res) => {
res.json(items);
});
// POST - Register an account
// (req, res) => { ... } is a callback which usually indicates that the
// execution of the code contained between brackets will continue
// asynchronously.
app.post('/api/register', (req, res) => {
const userData = req.body;
// Ensure that the required fields are present before proceeding
if (!userData.display_name || !userData.email || !userData.password) {
return res.status(400).json("Invalid request");
}
// The callback denoted by the arrow function is executed
// when hash() has finished its execution.
bcrypt.hash(userData.password, 10, (err, hashedPassword) => {
if (err) {
console.error('Error hashing password:', err);
}
else {
// Generate activation link token
const activationLink = crypto.randomBytes(16).toString('hex');
// Acquire a connection from the pool
pool.connect()
.then((client) => {
// SQL query with placeholders for parameters
const insertQuery = `
INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email, password)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *`; // Return the inserted row
return client.query(insertQuery, [
userData.display_name,
userData.date_of_birth,
userData.place_of_living,
userData.is_looking_for_job,
userData.email,
hashedPassword
])
.then((result) => {
// Respond with the inserted user data
res.status(200).json(result.rows[0]);
})
.catch((error) => {
console.error('Error inserting data:', error);
res.status(500).json("Internal server error");
})
.finally(() => {
// Release the connection back to the pool
client.release();
});
})
.catch((error) => {
console.error('Error acquiring a connection from the pool:', error);
res.status(500).json("Internal server error");
});
}
});
});
app.post('/api/registerv2', async (req, res) => {
const userData = req.body;
// Ensure that the required fields are present before proceeding
if (!userData.display_name || !userData.email || !userData.password) {
return res.status(400).json("Invalid request");
}
const hashPasswordPromise = bcrypt.hash(userData.password, 10);
var client;
try{
client = await pool.connect();
const insertQuery = `
INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email, password)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *`;
const result = await client.query(insertQuery, [
userData.display_name,
userData.date_of_birth,
userData.place_of_living,
userData.is_looking_for_job,
userData.email,
await hashPasswordPromise
]);
res.status(200).json(result.rows[0]);
}
catch (error){
console.error('Error inserting data:', error);
res.status(500).json("Internal server error");
}
finally {
if (client) {
client.release();
}
}
});
// Start the server
app.listen(port, () => {
console.log(`Blink API server is running on port ${port}`);
});

View File

@ -0,0 +1,26 @@
/*
** This code is part of Blink
** licensed under GPLv3
*/
// require() always returns a function
const express = require('express');
const api_controller = require('./api_controller.js');
// We can do express() because the express
// module exports a function. Exporting a function
// means making a JavaScript function defined in one
// module available for use in another module.
const app = express();
const port = 3000;
// Middleware which parses JSON for POST requests
app.use(express.json());
app.post('/blinkapi/register', api_controller.register_async);
app.post('blinkapi/login', api_controller.login);
// Start the server
app.listen(port, () => {
console.log(`Blink API server is running on port ${port}`);
});

View File

@ -0,0 +1,121 @@
const bcrypt = require('bcrypt');
const { Pool } = require('pg');
const crypto = require('crypto');
const pool = new Pool({
user: 'postgres',
host: 'localhost',
database: 'Blink',
password: 'postgres',
port: 5432,
max: 10,
idleTimeoutMillis: 30000,
});
function register(req, res){
const userData = req.body;
// Ensure that the required fields are present before proceeding
if (!userData.display_name || !userData.email || !userData.password) {
return res.status(400).json("Invalid request");
}
// The callback denoted by the arrow function is executed
// when hash() has finished its execution.
bcrypt.hash(userData.password, 10, (err, hashedPassword) => {
if (err) {
console.error('Error hashing password:', err);
}
else {
// Generate activation link token
const activationLink = crypto.randomBytes(16).toString('hex');
// Acquire a connection from the pool
pool.connect()
.then((client) => {
// SQL query with placeholders for parameters
const insertQuery = `
INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email, password)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *`; // Return the inserted row
return client.query(insertQuery, [
userData.display_name,
userData.date_of_birth,
userData.place_of_living,
userData.is_looking_for_job,
userData.email,
hashedPassword
])
.then((result) => {
// Respond with the inserted user data
res.status(200).json(result.rows[0]);
})
.catch((error) => {
console.error('Error inserting data:', error);
res.status(500).json("Internal server error");
})
.finally(() => {
// Release the connection back to the pool
client.release();
});
})
.catch((error) => {
console.error('Error acquiring a connection from the pool:', error);
res.status(500).json("Internal server error");
});
}
});
}
async function register_async(req, res){
const userData = req.body;
// Ensure that the required fields are present before proceeding
if (!userData.display_name || !userData.email || !userData.password) {
return res.status(400).json("Invalid request");
}
// Generate activation link token
const activationLink = crypto.randomBytes(16).toString('hex');
const hashPasswordPromise = bcrypt.hash(userData.password, 10);
var client;
try{
client = await pool.connect();
const insertQuery = `
INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email, password)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *`;
const result = await client.query(insertQuery, [
userData.display_name,
userData.date_of_birth,
userData.place_of_living,
userData.is_looking_for_job,
userData.email,
await hashPasswordPromise
]);
res.status(200).json(result.rows[0]);
}
catch (error){
console.error('Error inserting data:', error);
res.status(500).json("Internal server error");
}
finally {
if (client) {
client.release();
}
}
}
function login(req, res){
}
module.exports = {
register_async,
login
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

View File

@ -59,6 +59,7 @@ input {
} }
input[type="text"], input[type="text"],
input[type="password"],
input[type="email"] { input[type="email"] {
width: 100%; width: 100%;
padding: 0 0 0 10px; padding: 0 0 0 10px;
@ -75,26 +76,31 @@ input[type="email"] {
background: none; background: none;
} }
input[type="text"]:focus, input[type="text"]:focus,
input[type="password"]:focus,
input[type="email"]:focus { input[type="email"]:focus {
border-color: #3ca9e2; border-color: #3ca9e2;
} }
input[type="text"]:focus:invalid, input[type="text"]:focus:invalid,
input[type="password"]:focus:invalid,
input[type="email"]:focus:invalid { input[type="email"]:focus:invalid {
color: #cc1e2b; color: #cc1e2b;
border-color: #cc1e2b; border-color: #cc1e2b;
} }
input[type="text"]:valid ~ .validation, input[type="text"]:valid ~ .validation,
input[type="password"]:valid ~ .validation,
input[type="email"]:valid ~ .validation { input[type="email"]:valid ~ .validation {
display: block; display: block;
border-color: #0C0; border-color: #0C0;
} }
input[type="text"]:valid ~ .validation span, input[type="text"]:valid ~ .validation span,
input[type="password"]:valid ~ .validation span,
input[type="email"]:valid ~ .validation span { input[type="email"]:valid ~ .validation span {
background: #0C0; background: #0C0;
position: absolute; position: absolute;
border-radius: 6px; border-radius: 6px;
} }
input[type="text"]:valid ~ .validation span:first-child, input[type="text"]:valid ~ .validation span:first-child,
input[type="password"]:valid ~ .validation span:first-child,
input[type="email"]:valid ~ .validation span:first-child { input[type="email"]:valid ~ .validation span:first-child {
top: 30px; top: 30px;
left: 14px; left: 14px;
@ -104,6 +110,7 @@ input[type="email"]:valid ~ .validation span:first-child {
transform: rotate(-45deg); transform: rotate(-45deg);
} }
input[type="text"]:valid ~ .validation span:last-child, input[type="text"]:valid ~ .validation span:last-child,
input[type="password"]:valid ~ .validation span:last-child,
input[type="email"]:valid ~ .validation span:last-child { input[type="email"]:valid ~ .validation span:last-child {
top: 35px; top: 35px;
left: 8px; left: 8px;

View File

@ -11,13 +11,16 @@
<!-- partial:index.partial.html --> <!-- partial:index.partial.html -->
<div id="login-form-wrap"> <div id="login-form-wrap">
<h2>Login</h2> <h2>Login</h2>
<form id="login-form"> <form id="login-form" method="POST">
<p>
<input type="text" id="username" name="username" placeholder="Username" required><i class="validation"><span></span><span></span></i>
</p>
<p> <p>
<input type="email" id="email" name="email" placeholder="Email Address" required><i class="validation"><span></span><span></span></i> <input type="email" id="email" name="email" placeholder="Email Address" required><i class="validation"><span></span><span></span></i>
</p> </p>
<p>
<input type="password" id="password" name="password" placeholder="Password" required><i class="validation"><span></span><span></span></i>
</p>
<p> <p>
<input type="submit" id="login" value="Login"> <input type="submit" id="login" value="Login">
</p> </p>

View File

@ -42,3 +42,7 @@ function entryPoint(){
} }
entryPoint(); entryPoint();
// callbacks usually indicate that the
// execution of their code will continue
// asynchronously.