blink/backend/api.js

154 lines
4.7 KiB
JavaScript
Raw Normal View History

2023-09-27 12:18:29 +02:00
/*
2023-09-27 15:22:01 +02:00
** This code is part of Blink
2023-09-27 12:18:29 +02:00
** licensed under GPLv3
*/
2023-09-28 10:01:17 +02:00
// require() always returns a function
2023-09-27 12:18:29 +02:00
const express = require('express');
2023-09-27 12:47:27 +02:00
const bcrypt = require('bcrypt');
2023-09-27 15:07:40 +02:00
const { Pool } = require('pg');
2023-09-27 15:22:01 +02:00
const crypto = require('crypto');
2023-09-27 12:47:27 +02:00
2023-09-28 10:01:17 +02:00
// 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.
2023-09-27 12:18:29 +02:00
const app = express();
const port = 3000;
// Middleware which parses JSON for POST requests
app.use(express.json());
2023-09-27 15:07:40 +02:00
const pool = new Pool({
user: 'postgres',
host: 'localhost',
database: 'Blink',
password: 'postgres',
port: 5432,
max: 10,
idleTimeoutMillis: 30000,
});
2023-09-27 12:18:29 +02:00
// Define a route to get all items
app.get('/api/items', (req, res) => {
res.json(items);
});
// POST - Register an account
2023-10-05 12:20:00 +02:00
// (req, res) => { ... } is a callback which usually indicates that the
// execution of the code contained between brackets will continue
// asynchronously.
2023-09-27 12:18:29 +02:00
app.post('/api/register', (req, res) => {
2023-09-27 12:40:52 +02:00
const userData = req.body;
// Ensure that the required fields are present before proceeding
2023-09-27 15:07:40 +02:00
if (!userData.display_name || !userData.email || !userData.password) {
2023-09-27 12:40:52 +02:00
return res.status(400).json("Invalid request");
}
2023-10-05 12:20:00 +02:00
// The callback denoted by the arrow function is executed
// when hash() has finished its execution.
2023-09-27 15:07:40 +02:00
bcrypt.hash(userData.password, 10, (err, hashedPassword) => {
2023-09-27 15:22:01 +02:00
2023-09-27 15:07:40 +02:00
if (err) {
console.error('Error hashing password:', err);
2023-09-27 15:22:01 +02:00
}
else {
// Generate activation link token
const activationLink = crypto.randomBytes(16).toString('hex');
2023-09-27 15:07:40 +02:00
// 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)
2023-09-27 12:40:52 +02:00
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,
2023-09-27 15:07:40 +02:00
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();
});
2023-09-27 12:40:52 +02:00
})
.catch((error) => {
2023-09-27 15:07:40 +02:00
console.error('Error acquiring a connection from the pool:', error);
2023-09-27 12:40:52 +02:00
res.status(500).json("Internal server error");
});
2023-09-27 15:22:01 +02:00
}
});
2023-09-27 12:18:29 +02:00
});
2023-10-05 12:20:00 +02:00
app.post('/api/registerv2', (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");
}
bcrypt.hash(userData.password, 10)
.then( hashedPassword => {
// Generate activation link token
const activationLink = crypto.randomBytes(16).toString('hex');
// Acquire a connection from the pool
pool.connect()
.then(async (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
try {
try {
const result = await client.query(insertQuery, [
userData.display_name,
userData.date_of_birth,
userData.place_of_living,
userData.is_looking_for_job,
userData.email,
hashedPassword
]);
// 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");
});
});
});
2023-09-27 12:18:29 +02:00
// Start the server
app.listen(port, () => {
2023-09-27 15:22:01 +02:00
console.log(`Blink API server is running on port ${port}`);
2023-09-27 12:18:29 +02:00
});