Error "Service not found" when trying to authenticate user using auth0 in React app with Express.js backend

0

I’m currently working on a React application with an Express.js backend where I’m trying to authenticate users using the Auth0 package. I have a setup where the frontend and backend communicate through API requests.

In my React app, I’m encountering an error: “Service not found: http://localhost:5000” when trying to make a request from the frontend to the backend after obtaining an access token. I’ve checked the configurations and URLs, but I can’t seem to pinpoint the issue.

Here’s the relevant code:

Frontend: I’m using the useMutation hook to make the API request after obtaining the access token.

const { mutate } = useMutation({
  mutationKey: [user?.email],
  mutationFn: async (token) => {
    try {
      await ApiInstance.post(
        Endpoints.createUser,
        {
          email: user?.email,
        },
        {
          headers: {
            Authorization: `Bearer ${token}`,
          },
        }
      );
    } catch (error) {
      console.error("An error occurred:", error);
      throw error;
    }
  },
});

Backend: I’m using the express-oauth2-jwt-bearer package to perform JWT token validation.

import { auth } from "express-oauth2-jwt-bearer";

export const jwtCheck = auth({
  audience: "http://localhost:5000",
  issuerBaseURL: "https://MY_DOMAIN.auth0.com",
  tokenSigningAlg: "RS256",
});

I’ve verified that the URLs, CORS configuration, and token audience match on both the frontend and backend. The Express.js server is running on http://localhost:5000.

What could be causing this “Service not found” error? Are there any common pitfalls or misconfigurations I should be aware of when setting up this type of authentication flow between a React app and an Express.js backend?

Any help or insights would be greatly appreciated. Thank you!

1 Like

Hi @santhoshprabhakaran0,

Welcome to the Auth0 Community!

Thanks for the thorough question.

The service not found error indicates that the service for which you are requesting a token (http://localhost:5000) hasn’t been registered with Auth0. Have you set up an API in the Auth0 dashboard and set the correct identifier?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.