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
35 stars 21 forks source link

Fix user redirects and tests #646

Closed erikguntner closed 4 months ago

erikguntner commented 4 months ago

Closes #645

What changes did you make?

I made these changes to correctly redirect the user based on 2 cases:

  1. When the user logs out they are redirected to /
  2. When the user tries to access a protected page. For example, when they aren’t logged in or logged in as a different user role they are redirected to the /signin in page

One issue I ran into was the ProtectedRoute component was redirecting the user after logging out instead of allowing us to choose the path. This was being caused by this line of code in app/src/components/common/AuthenticatedHeader.tsx:

appDispatch(setCredentials({user: null, token: null}))

The ProtectedRoute component relies on the user object to determine whether to navigate the user to /signin or render the necessary components. A change to the user object was causing the component to render<Navigate to="/signin" state={{from: location}} replace />; and override our call to navigate('/'). By removing the state update we can now redirect the user to the home page. However, the user object is still stored in state, but since the credentials are no longer valid the user can't log in or access any protected routes. The user object will also be reset anytime the user revisits the application or a protected endpoint.

Testing done for these changes

Screenshots of Proposed Changes Of The Website (if any, please do not screen shot code changes)

Visuals before changes are applied ![image](Paste_Your_Image_Link_Here_After_Attaching_Files)
Visuals after changes are applied https://github.com/hackforla/HomeUniteUs/assets/27253583/68faddde-ba1c-4c92-97cf-924527676abc