import { getDoc, doc } from "firebase/firestore";
import { db } from "@/app/firebase";
export async function POST(req) {
try {
const body = await req.json();
console.log("corps de la requete", body)
// Vérification des paramètres requis
if (!body.uid || !body.token) {
return new Response(
JSON.stringify({ error: "UID ou token manquant." }),
{ status: 400, headers: { "Content-Type": "application/json" } }
);
}
// Récupérer le document du token depuis Firestore
const tokenDoc = await getDoc(doc(db, "tokens", uid));
// Vérifier si le token existe
if (!tokenDoc.exists()) {
return new Response(
JSON.stringify({
isValid: false,
errorMessage: "Aucun token trouvé pour cet utilisateur."
}),
{ status: 404, headers: { "Content-Type": "application/json" } }
);
}
const tokenData = tokenDoc.data();
const storedToken = tokenData.token;
const expirationDate = tokenData.expirationDate.toDate();
// Vérification de la validité du token
if (storedToken !== token) {
return new Response(
JSON.stringify({
isValid: false,
errorMessage: "Token invalide."
}),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
// Vérification de la date d'expiration
if (new Date() > expirationDate) {
return new Response(
JSON.stringify({
isValid: false,
errorMessage: "Token expiré. Redirection vers la page de connexion.",
redirectUrl: "/login"
}),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
// Si le token est valide et non expiré
return new Response(
JSON.stringify({
isValid: true,
message: "Token valide. Redirection vers Channel TV.",
redirectUrl: "/home/channelTv",
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
} catch (error) {
console.error("Erreur serveur :", error);
return new Response(
JSON.stringify({ error: "Erreur interne du serveur." }),
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
}
Someone can help me why it print me (""UID ou token manquant.") after trying to validate token from my tokens in Firestore ? using Nextjs
Top comments (0)