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

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →