hiyaryan / the-cdj

The Cognitive Distortion Journal (CDJ) is a smart journaling tool that helps remedy distorted thinking. It can feel impossible to follow the CBT technique of labeling distorted thinking and finding alternative modes of thought (i.e. reframing) while cognitive distortions are occurring. The CDJ does that work for you. -- The CDJ is in beta testing!!
https://thecdj.app
3 stars 0 forks source link

Remember logged-in users using jsonwebtokens. #31

Closed hiyaryan closed 9 months ago

hiyaryan commented 9 months ago

This PR adds functionality to the Remember Me checkbox on the login page. It remembers which users are logged in using localStorage and jsonwebtokens.

The initial commit adds the jsonwebtoken package to package.json.

hiyaryan commented 9 months ago

The final commits of this PR fully implements JWT token login using localStorage.

JWT tokens are stored in localStorage if the user checks Remember Me on login. If the user logs out or a user attempts to log in with a compromised token, the token is removed from localStorage.

A new /token-login route is added to the backend with associated isLoggedIn middleware, that validates the JWT token, and tokenLogin controller, that logs the user in to the current session.

Token login is handled along the Login React router route that checks if a token is stored in localStorage. If not, it renders the Login component, otherwise, it makes a request to the backend to authenticate using the token set in the authorization header.

If a user attempts to access any valid page of the site and is logged in using a token, the router to that page will send a reference to the Login react route on navigate. If authentication using the token is successful, the client will be re-routed back to page they were trying to access (provided the router sent a reference which only occurs in Entries and Account routers). This also works regardless of whether token login was used, however, in this instance the client will be re-routed only after a successful Login using the Login component form.

The very last commit is unrelated but removes a link to an image that did not exist and unrelated alt tag when the Navbar was imported at the very beginning of this project which threw a 404 every time the app was launched. Now a generic avatar (as opposed to a capital R) is used which more easily depicts a location where account settings may be located.