| 1 |
* React Single-Page Application |
| 2 |
|
| 3 |
This directory contains an example React application demonstrating the use of |
| 4 |
the headless functionality of django-allauth. The example consists of two parts, |
| 5 |
a backend running allauth in =HEADLESS_ONLY= mode, and a frontend containing the |
| 6 |
React application itself. |
| 7 |
|
| 8 |
This example can be found up and running over at [[https://react.demo.allauth.org/][react.demo.allauth.org]]. You can |
| 9 |
run it locally as well, as is documented below. |
| 10 |
|
| 11 |
|
| 12 |
** Backend |
| 13 |
|
| 14 |
The example backend Django project can be found in the =backend/= directory. It |
| 15 |
is running in =HEADLESS_ONLY= mode, meaning, no frontend facing functionality of |
| 16 |
allauth is used. Hence, there are no templates or additional apps/views to be |
| 17 |
found in this example Django project. The only points of interest of this |
| 18 |
project are: |
| 19 |
|
| 20 |
- The configuration of =HEADLESS_ONLY = True= over at =settings.py=. |
| 21 |
|
| 22 |
- Configuring =HEADLESS_FRONTEND_URLS= so that allauth can construct links pointing |
| 23 |
to the single-page React application. |
| 24 |
|
| 25 |
- The inclusion of =path("_allauth/", include("allauth.headless.urls"))= over at |
| 26 |
=urls.py=. |
| 27 |
|
| 28 |
|
| 29 |
** Frontend |
| 30 |
|
| 31 |
The frontend can be found in the =frontend/= directory. It is a fairly minimal |
| 32 |
example React project created using =create-react-app=, extended with |
| 33 |
=react-(router-)dom= for implementing routing within the single-page |
| 34 |
application. |
| 35 |
|
| 36 |
The sole purpose of this project is to demonstrate the technical integration of |
| 37 |
headless allauth within a Javascript single-page application. A pretty look and |
| 38 |
feel is not a goal, and hence, minimal effort has been spent on making the |
| 39 |
frontend visually appealing. |
| 40 |
|
| 41 |
Points of interest of this project: |
| 42 |
|
| 43 |
- [[file:frontend/src/lib/allauth.js][frontend/src/lib/allauth.js]]: This is a (React independent) file offering |
| 44 |
wrapper methods for the various allauth endpoints. |
| 45 |
|
| 46 |
- [[file:frontend/src/auth/AuthContext.js][frontend/src/auth/AuthContext.js]]: This file offers a React context provider |
| 47 |
that is able to expose the authentication state to other React components. |
| 48 |
|
| 49 |
- [[file:frontend/src/auth/hooks.js][frontend/src/auth/hooks.js]]: Offers various hooks that use the context provider above to make |
| 50 |
the current user (=useUser()=), allauth configuration (=useConfig()=) and authentication state (=useAuth()=) |
| 51 |
easily consumable in components. |
| 52 |
|
| 53 |
- [[file:frontend/src/auth/routing.js][frontend/src/auth/routing.js]]: When the authentication state changes, the user |
| 54 |
typically needs to be redirected to a different page. That logic is contained |
| 55 |
within this file. |
| 56 |
|
| 57 |
- [[file:frontend/src/account/][frontend/src/account/]]: This directory contains all components related towards |
| 58 |
the regular account functionality (e.g. login and signup using a local |
| 59 |
account, as well as changing your password and email verification). This functionality corresponds |
| 60 |
with the functionality of the =allauth.account= Django app. |
| 61 |
|
| 62 |
- [[file:frontend/src/socialaccount/][frontend/src/socialaccount/]]: This directory contains all components related |
| 63 |
towards the logging in with third-party accounts. This functionality corresponds |
| 64 |
with the functionality of the =allauth.socialaccount= Django app. |
| 65 |
|
| 66 |
- [[file:frontend/src/mfa/][frontend/src/mfa/]]: This directory contains all components related towards |
| 67 |
Two-Factor Authentication. This functionality corresponds with the |
| 68 |
functionality of the =allauth.mfa= Django app. |
| 69 |
|
| 70 |
- [[file:frontend/src/usersessions/][frontend/src/usersessions/]]: This directory contains user session management |
| 71 |
component. This functionality corresponds with the functionality of the |
| 72 |
=allauth.usersessions= Django app. |
| 73 |
|
| 74 |
- [[file:frontend/src/Router.js][frontend/src/Router.js]]: All routing is containing within this file. |
| 75 |
|
| 76 |
|
| 77 |
** Running |
| 78 |
|
| 79 |
A =docker-compose.yml= file is offered using which you can easily run the |
| 80 |
example project locally. It consists of the following services: |
| 81 |
|
| 82 |
- =backend=: The Django backend application running =allauth.headless=. |
| 83 |
The backend runs on port 8000. |
| 84 |
|
| 85 |
- =frontend=: The frontend (React) project, running on port 3000. |
| 86 |
|
| 87 |
- =proxy=: A proxy service (Traefik), running on port 10000, that depending on the URL |
| 88 |
forwards either to the backend or frontend service. |
| 89 |
|
| 90 |
- =mail=: A dummy email (SMTP) service (MailCatcher) that you can use to test |
| 91 |
e.g. email verification and password reset flows. |
| 92 |
|
| 93 |
To spin up these services, run: |
| 94 |
|
| 95 |
#+begin_src bash |
| 96 |
docker-compose up |
| 97 |
#+end_src |
| 98 |
|
| 99 |
Then, visit the app over at http://localhost:10000. |