Refactor OMDb API handling and user registration verification
Co-authored-by: mathieumoran1 <[email protected]>
This commit is contained in:
co-authored by
mathieumoran1
parent
4dced844e4
commit
d0edfe9278
+29
-2
@@ -45,7 +45,12 @@ function hasAnySource({ vod_link, video_src, embed_src }) {
|
|||||||
|
|
||||||
// Fetch movie details from OMDb API
|
// Fetch movie details from OMDb API
|
||||||
async function fetchMovieDetailsFromOmdb(imdb_id) {
|
async function fetchMovieDetailsFromOmdb(imdb_id) {
|
||||||
const apiKey = process.env.OMDB_API; // Replace with your actual OMDb API key
|
const apiKey = typeof process.env.OMDB_API === "string" ? process.env.OMDB_API.trim() : "";
|
||||||
|
if (!apiKey) {
|
||||||
|
const err = new Error("OMDb n'est pas configuré (env OMDB_API manquante).");
|
||||||
|
err.code = "OMDB_NOT_CONFIGURED";
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
const omdbUrl = `https://www.omdbapi.com/?i=${encodeURIComponent(imdb_id)}&apikey=${apiKey}`;
|
const omdbUrl = `https://www.omdbapi.com/?i=${encodeURIComponent(imdb_id)}&apikey=${apiKey}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -204,7 +209,22 @@ router.post("/add", async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch movie details from OMDb API
|
// Fetch movie details from OMDb API
|
||||||
const movieDetails = await fetchMovieDetailsFromOmdb(imdb_id);
|
let movieDetails;
|
||||||
|
try {
|
||||||
|
movieDetails = await fetchMovieDetailsFromOmdb(imdb_id);
|
||||||
|
} catch (err) {
|
||||||
|
// If OMDb isn't configured, keep the feature usable: add with placeholders so admin can fill manually later.
|
||||||
|
if (err?.code === "OMDB_NOT_CONFIGURED") {
|
||||||
|
movieDetails = {
|
||||||
|
title: String(imdb_id || "").trim() || "Untitled",
|
||||||
|
description: "",
|
||||||
|
rating: "",
|
||||||
|
poster: ""
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Proceed with adding the movie
|
// Proceed with adding the movie
|
||||||
const movie = new Movie({
|
const movie = new Movie({
|
||||||
@@ -363,11 +383,18 @@ router.put("/:id", async (req, res) => {
|
|||||||
|
|
||||||
// If refreshOmdb is true, overwrite OMDb-derived fields from the imdb_id
|
// If refreshOmdb is true, overwrite OMDb-derived fields from the imdb_id
|
||||||
if (refreshOmdb) {
|
if (refreshOmdb) {
|
||||||
|
try {
|
||||||
const details = await fetchMovieDetailsFromOmdb(movie.imdb_id);
|
const details = await fetchMovieDetailsFromOmdb(movie.imdb_id);
|
||||||
movie.title = details.title;
|
movie.title = details.title;
|
||||||
movie.description = details.description;
|
movie.description = details.description;
|
||||||
movie.rating = details.rating;
|
movie.rating = details.rating;
|
||||||
movie.poster = details.poster;
|
movie.poster = details.poster;
|
||||||
|
} catch (err) {
|
||||||
|
if (err?.code === "OMDB_NOT_CONFIGURED") {
|
||||||
|
return res.status(503).json({ message: "OMDb n'est pas configuré (env OMDB_API manquante)." });
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (title !== undefined) movie.title = title;
|
if (title !== undefined) movie.title = title;
|
||||||
if (description !== undefined) movie.description = description;
|
if (description !== undefined) movie.description = description;
|
||||||
|
|||||||
+18
-7
@@ -11,13 +11,21 @@ const router = express.Router();
|
|||||||
|
|
||||||
// Set password for verification
|
// Set password for verification
|
||||||
|
|
||||||
const woof = process.env.DOG_NAMES ? process.env.DOG_NAMES.split(",") : [];
|
const woof =
|
||||||
|
typeof process.env.DOG_NAMES === "string"
|
||||||
|
? process.env.DOG_NAMES.split(",").map((s) => s.trim()).filter(Boolean)
|
||||||
|
: [];
|
||||||
|
|
||||||
const ACTIVE_YEAR_KEY = "active_oscar_year";
|
const ACTIVE_YEAR_KEY = "active_oscar_year";
|
||||||
|
|
||||||
// Fetch movie details from OMDb API
|
// Fetch movie details from OMDb API
|
||||||
async function fetchMovieDetailsFromOmdb(imdb_id) {
|
async function fetchMovieDetailsFromOmdb(imdb_id) {
|
||||||
const apiKey = process.env.OMDB_API; // Replace with your actual OMDb API key
|
const apiKey = typeof process.env.OMDB_API === "string" ? process.env.OMDB_API.trim() : "";
|
||||||
|
if (!apiKey) {
|
||||||
|
const err = new Error("OMDb n'est pas configuré (env OMDB_API manquante).");
|
||||||
|
err.code = "OMDB_NOT_CONFIGURED";
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
const omdbUrl = `https://www.omdbapi.com/?i=${encodeURIComponent(imdb_id)}&apikey=${apiKey}`;
|
const omdbUrl = `https://www.omdbapi.com/?i=${encodeURIComponent(imdb_id)}&apikey=${apiKey}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -161,7 +169,11 @@ router.get("/stats", verifyToken, async (req, res) => {
|
|||||||
// Register User
|
// Register User
|
||||||
router.post("/register", async (req, res) => {
|
router.post("/register", async (req, res) => {
|
||||||
const { name, email, password, role, verifoof } = req.body;
|
const { name, email, password, role, verifoof } = req.body;
|
||||||
|
const dogCheckEnabled = woof.length > 0;
|
||||||
|
|
||||||
function registerVerification() {
|
function registerVerification() {
|
||||||
|
// If DOG_NAMES isn't configured, skip the "dog name" verification entirely.
|
||||||
|
if (!dogCheckEnabled) return true;
|
||||||
return woof.includes(verifoof);
|
return woof.includes(verifoof);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,16 +181,16 @@ router.post("/register", async (req, res) => {
|
|||||||
if (!password) {
|
if (!password) {
|
||||||
throw new Error("Le mot de passe est obligatoire et ne peut être indéfini");
|
throw new Error("Le mot de passe est obligatoire et ne peut être indéfini");
|
||||||
}
|
}
|
||||||
if (!verifoof) {
|
if (dogCheckEnabled && !verifoof) {
|
||||||
throw new Error("Le nom du chien doit être défini");
|
throw new Error("Le nom du chien doit être défini");
|
||||||
}
|
}
|
||||||
if (!registerVerification()) {
|
if (!registerVerification()) {
|
||||||
return res.status(201).json({ message: "Mauvais nom de chien" });
|
return res.status(400).json({ message: "Mauvais nom de chien" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingUser = await User.findOne({ email });
|
const existingUser = await User.findOne({ email });
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
return res.status(201).json({ message: "L'email existe déjà, veuillez en choisir un autre." });
|
return res.status(409).json({ message: "L'email existe déjà, veuillez en choisir un autre." });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash password and create user
|
// Hash password and create user
|
||||||
@@ -188,8 +200,7 @@ router.post("/register", async (req, res) => {
|
|||||||
|
|
||||||
res.status(201).json({ message: "L'utilisateur s'est enregistré avec succès !" });
|
res.status(201).json({ message: "L'utilisateur s'est enregistré avec succès !" });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
res.status(400).json({ error: err.message });
|
return res.status(400).json({ message: err?.message || "Erreur interne" });
|
||||||
res.status(403).json({ message: "Erreur interne lmao" });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user