Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
Problem with Firebase Auth Verification using Nuxt Server Middleware

I'm working on a nuxt project and I use @nuxt/firebase module to login/logout the user and it works fine.

Now i'm trying to verify the id token when a user visits the admin page and do something depending on the result, but I'm stuck on getting the verifyidtoken method to work.

I cant access nuxt.$fire.auth (I can in my plugins for example), I figured it's because this runs on the server so i had to import the packages but i'm not sure this is the optimal way.

I configured the nuxt.config.js to use a module named auth.js

modules/auth /

import cookie from "cookie";
import { initializeApp, applicationDefault } from "firebase-admin/app";
import { getAuth } from "firebase-admin/auth";

export default function () {
  const { nuxt } = this;

  nuxt.hook("render:setupMiddleware", (app) => {
    app.use("/admin", handler);
  });

  const handler = (req, res, next) => {
    const idToken = cookie.parse(req.headers.cookie)["idToken"];

    const fireAuth = initializeApp({
      credential: applicationDefault(),
      projectId: "testApp",
    });

    getAuth(fireAuth)
      .verifyIdToken(idToken)
      .then((decodedToken) => {
        console.log(decodedToken);
      })
      .catch((error) => {
        // Handle error
      });

    next();
  };
}

This works for the first time i visit the page. If i reload, it gives me the error that a firebase app is already initialized.

The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.

I dont know how to put a guard clause to see if the app is already initialized. Moreover, I feel that this is not the best approach.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda