Based on the information you provided, it seems like the issue may be related to the cookie not being set correctly when deploying your React and Express app to an online server. Here are a few possible reasons and suggestions to investigate further:
CORS (Cross-Origin Resource Sharing) issue: When your frontend (React) and backend (Express) are hosted on different domains, you need to ensure that your backend is configured to allow cross-origin requests from your frontend domain. You can use the cors package in Express to enable CORS. Make sure you have configured CORS properly on your server.
Cookie configuration: Check if you have configured cookies correctly in your Express backend. Make sure you are setting the SameSite attribute properly to allow the cookie to be sent on cross-site requests. For example, you can set it to "None" if your site is served over HTTPS.
Proxy configuration: If you are using a proxy server (e.g., Nginx) in front of your Express server, ensure that it is correctly configured to forward the necessary headers, including cookies. The proxy_set_header directive in Nginx can be used for this purpose.
Secure flag: If you're using cookies over HTTPS, ensure that the Secure flag is set for the cookie. This flag ensures that the cookie is only sent over secure (HTTPS) connections.
Domain and path: Verify that the domain and path properties of the cookie are correctly configured. The domain should match the domain of your online server, and the path should be set to '/' to allow the cookie to be sent on all routes.
Proxy server caching: If you're using a proxy server, check if it is caching responses. It's possible that the server response with the user data is being cached, and subsequent requests are not hitting your Express server.
Check network requests: Use browser developer tools to inspect the network requests and responses. Look for any errors, warnings, or missing cookies in the request headers and responses. This will help identify if the cookies are not being set or if they are not being sent with subsequent requests.
By investigating these areas, you should be able to identify the cause of the issue and make the necessary adjustments to ensure successful login and retrieval of user data in your React and Express application deployed on an online server.
@randall said in Login when app is hosted:
I'm using react with express backend. I'm following the fusionauth-example-react-2.0 When login using localhost, everything works great. When the apps are uploaded to online servers, I'm not able to successfully login. With online login, I do get the login form and when I check my recent user login, it does record a login. However my '/user' request returns empty. There is definitely no token on my '/user' request. I believe the cookie is not being set probably???, but any thought would be great...