I’m building an Express backend to a React app. I am at the step where I am using OIDC to authorize through Auth0.
I would like to be able to bring up the login portal in my React app.
I am currently able to bring up my login portal if I go directly to the Express URL in the browser, but I get an error if I press my login button in my React frontend.
My backend code is based off of [this code](My code is based off of this code. My frontend code is very basic:
...
function App() {
async function login() {
let res = await fetch('https://localhost:4000/auth',
{
method: 'GET',
}
}
}
return (
<div>
<Button onClick={login}>Login</Button>
</div>
)
}
...
/*
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://xxx.auth0.com/authorize?client_id=xxx&scope=openid&response_type=code&state=xxx&code_challenge=xxx&code_challenge_method=S256. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
Login: TypeError: NetworkError when attempting to fetch resource. App.js:19
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://xxx.auth0.com/authorize?client_id=xxx&scope=openid&response_type=code&state=xxx&code_challenge=xxx&code_challenge_method=S256. (Reason: CORS request did not succeed).
*/
I am running both the express and react dev servers behind a caddy reverse proxy so that they are secured by TLS during local testing.
When I try to press the login button, I get a 302 response from express, then another 302 response after the request is redirected to xxx.auth0.com. Then the error occurs. When I just go to the express backend URL in the browser (e.g. https://localhost:4000/auth ), then I am redirected correctly to the Auth0 login prompt.
Why am I not redirected properly in the React app? Why does it generate an error? What should I do to fix this?