0
votes

I'm trying to build a web app using auth0 and firebase firestore. for securing firestore on the backend, using firebase auth is a must. Learnt that custom token is a way to use different auth system for authenticating and also securing firestore. so went through a blog which says to use id_token given by auth0 to create custom token. have to to do all of this in custom express server and getting an error on the server Error: getaddrinfo EAI_AGAIN undefined at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) and

App.tsx:50 GET http://localhost:3001/firebase 500 (Internal Server Error) on the browser with this response: body: (...) bodyUsed: false headers: Headers {} ok: false redirected: false status: 500 statusText: "Internal Server Error" type: "cors" url: "http://localhost:3001/firebase" [[Prototype]]: Response Now not getting why this error is occuring

my backend code: server.js

const express = require('express');
const { expressjwt: jwt } = require('express-jwt');
const jwks = require('jwks-rsa');
const cors = require('cors');
const firebaseAdmin = require('firebase-admin');
const path = require('path');

const app = express();

app.use(cors());
app.use('/', express.static(path.join(__dirname, 'dist')));

const jwtCheck = jwt({
    secret: jwks.expressJwtSecret({
        cache: true,
        rateLimit: true,
        jwksRequestsPerMinute: 5,
        jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`
    }),
    audience: process.env.AUTH0_API_AUDIENCE,
    issuer: `https://${process.env.AUTH0_DOMAIN}`,
    algorithms: ['RS256']
});


const serviceAccount = require('./firebase/firebase-key.json');

firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(serviceAccount),
    databaseURL: `https://${serviceAccount.project_id}.firebaseio.com`,
});

app.get('/firebase', jwtCheck, async (req, res) => {
    const { sub: uid } = req.user;
    try {
        const firebaseToken = await firebaseAdmin.auth().createCustomToken(uid);
        res.json(firebaseToken);
    } catch (err) {
        res.statusCode(500).send({
            error: err
        });
    };
});

app.listen(3001, () => console.log('running on port 3001'));




this is frontend code requesting custom token for sign in:

import { FC, useEffect, useState } from "react";

import { useAuth0 } from "@auth0/auth0-react";

import { signInWithCustomToken } from "firebase/auth";
import { auth } from "./lib/firebase"; 



const App:FC = () => {

  //variables
  const { loginWithRedirect, logout, user, isAuthenticated, getIdTokenClaims } = useAuth0();


  //func types

  let logIn: () => void
  let logOut: () => void
  
  //functionalities

  logIn = () => {
    loginWithRedirect()
 
  }

  logOut = () => {
    logout({returnTo: window.location.origin})
  }


  useEffect(() => {
    const authenticate = async() => {
      const claims = await getIdTokenClaims();
      const response = await fetch('http://localhost:3001/firebase', {
        headers: {
          'Authorization': `Bearer ${claims?.__raw}`
        }
      })
      console.log(response)
    }
    isAuthenticated && authenticate();
  }, [isAuthenticated])