This commit is contained in:
xfarrow 2023-10-11 11:44:35 +02:00
parent 386bfcb6c3
commit 0fbe12b5d4
2 changed files with 31 additions and 41 deletions

View File

@ -99,7 +99,7 @@ app.post('/api/register', (req, res) => {
}); });
}); });
app.post('/api/registerv2', (req, res) => { app.post('/api/registerv2', 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
@ -107,47 +107,36 @@ app.post('/api/registerv2', (req, res) => {
return res.status(400).json("Invalid request"); return res.status(400).json("Invalid request");
} }
bcrypt.hash(userData.password, 10) const hashPasswordPromise = bcrypt.hash(userData.password, 10);
.then( hashedPassword => { var client;
// Generate activation link token try{
const activationLink = crypto.randomBytes(16).toString('hex'); client = await pool.connect();
// Acquire a connection from the pool const insertQuery = `
pool.connect() INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email, password)
.then(async (client) => { VALUES ($1, $2, $3, $4, $5, $6)
// SQL query with placeholders for parameters RETURNING *`;
const insertQuery = ` const result = await client.query(insertQuery, [
INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email, password) userData.display_name,
VALUES ($1, $2, $3, $4, $5, $6) userData.date_of_birth,
RETURNING *`; // Return the inserted row userData.place_of_living,
userData.is_looking_for_job,
try { userData.email,
try { await hashPasswordPromise
const result = await client.query(insertQuery, [ ]);
userData.display_name, res.status(200).json(result.rows[0]);
userData.date_of_birth, }
userData.place_of_living, catch (error){
userData.is_looking_for_job, console.error('Error inserting data:', error);
userData.email, res.status(500).json("Internal server error");
hashedPassword }
]); finally {
// Respond with the inserted user data if (client) {
res.status(200).json(result.rows[0]); client.release();
} 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");
});
});
}); });
// Start the server // Start the server
app.listen(port, () => { app.listen(port, () => {
console.log(`Blink API server is running on port ${port}`); console.log(`Blink API server is running on port ${port}`);

View File

@ -29,3 +29,4 @@ ASYNCHRONOUS
|--------B--------| |--------B--------|
Where Process A overlaps Process B, they're running concurrently or synchronously (dictionary definition), hence the confusion. Where Process A overlaps Process B, they're running concurrently or synchronously (dictionary definition), hence the confusion.