Simple cookie express/react app
This code is a study of a minimal react app, which interacts with an express backend to conceptually illustrate a cookie-based user session. It is based off this video and corresponding gist (thanks Amasaabu!).
How it works?
The express app exposes two endpoints that do the following:
POST
to/
with url query params:?name=foo
.GET
to/
returns thename
of the active session.
The react app presents a form that accepts a username, and it also has a button that will retrieve the active name in the user session. This more or less demonstrates how an authentication takes place.
How to run it?
cd backend && npm run dev
cd frontend && PORT=3001 npm run start
Server-side vs client-side considerations
By using express-session
this example commits to using server-side
sessions. An alternative would be to actually store the user session data
in a cookie is to use cookie-session
.
Disclaimer
Be careful with cookies and users data.