auth0-developer-hub / spa_react_javascript_hello-world

This JavaScript code sample demonstrates how to implement authentication in a React application that uses React Router 6. You can integrate the Auth0 React SDK with React Router 6 to create route guards in React.
Apache License 2.0
62 stars 79 forks source link

Unauthorized error when connecting to backend server #8

Open gaaaku opened 1 year ago

gaaaku commented 1 year ago

What happened?

Assumption, I am running spa_react_javascript_hello-world and api_spring_java_hello-world in a local environment.

The situation is as follows.

What did you expect to happen?

I expect the message will display correctly (Correct message is "This is an admin message.") in the Admin tab.

Anything else we need to know?

When I successfully authenticated Auth0 with another of my own apps, I stored the token information in the header.

export const getHero = (token: string): Promise<AxiosResponse<Hero>> => {
    return axios.get('/api/private/hero', {
        headers: {
            Authorization: `Bearer ${token}`
        }
    });
};

I guess it may be necessary to retrieve the token and store it in the header in a React app like this.

gaaaku commented 1 year ago

The following fixes solved the problem. Would you like to include these fixes in this repository?