hackforla / HomeUniteUs

We're working with community non-profits who have a Host Home or empty bedrooms initiative to develop a workflow management tool to make the process scalable (across all providers), reduce institutional bias, and effectively capture data.
https://homeunite.us/
GNU General Public License v2.0
36 stars 21 forks source link

Session and Refresh Endpoints Don't Work W/Default Local Configuration #633

Closed Joshua-Douglas closed 5 months ago

Joshua-Douglas commented 7 months ago

Overview

The backend API auth/session and auth/refresh endpoints always return a 401 UNAUTHORIZED response when called from frontend app deployed locally using the default api and app configurations. Both of these endpoints rely on the session cookie to provide an updated JWT, but the session cookie is never attached to the backend API requests, so the authentication always fails.

The major consequence of this bug is that the user is logged out anytime the web application is refreshed, or if the JWT expires and needs to be refreshed.

Interestingly, this bug is not present on the dev.homeunite.us deployment. The missing session cookies are due to a local configuration issue.

Root Cause

I've researched this issue and identified this as a cross-origin resource sharing issue. By default our backend application is deployed to 127.0.0.1:8080 and our frontend application is deployed to localhost:4040. The frontend development server does forward requests made to localhost:4040/api to localhost:8080.

Our session cookies are not sent with this configuration because: 1) 127.0.0.1 and localhost are considered as two different domains by browser same-origin policies 2) same-origin policies prevent cookies from being shared between two different origins, unless the server enables CORS by setting the correct CORS headers in the cookie response.

Reproduction Steps

Action Items

Resources/Instructions