|
|
@ -1,11 +1,11 @@ |
|
|
import bcrypt from "bcrypt"; |
|
|
import bcrypt from "bcrypt"; |
|
|
import {getClient} from "../utils/database.js"; |
|
|
import { getClient } from "../utils/database.js"; |
|
|
import jwt from "jsonwebtoken"; |
|
|
import jwt from "jsonwebtoken"; |
|
|
import path, {dirname} from "path"; |
|
|
import path, { dirname } from "path"; |
|
|
import fs from "fs"; |
|
|
import fs from "fs"; |
|
|
import {fileURLToPath} from "url"; |
|
|
import { fileURLToPath } from "url"; |
|
|
import crypto from "crypto"; |
|
|
import crypto from "crypto"; |
|
|
import {sendEmail} from "../utils/mail.js"; |
|
|
import { sendEmail } from "../utils/mail.js"; |
|
|
|
|
|
|
|
|
export async function register(req, res) { |
|
|
export async function register(req, res) { |
|
|
try { |
|
|
try { |
|
|
@ -49,7 +49,7 @@ export async function register(req, res) { |
|
|
const token = crypto.randomBytes(32).toString("hex").slice(0, 5); |
|
|
const token = crypto.randomBytes(32).toString("hex").slice(0, 5); |
|
|
|
|
|
|
|
|
const textMessage = "Merci de vous être inscrit. Veuillez vérifier votre e-mail. Code: " + token; |
|
|
const textMessage = "Merci de vous être inscrit. Veuillez vérifier votre e-mail. Code: " + token; |
|
|
|
|
|
|
|
|
const htmlMessage = ` |
|
|
const htmlMessage = ` |
|
|
<!DOCTYPE html> |
|
|
<!DOCTYPE html> |
|
|
<html> |
|
|
<html> |
|
|
@ -126,7 +126,7 @@ export async function register(req, res) { |
|
|
console.log("Successfully registered"); |
|
|
console.log("Successfully registered"); |
|
|
client.end(); |
|
|
client.end(); |
|
|
logger.write("successfully registered", 200); |
|
|
logger.write("successfully registered", 200); |
|
|
res.status(200).send({user: user}); |
|
|
res.status(200).send({ user: user }); |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.log(err); |
|
|
console.log(err); |
|
|
|
|
|
|
|
|
@ -187,7 +187,7 @@ export async function login(req, res) { |
|
|
|
|
|
|
|
|
if (!userInBase) { |
|
|
if (!userInBase) { |
|
|
logger.write("failed to login", 401) |
|
|
logger.write("failed to login", 401) |
|
|
res.status(401).json({error: "Invalid credentials"}); |
|
|
res.status(401).json({ error: "Invalid credentials" }); |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -195,7 +195,7 @@ export async function login(req, res) { |
|
|
|
|
|
|
|
|
if (!isPasswordValid) { |
|
|
if (!isPasswordValid) { |
|
|
logger.write("failed to login", 401) |
|
|
logger.write("failed to login", 401) |
|
|
res.status(401).json({error: "Invalid credentials"}); |
|
|
res.status(401).json({ error: "Invalid credentials" }); |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -215,7 +215,7 @@ export async function login(req, res) { |
|
|
|
|
|
|
|
|
logger.write("Successfully logged in", 200); |
|
|
logger.write("Successfully logged in", 200); |
|
|
client.end(); |
|
|
client.end(); |
|
|
res.status(200).json({token: token, user: userData}); |
|
|
res.status(200).json({ token: token, user: userData }); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -231,12 +231,12 @@ export async function getById(req, res) { |
|
|
if (!result.rows[0]) { |
|
|
if (!result.rows[0]) { |
|
|
logger.write("failed to retrieve user " + id + " because it doesn't exist", 404); |
|
|
logger.write("failed to retrieve user " + id + " because it doesn't exist", 404); |
|
|
client.end() |
|
|
client.end() |
|
|
res.status(404).json({error: "Not Found"}); |
|
|
res.status(404).json({ error: "Not Found" }); |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
logger.write("successfully retrieved user " + id, 200); |
|
|
logger.write("successfully retrieved user " + id, 200); |
|
|
if (result.rows[0].picture) { |
|
|
if (result.rows[0].picture) { |
|
|
return res.status(200).json({user: result.rows[0]}); |
|
|
return res.status(200).json({ user: result.rows[0] }); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -250,12 +250,12 @@ export async function getByUsername(req, res) { |
|
|
if (!result.rows[0]) { |
|
|
if (!result.rows[0]) { |
|
|
logger.write("failed to retrieve user " + username + " because it doesn't exist", 404); |
|
|
logger.write("failed to retrieve user " + username + " because it doesn't exist", 404); |
|
|
client.end() |
|
|
client.end() |
|
|
res.status(404).json({error: "Not Found"}); |
|
|
res.status(404).json({ error: "Not Found" }); |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
logger.write("successfully retrieved user " + username, 200); |
|
|
logger.write("successfully retrieved user " + username, 200); |
|
|
client.end(); |
|
|
client.end(); |
|
|
return res.status(200).json({user: result.rows[0]}); |
|
|
return res.status(200).json({ user: result.rows[0] }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export async function update(req, res) { |
|
|
export async function update(req, res) { |
|
|
@ -283,7 +283,7 @@ export async function update(req, res) { |
|
|
if (emailResult.rows[0]) { |
|
|
if (emailResult.rows[0]) { |
|
|
logger.write("failed to update because email is already used", 400) |
|
|
logger.write("failed to update because email is already used", 400) |
|
|
client.end(); |
|
|
client.end(); |
|
|
res.status(400).json({error: "Email already exists"}); |
|
|
res.status(400).json({ error: "Email already exists" }); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -293,7 +293,7 @@ export async function update(req, res) { |
|
|
if (usernameResult.rows[0]) { |
|
|
if (usernameResult.rows[0]) { |
|
|
logger.write("failed to update because username is already used", 400) |
|
|
logger.write("failed to update because username is already used", 400) |
|
|
client.end(); |
|
|
client.end(); |
|
|
res.status(400).json({error: "Username already exists"}); |
|
|
res.status(400).json({ error: "Username already exists" }); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -318,12 +318,12 @@ export async function update(req, res) { |
|
|
path.join(__dirname, "..", "uploads", "profiles", profilePicture), |
|
|
path.join(__dirname, "..", "uploads", "profiles", profilePicture), |
|
|
path.join(__dirname, "..", "uploads", "profiles", user.username + "." + profilePicture.split(".").pop()), |
|
|
path.join(__dirname, "..", "uploads", "profiles", user.username + "." + profilePicture.split(".").pop()), |
|
|
(err) => { |
|
|
(err) => { |
|
|
if (err) { |
|
|
if (err) { |
|
|
logger.write("failed to update profile picture", 500); |
|
|
logger.write("failed to update profile picture", 500); |
|
|
console.error("Error renaming file:", err); |
|
|
console.error("Error renaming file:", err); |
|
|
throw err; |
|
|
throw err; |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
profilePicture = "/api/media/profile/" + user.username + "." + profilePicture.split(".").pop(); |
|
|
profilePicture = "/api/media/profile/" + user.username + "." + profilePicture.split(".").pop(); |
|
|
|
|
|
|
|
|
const updateQuery = `UPDATE users SET email = $1, username = $2, password = $3, picture = $4 WHERE id = $5 RETURNING id, email, username, picture`; |
|
|
const updateQuery = `UPDATE users SET email = $1, username = $2, password = $3, picture = $4 WHERE id = $5 RETURNING id, email, username, picture`; |
|
|
@ -334,7 +334,7 @@ export async function update(req, res) { |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.log(err); |
|
|
console.log(err); |
|
|
client.end() |
|
|
client.end() |
|
|
res.status(500).json({error: err}); |
|
|
res.status(500).json({ error: err }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
@ -348,7 +348,7 @@ export async function deleteUser(req, res) { |
|
|
await client.query(query, [id]); |
|
|
await client.query(query, [id]); |
|
|
logger.write("successfully deleted user " + id); |
|
|
logger.write("successfully deleted user " + id); |
|
|
client.end(); |
|
|
client.end(); |
|
|
res.status(200).json({message: 'User deleted'}); |
|
|
res.status(200).json({ message: 'User deleted' }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export async function getChannel(req, res) { |
|
|
export async function getChannel(req, res) { |
|
|
@ -363,13 +363,13 @@ export async function getChannel(req, res) { |
|
|
if (!result.rows[0]) { |
|
|
if (!result.rows[0]) { |
|
|
logger.write("failed to retrieve channel of user " + id + " because it doesn't exist", 404); |
|
|
logger.write("failed to retrieve channel of user " + id + " because it doesn't exist", 404); |
|
|
client.end(); |
|
|
client.end(); |
|
|
res.status(404).json({error: "Channel Not Found"}); |
|
|
res.status(404).json({ error: "Channel Not Found" }); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logger.write("successfully retrieved channel of user " + id, 200); |
|
|
logger.write("successfully retrieved channel of user " + id, 200); |
|
|
client.end(); |
|
|
client.end(); |
|
|
res.status(200).json({channel: result.rows[0]}); |
|
|
res.status(200).json({ channel: result.rows[0] }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export async function getHistory(req, res) { |
|
|
export async function getHistory(req, res) { |
|
|
@ -414,7 +414,7 @@ export async function getHistory(req, res) { |
|
|
|
|
|
|
|
|
if (!result.rows[0]) { |
|
|
if (!result.rows[0]) { |
|
|
logger.write("failed to retrieve history of user " + id + " because it doesn't exist", 404); |
|
|
logger.write("failed to retrieve history of user " + id + " because it doesn't exist", 404); |
|
|
res.status(404).json({error: "History Not Found"}); |
|
|
res.status(404).json({ error: "History Not Found" }); |
|
|
client.end(); |
|
|
client.end(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
@ -440,11 +440,11 @@ export async function isSubscribed(req, res) { |
|
|
if (result.rows[0]) { |
|
|
if (result.rows[0]) { |
|
|
logger.write(`user ${userId} is subscribed to channel ${channelId}`, 200); |
|
|
logger.write(`user ${userId} is subscribed to channel ${channelId}`, 200); |
|
|
client.end(); |
|
|
client.end(); |
|
|
return res.status(200).json({subscribed: true}); |
|
|
return res.status(200).json({ subscribed: true }); |
|
|
} else { |
|
|
} else { |
|
|
logger.write(`user ${userId} is not subscribed to channel ${channelId}`, 200); |
|
|
logger.write(`user ${userId} is not subscribed to channel ${channelId}`, 200); |
|
|
client.end(); |
|
|
client.end(); |
|
|
return res.status(200).json({subscribed: false}); |
|
|
return res.status(200).json({ subscribed: false }); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -454,13 +454,13 @@ export async function searchByUsername(req, res) { |
|
|
const logger = req.body.logger; |
|
|
const logger = req.body.logger; |
|
|
logger.action("try to search user by username " + username); |
|
|
logger.action("try to search user by username " + username); |
|
|
|
|
|
|
|
|
const query = `SELECT * FROM users WHERE username ILIKE $1`; |
|
|
const query = `SELECT id, username, picture, email, is_verified FROM users WHERE username ILIKE $1`; |
|
|
const result = await client.query(query, [`%${username}%`]); |
|
|
const result = await client.query(query, [`%${username}%`]); |
|
|
|
|
|
|
|
|
if (result.rows.length === 0) { |
|
|
if (result.rows.length === 0) { |
|
|
logger.write("no user found with username " + username, 404); |
|
|
logger.write("no user found with username " + username, 404); |
|
|
client.end(); |
|
|
client.end(); |
|
|
return res.status(404).json({error: "User Not Found"}); |
|
|
return res.status(404).json({ error: "User Not Found" }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logger.write("successfully found user with username " + username, 200); |
|
|
logger.write("successfully found user with username " + username, 200); |
|
|
|