DEV Community

Sissoko20
Sissoko20

Posted on

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 (!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" } }
);
Enter fullscreen mode Exit fullscreen mode

} 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)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video