# "You forgot to wrap your component in \<Auth0Provider\>" only in built application

**URL:** <https://community.auth0.com/t/you-forgot-to-wrap-your-component-in-auth0provider-only-in-built-application/120322>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [November 6, 2023, 3:30pm UTC](https://community.auth0.com/t/you-forgot-to-wrap-your-component-in-auth0provider-only-in-built-application/120322 "2023-11-06T15:30:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschersten](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mschersten/32/8917_2.png) [@mschersten](https://community.auth0.com/u/mschersten)\
**Post date:** [November 6, 2023, 3:30pm UTC](https://community.auth0.com/t/you-forgot-to-wrap-your-component-in-auth0provider-only-in-built-application/120322/1 "2023-11-06T15:30:56Z")

</div>

I have a react application using auth0-react and react-router-dom. I use getAccessTokenSilently() from useAuth0() to add a token to API request headers. I also use user from useAuth0() to display the user name on the top banner.

All of this works correctly when running the application locally with the “vite” command. However, when building the application with “tsc && vite build” and then running it locally from the dist folder using “vite preview”, as well as when this is done to deploy the application to the cloud, the following error is produced when calling getAccessTokenSilently(): “You forgot to wrap your component in ”. This call is nested many layers deep, however the username derived from useAuth0 is still displayed correctly, showing that my Auth0Provider is still working.

I have tried different approaches to requesting the token, like doing different parts in useEffect and providing different dependencies, and attempting to wrap the components making the request directly in another instance of Auth0Provider. Some of these methods still work in development but none work in the built version. Additionally, I have tried getting user from useAuth0() at the same time but that returns undefined when/where I have this issue, even though the banner still shows the username.

This builds to a single .js and .css file, and inspecting the react components in chrome react devtools it looks like everything is inside of the Auth0Provider. Any help resolving this would be appreciated.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [November 13, 2023, 3:31pm UTC](https://community.auth0.com/t/you-forgot-to-wrap-your-component-in-auth0provider-only-in-built-application/120322/3 "2023-11-13T15:31:53Z")

</div>

Hi @mschersten,

Welcome to the Auth0 Community!

Here’s a couple of threads that provide additional context on the error you’re seeing. If you don’t find an answer here, can you please share some code snippets of your implementation. Don’t include any sensitive data.

> [@Auth0, React and Single-Spa - \[You forgot to wrap your component in \<Auth0Provider\>\]](https://community.auth0.com/t/auth0-react-and-single-spa-you-forgot-to-wrap-your-component-in-auth0provider/77731):
>
> I am creating an app using Single-Spa, React and Auth0. The auth0/auth0-react documentation specifies to wrap the app in the following way: \<Auth0Provider domain="YOUR\_AUTH0\_DOMAIN" clientId="YOUR\_AUTH0\_CLIENT\_ID" redirectUri={window.location.origin} \> \<App /\> \</Auth0Provider\> However with Single-Spa the components are mounted and un-mounted based on the url path, as specified by the root-config’s index.ejs file (there is no root component). With Single-Spa I do not have …

> <https://stackoverflow.com/questions/70760724/how-to-logout-with-auth0-sdk-you-forgot-to-wrap-your-component-in-auth0provid>

---

<div class="post-metadata">

**Author:** ![mschersten](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mschersten/32/8917_2.png) [@mschersten](https://community.auth0.com/u/mschersten)\
**Post date:** [November 14, 2023, 1:10pm UTC](https://community.auth0.com/t/you-forgot-to-wrap-your-component-in-auth0provider-only-in-built-application/120322/4 "2023-11-14T13:10:58Z")

</div>

Hi Dan,

Thanks for the reply. I have reviewed just about every mention of “you forgot to wrap your component” I can find on Google. Again, this does work using “vite dev” to run the application directly from the unbuilt application in src/, and it’s only in the built application in dist/ with “vite preview” that I have the issue. I’ll see if I can reproduce the issue in a more bare-bones example of it, but this seems to be related to react-router-dom.

We are using react-router-dom v6 with createBrowserRouter() and \<RouterProvider\>, like this:

```auto
const AppRouterProvider = ({ routes }: AppRouterProviderProps) => {
  const { getAccessTokenSilently } = useAuth0()

  useEffect(()=>{
    const getToken = async () => {
      const token = await getAccessTokenSilently()
      console.log(token);
      return token;
    }
    getToken();
  })
  const router = createBrowserRouter(routes)
  return <RouterProvider router={router} />
}

```

That getToken() is successful in both the dev and preview version, but any requests made in a route only work in the dev version.

When I look at the components in chrome react devtools I see that everything is contained within my Auth0Provider:

![image](https://global.discourse-cdn.com/auth0/original/3X/1/2/124fd9be8af7a949decc488e11839f1856e890e9.png)

This is identical in both the dev and preview version. Any advice about how to debug this would be appreciated.

Mike

---

<div class="post-metadata">

**Author:** ![penandpaper](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/penandpaper/32/49208_2.png) [@penandpaper](https://community.auth0.com/u/penandpaper)\
**Post date:** [November 23, 2023, 8:34am UTC](https://community.auth0.com/t/you-forgot-to-wrap-your-component-in-auth0provider-only-in-built-application/120322/5 "2023-11-23T08:34:42Z")

</div>

This exact problem we are having too and we are on `auth0 ^2.2.3`

Did you happen to fix this issue?

---

<div class="post-metadata">

**Author:** ![Mostak-Ahamed-Nishat](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mostak-ahamed-nishat/32/57094_2.png) [@Mostak-Ahamed-Nishat](https://community.auth0.com/u/Mostak-Ahamed-Nishat)\
**Post date:** [May 14, 2024, 5:57am UTC](https://community.auth0.com/t/you-forgot-to-wrap-your-component-in-auth0provider-only-in-built-application/120322/6 "2024-05-14T05:57:30Z")

</div>

Even if we wrap our main component with `<Auth0Provider>` at the root level in React, we will probably encounter this error when attempting to retrieve the `getAccessTokenSilently` at the same level. This occurs because we wrap our `App` with App` , causing us to obtain the token at the child component.So we can only get the getAccessTokenSilently at children not the same page.

**So I did this way :**

1. Create a 2 seperate compoenent
2. Wrap main component with my component

**Auth0ProviderWithNavigate.tsx**

```auto
import { AppState, Auth0Provider, User } from "@auth0/auth0-react";
import { useNavigate } from "react-router-dom";

type Props = {
  children: React.ReactNode;
};

function Auth0ProviderWithNavigate({ children }: Props) {
  
  const domain = import.meta.env.VITE_AUTH0_DOMAIN;
  const clientId = import.meta.env.VITE_AUTH0_CLIENT_ID;
  const redirectUri = import.meta.env.VITE_AUTH0_REDIRECT_URI;

  const navigate = useNavigate();

  if (!domain || !clientId || !redirectUri) {
    throw Error("Unable to initialize auth0. something missing");
  }
  const onTheTimeThatRedirecting = (appState?: AppState, user?: User) => {
    navigate("/auth-callback");
  };

  return (
    <Auth0Provider
      domain={domain}
      clientId={clientId}
      authorizationParams={{
        redirect_uri: redirectUri,
      }}
      onRedirectCallback={onTheTimeThatRedirecting}
    >
      {children}
    </Auth0Provider>
  );
}

export default Auth0ProviderWithNavigate;

```

**AppRoutes.tsx**

```auto
<Route path="/auth-callback" element={<AuthCallbackPage />} />

```

**AuthCallbackPage.tsx**

```auto
import { useCreateUser } from "@/api/MyUserApi";
import { useAuth0 } from "@auth0/auth0-react";
import { useEffect, useRef } from "react";
import { useNavigate } from "react-router-dom";

const AuthCallbackPage = () => {
  const { user } = useAuth0();
  const { createUser } = useCreateUser();
  const navigate = useNavigate();

  const hasCreatedUser = useRef(false);

  useEffect(() => {
    if (user?.sub && user?.email && !hasCreatedUser.current) {
      createUser({ auth0Id: user?.sub, email: user?.email });
      hasCreatedUser.current = true;
    }
    navigate("/");
  }, [createUser, navigate, user]);

  return <>Loading......</>;
};
export default AuthCallbackPage;

```

**main.tsx** Wrap your main component with coustom

```auto
 <Auth0ProviderWithNavigate>
        <App/>
  </Auth0ProviderWithNavigate>

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 7, 2024, 2:00am UTC](https://community.auth0.com/t/you-forgot-to-wrap-your-component-in-auth0provider-only-in-built-application/120322/7 "2024-12-07T02:00:47Z")

</div>

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