mirror of
https://github.com/xfarrow/blink
synced 2025-02-14 07:50:34 +01:00
comments
This commit is contained in:
parent
70f3b1f754
commit
4356ec88a9
@ -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
|
||||
|
@ -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 crypto = require('crypto');
|
||||
const pgp = require('pg-promise')();
|
||||
@ -13,108 +26,55 @@ const database_configuration = {
|
||||
|
||||
const db = pgp(database_configuration);
|
||||
|
||||
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){
|
||||
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");
|
||||
return res.status(400).json("Invalid request.");
|
||||
}
|
||||
|
||||
// Generate activation link token
|
||||
const activationLink = crypto.randomBytes(16).toString('hex');
|
||||
|
||||
// Hash provided password
|
||||
const hashPasswordPromise = bcrypt.hash(userData.password, 10);
|
||||
|
||||
try{
|
||||
// Begin transaction
|
||||
|
||||
// Begin transaction
|
||||
await db.tx(async (t) => {
|
||||
|
||||
// Inserting in "Person" table
|
||||
const userInsertQuery = `
|
||||
INSERT INTO "Person" (email, password, display_name, date_of_birth, available, enabled, place_of_living)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id`;
|
||||
// Inserting in the "Person" table
|
||||
const userInsertQuery = `
|
||||
INSERT INTO "Person" (email, password, display_name, date_of_birth, available, enabled, place_of_living)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id`;
|
||||
|
||||
const userResult = await t.one(userInsertQuery, [
|
||||
userData.email,
|
||||
await hashPasswordPromise,
|
||||
userData.display_name,
|
||||
userData.date_of_birth,
|
||||
userData.available,
|
||||
false,
|
||||
userData.place_of_living
|
||||
]);
|
||||
const userResult = await t.one(userInsertQuery, [
|
||||
userData.email,
|
||||
await hashPasswordPromise,
|
||||
userData.display_name,
|
||||
userData.date_of_birth,
|
||||
userData.available,
|
||||
false,
|
||||
userData.place_of_living
|
||||
]);
|
||||
|
||||
const activationLinkInsertQuery = `
|
||||
INSERT INTO "ActivationLink" (person_id, identifier)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *`;
|
||||
// Inserting in the "ActivationLink" table
|
||||
const activationLinkInsertQuery = `
|
||||
INSERT INTO "ActivationLink" (person_id, identifier)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *`;
|
||||
|
||||
const activationLinkResult = await t.one(activationLinkInsertQuery, [
|
||||
userResult.id,
|
||||
activationLink,
|
||||
]);
|
||||
return res.status(200).json({ activationLink: activationLinkResult.identifier });
|
||||
const activationLinkResult = await t.one(activationLinkInsertQuery, [
|
||||
userResult.id,
|
||||
activationLink,
|
||||
]);
|
||||
|
||||
return res.status(200).json({ activationLink: activationLinkResult.identifier });
|
||||
|
||||
});
|
||||
}
|
||||
catch (error){
|
||||
@ -128,5 +88,5 @@ function login(req, res){
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
register_async
|
||||
register
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user