Impossible to access “/home/channelTv” page, after insert token

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…


This content originally appeared on DEV Community and was authored by Sissoko20

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


This content originally appeared on DEV Community and was authored by Sissoko20


Print Share Comment Cite Upload Translate Updates
APA

Sissoko20 | Sciencx (2025-01-25T21:37:01+00:00) Impossible to access “/home/channelTv” page, after insert token. Retrieved from https://www.scien.cx/2025/01/25/impossible-to-access-home-channeltv-page-after-insert-token/

MLA
" » Impossible to access “/home/channelTv” page, after insert token." Sissoko20 | Sciencx - Saturday January 25, 2025, https://www.scien.cx/2025/01/25/impossible-to-access-home-channeltv-page-after-insert-token/
HARVARD
Sissoko20 | Sciencx Saturday January 25, 2025 » Impossible to access “/home/channelTv” page, after insert token., viewed ,<https://www.scien.cx/2025/01/25/impossible-to-access-home-channeltv-page-after-insert-token/>
VANCOUVER
Sissoko20 | Sciencx - » Impossible to access “/home/channelTv” page, after insert token. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/25/impossible-to-access-home-channeltv-page-after-insert-token/
CHICAGO
" » Impossible to access “/home/channelTv” page, after insert token." Sissoko20 | Sciencx - Accessed . https://www.scien.cx/2025/01/25/impossible-to-access-home-channeltv-page-after-insert-token/
IEEE
" » Impossible to access “/home/channelTv” page, after insert token." Sissoko20 | Sciencx [Online]. Available: https://www.scien.cx/2025/01/25/impossible-to-access-home-channeltv-page-after-insert-token/. [Accessed: ]
rf:citation
» Impossible to access “/home/channelTv” page, after insert token | Sissoko20 | Sciencx | https://www.scien.cx/2025/01/25/impossible-to-access-home-channeltv-page-after-insert-token/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.