How to call my external API?

  1. I am trying to fetch data from my NextJS frontend from my backend running with fastapi. Both are using auth0 for authentication. Now the NextJS with package creates a session cookie but the API wants a JWT Token. Cant I use credentials: "include"?
    I found the docs and do I really have to fetch data from the next.js API which fetch data from my external api? Normally I use useSWR for my fetches.

I did the following according to:

import { getAccessToken } from "@auth0/nextjs-auth0";
const defaultFetcherGet = async (url: string) => {
  const { accessToken } = await getAccessToken();
  return fetch(url, {
    method: "GET",
    headers: {
      authorization: `Bearer ${accessToken}`,
      "Content-Type": "application/json",
    },
  }).then((r) => r.json());
};
export default defaultFetcherGet;

But I am getting:
Module not found: Can't resolve 'fs'

Hi @Roalkege,

Welcome to the Auth0 Community!

That’s an API protected route, so you must get your App’s access token to access the API route and then fetch the data from your external API.

According to the Module not found: Can't resolve 'fs' error, it looks like the module may not have been included in your node-modules or packages in your app. Have you made sure that the ‘fs’ module was added to your app’s node-modules and packages?

Hey I dont use fs. I added const { accessToken } = await getAccessToken(); to my code and now this error pop up.

Hi @Roalkege,

Thanks for following up.

I have not been able to reproduce the issue you mentioned.

Can you confirm if removing the code you added fixes the error?

In any case, I noticed that your code does not have error handling to handle errors that might occur during the getAccessToken() or fetch operation. You can consider adding try-catch blocks to handle potential errors.

Thanks,
Rueben

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