|
| 1 | +# React Example |
| 2 | + |
| 3 | +This example intends to be a generic example of how to use `csrf-csrf`. Whilst the example is React based the backend configuration is catered to serving a Single Page Application (SPA), or any client which is being independently hosted of the API. The example assumes that the frontend is hosted cross-site to the backend API. In a case where the frontend is not hosted cross-site to the backend you would want to ensure the CSRF cookie has `sameSite` set to `strict`. |
| 4 | + |
| 5 | +If you aren't sure whether requests from your frontend to your backend are considered cross-site, check the ["What is considered a cross-site request?"](../../FAQ.md#additional-resources) in the FAQ. |
| 6 | + |
| 7 | +For this particular example it would actually be better to use `csrf-sync` instead, however this is for demonstrative purposes of `csrf-csrf`. |
| 8 | + |
| 9 | +The React app attempts to take on and follow the principles of [bulletproof-react](https://github.com/alan2207/bulletproof-react/tree/master/apps/react-vite) but does not do so exhaustively as it is just an example, the backend attempts to translate the same principles. |
| 10 | + |
| 11 | +## Running the example |
| 12 | + |
| 13 | +### With Docker |
| 14 | + |
| 15 | +The example will make use of the below ports, so make sure these ports are available, otherwise you can change them in the `docker-compose.yml` configuration. |
| 16 | + |
| 17 | +* 3700 for the frontend client (react) |
| 18 | +* 3710 for the backend API (express port) |
| 19 | +* 3779 for redis (6379 on the container) (this is only needed if you want to run the backend API locally instead of within docker) |
| 20 | +* 9229 for remote debugging of the backend |
| 21 | + |
| 22 | +In `backend` run: |
| 23 | + |
| 24 | +```bash |
| 25 | +npm install |
| 26 | +npm run build |
| 27 | +``` |
| 28 | + |
| 29 | +Then from this directory (`example/react`) run: |
| 30 | + |
| 31 | +```bash |
| 32 | +docker compose up -d |
| 33 | +``` |
| 34 | +Once the containers are up and running, you should find the React app at http://localhost:3700/ |
| 35 | + |
| 36 | +Tear it down with |
| 37 | + |
| 38 | +```bash |
| 39 | +docker compose down |
| 40 | +``` |
| 41 | +from the same directory. |
| 42 | + |
| 43 | +#### Docker Watch Mode |
| 44 | + |
| 45 | +If you want to make changes to the backend and have them update automatically whilst running with Docker, make sure to run `npm run watch` within `backend` from a terminal. Changes will be applied without needing to rebuild or restart the container. |
| 46 | + |
| 47 | +For the client, the Vite watch mode is enabled by default. Any changes made to the client will be replicated in the container, hot reloading will work as expected. |
| 48 | + |
| 49 | +### Without Docker |
| 50 | + |
| 51 | +By default Docker is much easier, however you can run without Docker. |
| 52 | + |
| 53 | +1. Run `npm install` in both `backend` and `client` |
| 54 | +2. Create a `.env` file in `backend` and populate it appropriately: |
| 55 | + |
| 56 | +``` |
| 57 | +EXAMPLE_CSRF_SECRET=Fake CSRF secret |
| 58 | +EXAMPLE_ALLOWED_ORIGINS="http://localhost:3700" |
| 59 | +EXAMPLE_API_PORT=3710 |
| 60 | +EXAMPLE_SESSION_SECRET=Fake session secret |
| 61 | +EXAMPLE_REDIS_HOST=localhost |
| 62 | +EXAMPLE_REDIS_PORT=3779 |
| 63 | +NODE_ENV=development |
| 64 | +``` |
| 65 | +3. Create a `.env` file in `client` and populate it appropriately: |
| 66 | + |
| 67 | +``` |
| 68 | +VITE_EXAMPLE_BASE_API_URL=http://localhost:3710 |
| 69 | +``` |
| 70 | +4. Make sure you have a working `redis` instance and that it's configured appropriately in the above environment files |
| 71 | + * You could run via Docker first (as above) and then stop the `csrf-client` and `csrf-backend` containers, leaving the `csrf-redis` container. |
| 72 | + * Alternatively give the `redis` service a docker-compose profile and only run that. |
| 73 | +5. Run `npm run dev` in `backend` from one terminal |
| 74 | +6. Run `npm run dev` in `client` from another terminal |
| 75 | + |
| 76 | +### Development |
| 77 | + |
| 78 | +For local development of the example you will want to run `npm install` under both of the `client` and the `backend`. Once you have the containers running, if you want or need to install a new dependency, you'll need to re-run `npm install` on the container as well. You can do this by connecting to the container and running `npm install` from the `/app` directory |
| 79 | + |
| 80 | +```bash |
| 81 | +docker exec -it csrf-backend sh |
| 82 | +docker exec -it csrf-client sh |
| 83 | +``` |
| 84 | + |
0 commit comments