This commit is contained in:
xfarrow 2023-10-12 16:56:45 +02:00
parent 70f3b1f754
commit 4356ec88a9
2 changed files with 57 additions and 88 deletions

View File

@ -1,6 +1,15 @@
/* /*
** This code is part of Blink
** licensed under GPLv3 This code is part of Blink
licensed under GPLv3
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
// require() always returns a function // require() always returns a function

View File

@ -1,3 +1,16 @@
/*
This code is part of Blink
licensed under GPLv3
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const crypto = require('crypto'); const crypto = require('crypto');
const pgp = require('pg-promise')(); const pgp = require('pg-promise')();
@ -13,108 +26,55 @@ const database_configuration = {
const db = pgp(database_configuration); const db = pgp(database_configuration);
function register(req, res){ async 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; const userData = req.body;
// Ensure that the required fields are present before proceeding // Ensure that the required fields are present before proceeding
if (!userData.display_name || !userData.email || !userData.password) { if (!userData.display_name || !userData.email || !userData.password) {
return res.status(400).json("Invalid request"); return res.status(400).json("Invalid request.");
} }
// Generate activation link token // Generate activation link token
const activationLink = crypto.randomBytes(16).toString('hex'); const activationLink = crypto.randomBytes(16).toString('hex');
// Hash provided password
const hashPasswordPromise = bcrypt.hash(userData.password, 10); const hashPasswordPromise = bcrypt.hash(userData.password, 10);
try{ try{
// Begin transaction
// Begin transaction
await db.tx(async (t) => { await db.tx(async (t) => {
// Inserting in "Person" table // Inserting in the "Person" table
const userInsertQuery = ` const userInsertQuery = `
INSERT INTO "Person" (email, password, display_name, date_of_birth, available, enabled, place_of_living) INSERT INTO "Person" (email, password, display_name, date_of_birth, available, enabled, place_of_living)
VALUES ($1, $2, $3, $4, $5, $6, $7) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id`; RETURNING id`;
const userResult = await t.one(userInsertQuery, [ const userResult = await t.one(userInsertQuery, [
userData.email, userData.email,
await hashPasswordPromise, await hashPasswordPromise,
userData.display_name, userData.display_name,
userData.date_of_birth, userData.date_of_birth,
userData.available, userData.available,
false, false,
userData.place_of_living userData.place_of_living
]); ]);
const activationLinkInsertQuery = ` // Inserting in the "ActivationLink" table
INSERT INTO "ActivationLink" (person_id, identifier) const activationLinkInsertQuery = `
VALUES ($1, $2) INSERT INTO "ActivationLink" (person_id, identifier)
RETURNING *`; VALUES ($1, $2)
RETURNING *`;
const activationLinkResult = await t.one(activationLinkInsertQuery, [ const activationLinkResult = await t.one(activationLinkInsertQuery, [
userResult.id, userResult.id,
activationLink, activationLink,
]); ]);
return res.status(200).json({ activationLink: activationLinkResult.identifier });
return res.status(200).json({ activationLink: activationLinkResult.identifier });
}); });
} }
catch (error){ catch (error){
@ -128,5 +88,5 @@ function login(req, res){
} }
module.exports = { module.exports = {
register_async register
}; };