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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay