├── .gitignore ├── Pipfile ├── Pipfile.lock ├── README.md ├── client ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ └── index.html └── src │ ├── App.js │ ├── components │ └── layout │ │ └── Navbar.js │ ├── index.js │ └── views │ ├── app │ └── Dashboard.js │ └── auth │ ├── Login.js │ ├── Logout.js │ └── Signup.js ├── manage.py ├── mysite ├── __init__.py ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py └── users ├── __init__.py ├── admin.py ├── apps.py ├── forms.py ├── migrations ├── 0001_initial.py └── __init__.py ├── models.py ├── serializers.py ├── tests.py ├── urls.py └── views.py /.gitignore: -------------------------------------------------------------------------------- 1 | ### Env ### 2 | .vscode 3 | .DS_Store 4 | 5 | ### Django ### 6 | *.log 7 | *.pot 8 | *.pyc 9 | __pycache__/ 10 | local_settings.py 11 | db.sqlite3 12 | db.sqlite3-journal 13 | media -------------------------------------------------------------------------------- /Pipfile: -------------------------------------------------------------------------------- 1 | [[source]] 2 | name = "pypi" 3 | url = "https://pypi.org/simple" 4 | verify_ssl = true 5 | 6 | [dev-packages] 7 | autopep8 = "*" 8 | 9 | [packages] 10 | django = "*" 11 | djangorestframework = "*" 12 | django-rest-auth = "*" 13 | django-allauth = "*" 14 | django-cors-headers = "*" 15 | 16 | [requires] 17 | python_version = "3.8" 18 | -------------------------------------------------------------------------------- /Pipfile.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_meta": { 3 | "hash": { 4 | "sha256": "5f55a4241359be3a4214f3cdc15b8cd27389156c109f6664042261ef190a9ad1" 5 | }, 6 | "pipfile-spec": 6, 7 | "requires": { 8 | "python_version": "3.8" 9 | }, 10 | "sources": [ 11 | { 12 | "name": "pypi", 13 | "url": "https://pypi.org/simple", 14 | "verify_ssl": true 15 | } 16 | ] 17 | }, 18 | "default": { 19 | "asgiref": { 20 | "hashes": [ 21 | "sha256:7e51911ee147dd685c3c8b805c0ad0cb58d360987b56953878f8c06d2d1c6f1a", 22 | "sha256:9fc6fb5d39b8af147ba40765234fa822b39818b12cc80b35ad9b0cef3a476aed" 23 | ], 24 | "version": "==3.2.10" 25 | }, 26 | "certifi": { 27 | "hashes": [ 28 | "sha256:5930595817496dd21bb8dc35dad090f1c2cd0adfaf21204bf6732ca5d8ee34d3", 29 | "sha256:8fc0819f1f30ba15bdb34cceffb9ef04d99f420f68eb75d901e9560b8749fc41" 30 | ], 31 | "version": "==2020.6.20" 32 | }, 33 | "cffi": { 34 | "hashes": [ 35 | "sha256:005f2bfe11b6745d726dbb07ace4d53f057de66e336ff92d61b8c7e9c8f4777d", 36 | "sha256:09e96138280241bd355cd585148dec04dbbedb4f46128f340d696eaafc82dd7b", 37 | "sha256:0b1ad452cc824665ddc682400b62c9e4f5b64736a2ba99110712fdee5f2505c4", 38 | "sha256:0ef488305fdce2580c8b2708f22d7785ae222d9825d3094ab073e22e93dfe51f", 39 | "sha256:15f351bed09897fbda218e4db5a3d5c06328862f6198d4fb385f3e14e19decb3", 40 | "sha256:22399ff4870fb4c7ef19fff6eeb20a8bbf15571913c181c78cb361024d574579", 41 | "sha256:23e5d2040367322824605bc29ae8ee9175200b92cb5483ac7d466927a9b3d537", 42 | "sha256:2791f68edc5749024b4722500e86303a10d342527e1e3bcac47f35fbd25b764e", 43 | "sha256:2f9674623ca39c9ebe38afa3da402e9326c245f0f5ceff0623dccdac15023e05", 44 | "sha256:3363e77a6176afb8823b6e06db78c46dbc4c7813b00a41300a4873b6ba63b171", 45 | "sha256:33c6cdc071ba5cd6d96769c8969a0531be2d08c2628a0143a10a7dcffa9719ca", 46 | "sha256:3b8eaf915ddc0709779889c472e553f0d3e8b7bdf62dab764c8921b09bf94522", 47 | "sha256:3cb3e1b9ec43256c4e0f8d2837267a70b0e1ca8c4f456685508ae6106b1f504c", 48 | "sha256:3eeeb0405fd145e714f7633a5173318bd88d8bbfc3dd0a5751f8c4f70ae629bc", 49 | "sha256:44f60519595eaca110f248e5017363d751b12782a6f2bd6a7041cba275215f5d", 50 | "sha256:4d7c26bfc1ea9f92084a1d75e11999e97b62d63128bcc90c3624d07813c52808", 51 | "sha256:529c4ed2e10437c205f38f3691a68be66c39197d01062618c55f74294a4a4828", 52 | "sha256:6642f15ad963b5092d65aed022d033c77763515fdc07095208f15d3563003869", 53 | "sha256:85ba797e1de5b48aa5a8427b6ba62cf69607c18c5d4eb747604b7302f1ec382d", 54 | "sha256:8f0f1e499e4000c4c347a124fa6a27d37608ced4fe9f7d45070563b7c4c370c9", 55 | "sha256:a624fae282e81ad2e4871bdb767e2c914d0539708c0f078b5b355258293c98b0", 56 | "sha256:b0358e6fefc74a16f745afa366acc89f979040e0cbc4eec55ab26ad1f6a9bfbc", 57 | "sha256:bbd2f4dfee1079f76943767fce837ade3087b578aeb9f69aec7857d5bf25db15", 58 | "sha256:bf39a9e19ce7298f1bd6a9758fa99707e9e5b1ebe5e90f2c3913a47bc548747c", 59 | "sha256:c11579638288e53fc94ad60022ff1b67865363e730ee41ad5e6f0a17188b327a", 60 | "sha256:c150eaa3dadbb2b5339675b88d4573c1be3cb6f2c33a6c83387e10cc0bf05bd3", 61 | "sha256:c53af463f4a40de78c58b8b2710ade243c81cbca641e34debf3396a9640d6ec1", 62 | "sha256:cb763ceceae04803adcc4e2d80d611ef201c73da32d8f2722e9d0ab0c7f10768", 63 | "sha256:cc75f58cdaf043fe6a7a6c04b3b5a0e694c6a9e24050967747251fb80d7bce0d", 64 | "sha256:d80998ed59176e8cba74028762fbd9b9153b9afc71ea118e63bbf5d4d0f9552b", 65 | "sha256:de31b5164d44ef4943db155b3e8e17929707cac1e5bd2f363e67a56e3af4af6e", 66 | "sha256:e66399cf0fc07de4dce4f588fc25bfe84a6d1285cc544e67987d22663393926d", 67 | "sha256:f0620511387790860b249b9241c2f13c3a80e21a73e0b861a2df24e9d6f56730", 68 | "sha256:f4eae045e6ab2bb54ca279733fe4eb85f1effda392666308250714e01907f394", 69 | "sha256:f92cdecb618e5fa4658aeb97d5eb3d2f47aa94ac6477c6daf0f306c5a3b9e6b1", 70 | "sha256:f92f789e4f9241cd262ad7a555ca2c648a98178a953af117ef7fad46aa1d5591" 71 | ], 72 | "version": "==1.14.3" 73 | }, 74 | "chardet": { 75 | "hashes": [ 76 | "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", 77 | "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" 78 | ], 79 | "version": "==3.0.4" 80 | }, 81 | "cryptography": { 82 | "hashes": [ 83 | "sha256:21b47c59fcb1c36f1113f3709d37935368e34815ea1d7073862e92f810dc7499", 84 | "sha256:451cdf60be4dafb6a3b78802006a020e6cd709c22d240f94f7a0696240a17154", 85 | "sha256:4549b137d8cbe3c2eadfa56c0c858b78acbeff956bd461e40000b2164d9167c6", 86 | "sha256:48ee615a779ffa749d7d50c291761dc921d93d7cf203dca2db663b4f193f0e49", 87 | "sha256:559d622aef2a2dff98a892eef321433ba5bc55b2485220a8ca289c1ecc2bd54f", 88 | "sha256:5d52c72449bb02dd45a773a203196e6d4fae34e158769c896012401f33064396", 89 | "sha256:65beb15e7f9c16e15934569d29fb4def74ea1469d8781f6b3507ab896d6d8719", 90 | "sha256:680da076cad81cdf5ffcac50c477b6790be81768d30f9da9e01960c4b18a66db", 91 | "sha256:762bc5a0df03c51ee3f09c621e1cee64e3a079a2b5020de82f1613873d79ee70", 92 | "sha256:89aceb31cd5f9fc2449fe8cf3810797ca52b65f1489002d58fe190bfb265c536", 93 | "sha256:983c0c3de4cb9fcba68fd3f45ed846eb86a2a8b8d8bc5bb18364c4d00b3c61fe", 94 | "sha256:99d4984aabd4c7182050bca76176ce2dbc9fa9748afe583a7865c12954d714ba", 95 | "sha256:9d9fc6a16357965d282dd4ab6531013935425d0dc4950df2e0cf2a1b1ac1017d", 96 | "sha256:a7597ffc67987b37b12e09c029bd1dc43965f75d328076ae85721b84046e9ca7", 97 | "sha256:ab010e461bb6b444eaf7f8c813bb716be2d78ab786103f9608ffd37a4bd7d490", 98 | "sha256:b12e715c10a13ca1bd27fbceed9adc8c5ff640f8e1f7ea76416352de703523c8", 99 | "sha256:b2bded09c578d19e08bd2c5bb8fed7f103e089752c9cf7ca7ca7de522326e921", 100 | "sha256:b372026ebf32fe2523159f27d9f0e9f485092e43b00a5adacf732192a70ba118", 101 | "sha256:cb179acdd4ae1e4a5a160d80b87841b3d0e0be84af46c7bb2cd7ece57a39c4ba", 102 | "sha256:e97a3b627e3cb63c415a16245d6cef2139cca18bb1183d1b9375a1c14e83f3b3", 103 | "sha256:f0e099fc4cc697450c3dd4031791559692dd941a95254cb9aeded66a7aa8b9bc", 104 | "sha256:f99317a0fa2e49917689b8cf977510addcfaaab769b3f899b9c481bbd76730c2" 105 | ], 106 | "version": "==3.1.1" 107 | }, 108 | "defusedxml": { 109 | "hashes": [ 110 | "sha256:6687150770438374ab581bb7a1b327a847dd9c5749e396102de3fad4e8a3ef93", 111 | "sha256:f684034d135af4c6cbb949b8a4d2ed61634515257a67299e5f940fbaa34377f5" 112 | ], 113 | "version": "==0.6.0" 114 | }, 115 | "django": { 116 | "hashes": [ 117 | "sha256:a2127ad0150ec6966655bedf15dbbff9697cc86d61653db2da1afa506c0b04cc", 118 | "sha256:c93c28ccf1d094cbd00d860e83128a39e45d2c571d3b54361713aaaf9a94cac4" 119 | ], 120 | "index": "pypi", 121 | "version": "==3.1.2" 122 | }, 123 | "django-allauth": { 124 | "hashes": [ 125 | "sha256:0bfb975fe46e8bff67effb3fa46dea5acd7c66140cb78b5ed92c533f639898e2" 126 | ], 127 | "index": "pypi", 128 | "version": "==0.43.0" 129 | }, 130 | "django-cors-headers": { 131 | "hashes": [ 132 | "sha256:9322255c296d5f75089571f29e520c83ff9693df17aa3cf9f6a4bea7c6740169", 133 | "sha256:db82b2840f667d47872ae3e4a4e0a0d72fbecb42779b8aa233fa8bb965f7836a" 134 | ], 135 | "index": "pypi", 136 | "version": "==3.5.0" 137 | }, 138 | "django-rest-auth": { 139 | "hashes": [ 140 | "sha256:f11e12175dafeed772f50d740d22caeab27e99a3caca24ec65e66a8d6de16571" 141 | ], 142 | "index": "pypi", 143 | "version": "==0.9.5" 144 | }, 145 | "djangorestframework": { 146 | "hashes": [ 147 | "sha256:5c5071fcbad6dce16f566d492015c829ddb0df42965d488b878594aabc3aed21", 148 | "sha256:d54452aedebb4b650254ca092f9f4f5df947cb1de6ab245d817b08b4f4156249" 149 | ], 150 | "index": "pypi", 151 | "version": "==3.12.1" 152 | }, 153 | "idna": { 154 | "hashes": [ 155 | "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6", 156 | "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0" 157 | ], 158 | "version": "==2.10" 159 | }, 160 | "oauthlib": { 161 | "hashes": [ 162 | "sha256:bee41cc35fcca6e988463cacc3bcb8a96224f470ca547e697b604cc697b2f889", 163 | "sha256:df884cd6cbe20e32633f1db1072e9356f53638e4361bef4e8b03c9127c9328ea" 164 | ], 165 | "version": "==3.1.0" 166 | }, 167 | "pycparser": { 168 | "hashes": [ 169 | "sha256:2d475327684562c3a96cc71adf7dc8c4f0565175cf86b6d7a404ff4c771f15f0", 170 | "sha256:7582ad22678f0fcd81102833f60ef8d0e57288b6b5fb00323d101be910e35705" 171 | ], 172 | "version": "==2.20" 173 | }, 174 | "pyjwt": { 175 | "extras": [ 176 | "crypto" 177 | ], 178 | "hashes": [ 179 | "sha256:5c6eca3c2940464d106b99ba83b00c6add741c9becaec087fb7ccdefea71350e", 180 | "sha256:8d59a976fb773f3e6a39c85636357c4f0e242707394cadadd9814f5cbaa20e96" 181 | ], 182 | "version": "==1.7.1" 183 | }, 184 | "python3-openid": { 185 | "hashes": [ 186 | "sha256:33fbf6928f401e0b790151ed2b5290b02545e8775f982485205a066f874aaeaf", 187 | "sha256:6626f771e0417486701e0b4daff762e7212e820ca5b29fcc0d05f6f8736dfa6b" 188 | ], 189 | "version": "==3.2.0" 190 | }, 191 | "pytz": { 192 | "hashes": [ 193 | "sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed", 194 | "sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048" 195 | ], 196 | "version": "==2020.1" 197 | }, 198 | "requests": { 199 | "hashes": [ 200 | "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b", 201 | "sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898" 202 | ], 203 | "version": "==2.24.0" 204 | }, 205 | "requests-oauthlib": { 206 | "hashes": [ 207 | "sha256:7f71572defaecd16372f9006f33c2ec8c077c3cfa6f5911a9a90202beb513f3d", 208 | "sha256:b4261601a71fd721a8bd6d7aa1cc1d6a8a93b4a9f5e96626f8e4d91e8beeaa6a" 209 | ], 210 | "version": "==1.3.0" 211 | }, 212 | "six": { 213 | "hashes": [ 214 | "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259", 215 | "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced" 216 | ], 217 | "version": "==1.15.0" 218 | }, 219 | "sqlparse": { 220 | "hashes": [ 221 | "sha256:017cde379adbd6a1f15a61873f43e8274179378e95ef3fede90b5aa64d304ed0", 222 | "sha256:0f91fd2e829c44362cbcfab3e9ae12e22badaa8a29ad5ff599f9ec109f0454e8" 223 | ], 224 | "version": "==0.4.1" 225 | }, 226 | "urllib3": { 227 | "hashes": [ 228 | "sha256:8d7eaa5a82a1cac232164990f04874c594c9453ec55eef02eab885aa02fc17a2", 229 | "sha256:f5321fbe4bf3fefa0efd0bfe7fb14e90909eb62a48ccda331726b4319897dd5e" 230 | ], 231 | "version": "==1.25.11" 232 | } 233 | }, 234 | "develop": { 235 | "autopep8": { 236 | "hashes": [ 237 | "sha256:d21d3901cb0da6ebd1e83fc9b0dfbde8b46afc2ede4fe32fbda0c7c6118ca094" 238 | ], 239 | "index": "pypi", 240 | "version": "==1.5.4" 241 | }, 242 | "pycodestyle": { 243 | "hashes": [ 244 | "sha256:2295e7b2f6b5bd100585ebcb1f616591b652db8a741695b3d8f5d28bdc934367", 245 | "sha256:c58a7d2815e0e8d7972bf1803331fb0152f867bd89adf8a01dfd55085434192e" 246 | ], 247 | "version": "==2.6.0" 248 | }, 249 | "toml": { 250 | "hashes": [ 251 | "sha256:926b612be1e5ce0634a2ca03470f95169cf16f939018233a670519cb4ac58b0f", 252 | "sha256:bda89d5935c2eac546d648028b9901107a595863cb36bae0c73ac804a9b4ce88" 253 | ], 254 | "version": "==0.10.1" 255 | } 256 | } 257 | } 258 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django React Auth 2 | 3 | ## Overview 4 | 5 | Django React Auth is a simple user authentication demonstration that uses 6 | Django, and the Django Rest Framework on the backend, and React on the frontend. 7 | 8 | ## Dependencies 9 | 10 | 1. Python 3.5 or greater 11 | 2. Django 3.1 12 | 3. React 17.0 13 | 14 | ## Installation 15 | 16 | The project is setup using pipenv, and create-react-app. Follow these steps after 17 | cloning the repository to get it up and running. 18 | 19 | 1. Run the following commands in the same directory as `manage.py`: 20 | `pipenv install` and `pipenv install --dev` 21 | 22 | 2. Next create a `local_settings.py` file inside of the `mysite` directory. 23 | create 3 variables inside of it: 24 | 25 | - secret_key - this is equal to a string 26 | - debug - this is a boolean value (True, or False) 27 | - allowed_hosts - this is an array 28 | 29 | Note that `local_settings.py` is in the gitignore. 30 | 31 | 3. Now run the django migrations. In the same directory as `manage.py` run: 32 | `python manage.py migrate` 33 | -------------------------------------------------------------------------------- /client/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /client/README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 35 | 36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 39 | 40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | 48 | ### Code Splitting 49 | 50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 51 | 52 | ### Analyzing the Bundle Size 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 55 | 56 | ### Making a Progressive Web App 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 59 | 60 | ### Advanced Configuration 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 63 | 64 | ### Deployment 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 67 | 68 | ### `npm run build` fails to minify 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 71 | -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.6", 7 | "@testing-library/react": "^11.1.2", 8 | "@testing-library/user-event": "^12.2.2", 9 | "react": "^17.0.1", 10 | "react-dom": "^17.0.1", 11 | "react-router-dom": "^5.2.0", 12 | "react-scripts": "4.0.0", 13 | "web-vitals": "^0.2.4" 14 | }, 15 | "scripts": { 16 | "start": "react-scripts start", 17 | "build": "react-scripts build", 18 | "test": "react-scripts test", 19 | "eject": "react-scripts eject" 20 | }, 21 | "eslintConfig": { 22 | "extends": [ 23 | "react-app", 24 | "react-app/jest" 25 | ] 26 | }, 27 | "browserslist": { 28 | "production": [ 29 | ">0.2%", 30 | "not dead", 31 | "not op_mini all" 32 | ], 33 | "development": [ 34 | "last 1 chrome version", 35 | "last 1 firefox version", 36 | "last 1 safari version" 37 | ] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /client/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | Django React Auth 11 | 12 | 13 | 14 |
15 | 16 | 17 | -------------------------------------------------------------------------------- /client/src/App.js: -------------------------------------------------------------------------------- 1 | import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'; 2 | import Navbar from './components/layout/Navbar'; 3 | import Login from './views/auth/Login'; 4 | import Signup from './views/auth/Signup'; 5 | import Logout from './views/auth/Logout'; 6 | import Dashboard from './views/app/Dashboard'; 7 | 8 | const App = () => { 9 | return ( 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | ); 22 | }; 23 | 24 | export default App; 25 | -------------------------------------------------------------------------------- /client/src/components/layout/Navbar.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect, Fragment } from 'react'; 2 | import { Link } from 'react-router-dom'; 3 | 4 | const Navbar = () => { 5 | const [isAuth, setIsAuth] = useState(false); 6 | 7 | useEffect(() => { 8 | if (localStorage.getItem('token') !== null) { 9 | setIsAuth(true); 10 | } 11 | }, []); 12 | 13 | return ( 14 | 40 | ); 41 | }; 42 | 43 | export default Navbar; 44 | -------------------------------------------------------------------------------- /client/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | ReactDOM.render( 6 | 7 | 8 | , 9 | document.getElementById('root') 10 | ); 11 | -------------------------------------------------------------------------------- /client/src/views/app/Dashboard.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect, Fragment } from 'react'; 2 | 3 | const Dashboard = () => { 4 | const [userEmail, setUserEmail] = useState(''); 5 | const [loading, setLoading] = useState(true); 6 | 7 | useEffect(() => { 8 | if (localStorage.getItem('token') === null) { 9 | window.location.replace('http://localhost:3000/login'); 10 | } else { 11 | fetch('http://127.0.0.1:8000/api/v1/users/auth/user/', { 12 | method: 'GET', 13 | headers: { 14 | 'Content-Type': 'application/json', 15 | Authorization: `Token ${localStorage.getItem('token')}` 16 | } 17 | }) 18 | .then(res => res.json()) 19 | .then(data => { 20 | setUserEmail(data.email); 21 | setLoading(false); 22 | }); 23 | } 24 | }, []); 25 | 26 | return ( 27 |
28 | {loading === false && ( 29 | 30 |

Dashboard

31 |

Hello {userEmail}!

32 |
33 | )} 34 |
35 | ); 36 | }; 37 | 38 | export default Dashboard; 39 | -------------------------------------------------------------------------------- /client/src/views/auth/Login.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from 'react'; 2 | 3 | const Login = () => { 4 | const [email, setEmail] = useState(''); 5 | const [password, setPassword] = useState(''); 6 | const [errors, setErrors] = useState(false); 7 | const [loading, setLoading] = useState(true); 8 | 9 | useEffect(() => { 10 | if (localStorage.getItem('token') !== null) { 11 | window.location.replace('http://localhost:3000/dashboard'); 12 | } else { 13 | setLoading(false); 14 | } 15 | }, []); 16 | 17 | const onSubmit = e => { 18 | e.preventDefault(); 19 | 20 | const user = { 21 | email: email, 22 | password: password 23 | }; 24 | 25 | fetch('http://127.0.0.1:8000/api/v1/users/auth/login/', { 26 | method: 'POST', 27 | headers: { 28 | 'Content-Type': 'application/json' 29 | }, 30 | body: JSON.stringify(user) 31 | }) 32 | .then(res => res.json()) 33 | .then(data => { 34 | if (data.key) { 35 | localStorage.clear(); 36 | localStorage.setItem('token', data.key); 37 | window.location.replace('http://localhost:3000/dashboard'); 38 | } else { 39 | setEmail(''); 40 | setPassword(''); 41 | localStorage.clear(); 42 | setErrors(true); 43 | } 44 | }); 45 | }; 46 | 47 | return ( 48 |
49 | {loading === false &&

Login

} 50 | {errors === true &&

Cannot log in with provided credentials

} 51 | {loading === false && ( 52 |
53 |
54 | setEmail(e.target.value)} 60 | />{' '} 61 |
62 |
63 | setPassword(e.target.value)} 69 | />{' '} 70 |
71 | 72 |
73 | )} 74 |
75 | ); 76 | }; 77 | 78 | export default Login; 79 | -------------------------------------------------------------------------------- /client/src/views/auth/Logout.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect, Fragment } from 'react'; 2 | 3 | const Logout = () => { 4 | const [loading, setLoading] = useState(true); 5 | 6 | useEffect(() => { 7 | if (localStorage.getItem('token') == null) { 8 | window.location.replace('http://localhost:3000/login'); 9 | } else { 10 | setLoading(false); 11 | } 12 | }, []); 13 | 14 | const handleLogout = e => { 15 | e.preventDefault(); 16 | 17 | fetch('http://127.0.0.1:8000/api/v1/users/auth/logout/', { 18 | method: 'POST', 19 | headers: { 20 | 'Content-Type': 'application/json', 21 | Authorization: `Token ${localStorage.getItem('token')}` 22 | } 23 | }) 24 | .then(res => res.json()) 25 | .then(data => { 26 | console.log(data); 27 | localStorage.clear(); 28 | window.location.replace('http://localhost:3000/login'); 29 | }); 30 | }; 31 | 32 | return ( 33 |
34 | {loading === false && ( 35 | 36 |

Are you sure you want to logout?

37 | 38 |
39 | )} 40 |
41 | ); 42 | }; 43 | 44 | export default Logout; 45 | -------------------------------------------------------------------------------- /client/src/views/auth/Signup.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from 'react'; 2 | 3 | const Signup = () => { 4 | const [email, setEmail] = useState(''); 5 | const [password1, setPassword1] = useState(''); 6 | const [password2, setPassword2] = useState(''); 7 | const [errors, setErrors] = useState(false); 8 | const [loading, setLoading] = useState(true); 9 | 10 | useEffect(() => { 11 | if (localStorage.getItem('token') !== null) { 12 | window.location.replace('http://localhost:3000/dashboard'); 13 | } else { 14 | setLoading(false); 15 | } 16 | }, []); 17 | 18 | const onSubmit = e => { 19 | e.preventDefault(); 20 | 21 | const user = { 22 | email: email, 23 | password1: password1, 24 | password2: password2 25 | }; 26 | 27 | fetch('http://127.0.0.1:8000/api/v1/users/auth/register/', { 28 | method: 'POST', 29 | headers: { 30 | 'Content-Type': 'application/json' 31 | }, 32 | body: JSON.stringify(user) 33 | }) 34 | .then(res => res.json()) 35 | .then(data => { 36 | if (data.key) { 37 | localStorage.clear(); 38 | localStorage.setItem('token', data.key); 39 | window.location.replace('http://localhost:3000/dashboard'); 40 | } else { 41 | setEmail(''); 42 | setPassword1(''); 43 | setPassword2(''); 44 | localStorage.clear(); 45 | setErrors(true); 46 | } 47 | }); 48 | }; 49 | 50 | return ( 51 |
52 | {loading === false &&

Signup

} 53 | {errors === true &&

Cannot signup with provided credentials

} 54 |
55 |
56 | setEmail(e.target.value)} 61 | required 62 | />{' '} 63 |
64 |
65 | setPassword1(e.target.value)} 70 | required 71 | />{' '} 72 |
73 |
74 | setPassword2(e.target.value)} 79 | required 80 | />{' '} 81 |
82 | 83 |
84 |
85 | ); 86 | }; 87 | 88 | export default Signup; 89 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /mysite/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tarricsookdeo/django-react-auth/4e0b86d1410b5a8845e9a834494d92e5e6229415/mysite/__init__.py -------------------------------------------------------------------------------- /mysite/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for mysite project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /mysite/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for mysite project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.1.2. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | from . import local_settings 16 | 17 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 18 | BASE_DIR = Path(__file__).resolve().parent.parent 19 | 20 | 21 | # Quick-start development settings - unsuitable for production 22 | # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ 23 | 24 | # SECURITY WARNING: keep the secret key used in production secret! 25 | SECRET_KEY = local_settings.secret_key 26 | 27 | # SECURITY WARNING: don't run with debug turned on in production! 28 | DEBUG = local_settings.debug 29 | 30 | ALLOWED_HOSTS = local_settings.allowed_hosts 31 | 32 | 33 | # Application definition 34 | 35 | INSTALLED_APPS = [ 36 | 'django.contrib.admin', 37 | 'django.contrib.auth', 38 | 'django.contrib.contenttypes', 39 | 'django.contrib.sessions', 40 | 'django.contrib.messages', 41 | 'django.contrib.staticfiles', 42 | 43 | # 3rd party apps 44 | 'rest_framework', 45 | 'rest_framework.authtoken', 46 | 'rest_auth', 47 | 'django.contrib.sites', 48 | 'allauth', 49 | 'allauth.account', 50 | 'allauth.socialaccount', 51 | 'rest_auth.registration', 52 | 'corsheaders', 53 | 54 | # Local apps 55 | 'users', # Responsible for all actions pertaining to user model 56 | ] 57 | 58 | CORS_ALLOWED_ORIGINS = [ 59 | 'http://localhost:3000' 60 | ] 61 | 62 | 63 | MIDDLEWARE = [ 64 | 'corsheaders.middleware.CorsMiddleware', 65 | 'django.middleware.security.SecurityMiddleware', 66 | 'django.contrib.sessions.middleware.SessionMiddleware', 67 | 'django.middleware.common.CommonMiddleware', 68 | 'django.middleware.csrf.CsrfViewMiddleware', 69 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 70 | 'django.contrib.messages.middleware.MessageMiddleware', 71 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 72 | ] 73 | 74 | ROOT_URLCONF = 'mysite.urls' 75 | 76 | TEMPLATES = [ 77 | { 78 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 79 | 'DIRS': [], 80 | 'APP_DIRS': True, 81 | 'OPTIONS': { 82 | 'context_processors': [ 83 | 'django.template.context_processors.debug', 84 | 'django.template.context_processors.request', 85 | 'django.contrib.auth.context_processors.auth', 86 | 'django.contrib.messages.context_processors.messages', 87 | ], 88 | }, 89 | }, 90 | ] 91 | 92 | WSGI_APPLICATION = 'mysite.wsgi.application' 93 | 94 | 95 | # Database 96 | # https://docs.djangoproject.com/en/3.1/ref/settings/#databases 97 | 98 | DATABASES = { 99 | 'default': { 100 | 'ENGINE': 'django.db.backends.sqlite3', 101 | 'NAME': BASE_DIR / 'db.sqlite3', 102 | } 103 | } 104 | 105 | 106 | # Password validation 107 | # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators 108 | 109 | AUTH_PASSWORD_VALIDATORS = [ 110 | { 111 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 112 | }, 113 | { 114 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 115 | }, 116 | { 117 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 118 | }, 119 | { 120 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 121 | }, 122 | ] 123 | 124 | 125 | # Internationalization 126 | # https://docs.djangoproject.com/en/3.1/topics/i18n/ 127 | 128 | LANGUAGE_CODE = 'en-us' 129 | 130 | TIME_ZONE = 'America/New_York' 131 | 132 | USE_I18N = True 133 | 134 | USE_L10N = True 135 | 136 | USE_TZ = True 137 | 138 | 139 | # Static files (CSS, JavaScript, Images) 140 | # https://docs.djangoproject.com/en/3.1/howto/static-files/ 141 | 142 | STATIC_URL = '/static/' 143 | 144 | # Points to the custom user model 145 | AUTH_USER_MODEL = 'users.CustomUser' 146 | 147 | # Django All Auth config 148 | EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' 149 | 150 | AUTHENTICATION_BACKENDS = ( 151 | "django.contrib.auth.backends.ModelBackend", 152 | "allauth.account.auth_backends.AuthenticationBackend", 153 | ) 154 | 155 | SITE_ID = 1 156 | 157 | ACCOUNT_EMAIL_REQUIRED = True 158 | ACCOUNT_USERNAME_REQUIRED = False 159 | ACCOUNT_SESSION_REMEMBER = True 160 | ACCOUNT_AUTHENTICATION_METHOD = 'email' 161 | ACCOUNT_UNIQUE_EMAIL = True 162 | 163 | # Rest Framework config 164 | REST_FRAMEWORK = { 165 | 'DATETIME_FORMAT': "%m/%d/%Y %I:%M%P", 166 | 'DEFAULT_AUTHENTICATION_CLASSES': [ 167 | 'rest_framework.authentication.TokenAuthentication', 168 | ], 169 | } 170 | -------------------------------------------------------------------------------- /mysite/urls.py: -------------------------------------------------------------------------------- 1 | """mysite URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import include, path 18 | 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | path('api/v1/users/', include('users.urls')), 22 | ] 23 | -------------------------------------------------------------------------------- /mysite/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mysite project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /users/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tarricsookdeo/django-react-auth/4e0b86d1410b5a8845e9a834494d92e5e6229415/users/__init__.py -------------------------------------------------------------------------------- /users/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.contrib.auth import get_user_model 3 | from django.contrib.auth.admin import UserAdmin 4 | 5 | from .forms import CustomUserChangeForm, CustomUserCreationForm 6 | from .models import CustomUser 7 | 8 | 9 | class CustomUserAdmin(UserAdmin): 10 | add_form = CustomUserCreationForm 11 | form = CustomUserChangeForm 12 | model = CustomUser 13 | list_display = ['email'] 14 | 15 | 16 | admin.site.register(CustomUser, CustomUserAdmin) 17 | -------------------------------------------------------------------------------- /users/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class UsersConfig(AppConfig): 5 | name = 'users' 6 | -------------------------------------------------------------------------------- /users/forms.py: -------------------------------------------------------------------------------- 1 | from django import forms 2 | from django.contrib.auth.forms import UserChangeForm, UserCreationForm 3 | 4 | from .models import CustomUser 5 | 6 | 7 | class CustomUserCreationForm(UserCreationForm): 8 | class Meta: 9 | model = CustomUser 10 | fields = ('email', ) 11 | 12 | 13 | class CustomUserChangeForm(UserChangeForm): 14 | class Meta: 15 | model = CustomUser 16 | fields = UserChangeForm.Meta.fields 17 | -------------------------------------------------------------------------------- /users/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.1.2 on 2020-10-19 02:59 2 | 3 | import django.contrib.auth.models 4 | import django.contrib.auth.validators 5 | from django.db import migrations, models 6 | import django.utils.timezone 7 | 8 | 9 | class Migration(migrations.Migration): 10 | 11 | initial = True 12 | 13 | dependencies = [ 14 | ('auth', '0012_alter_user_first_name_max_length'), 15 | ] 16 | 17 | operations = [ 18 | migrations.CreateModel( 19 | name='CustomUser', 20 | fields=[ 21 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 22 | ('password', models.CharField(max_length=128, verbose_name='password')), 23 | ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), 24 | ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), 25 | ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), 26 | ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), 27 | ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), 28 | ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')), 29 | ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), 30 | ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), 31 | ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), 32 | ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), 33 | ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), 34 | ], 35 | options={ 36 | 'verbose_name': 'user', 37 | 'verbose_name_plural': 'users', 38 | 'abstract': False, 39 | }, 40 | managers=[ 41 | ('objects', django.contrib.auth.models.UserManager()), 42 | ], 43 | ), 44 | ] 45 | -------------------------------------------------------------------------------- /users/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tarricsookdeo/django-react-auth/4e0b86d1410b5a8845e9a834494d92e5e6229415/users/migrations/__init__.py -------------------------------------------------------------------------------- /users/models.py: -------------------------------------------------------------------------------- 1 | from django.contrib.auth.models import AbstractUser 2 | from django.db import models 3 | 4 | 5 | class CustomUser(AbstractUser): 6 | def __str__(self): 7 | return self.email 8 | -------------------------------------------------------------------------------- /users/serializers.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | 3 | from rest_framework.serializers import ModelSerializer, SerializerMethodField 4 | 5 | from .models import CustomUser 6 | 7 | 8 | class UserSerializer(ModelSerializer): 9 | class Meta: 10 | model = CustomUser 11 | fields = ('email', 'last_login', 'date_joined', 'is_staff') 12 | -------------------------------------------------------------------------------- /users/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /users/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import include, path 2 | 3 | from .views import UserListView 4 | 5 | urlpatterns = [ 6 | path('', UserListView.as_view()), 7 | path('auth/', include('rest_auth.urls')), 8 | path('auth/register/', include('rest_auth.registration.urls')) 9 | ] 10 | -------------------------------------------------------------------------------- /users/views.py: -------------------------------------------------------------------------------- 1 | from rest_framework.generics import ListAPIView 2 | from rest_framework.permissions import IsAdminUser 3 | 4 | from .models import CustomUser 5 | from .serializers import UserSerializer 6 | 7 | 8 | class UserListView(ListAPIView): 9 | queryset = CustomUser.objects.all() 10 | serializer_class = UserSerializer 11 | permission_classes = [IsAdminUser] 12 | --------------------------------------------------------------------------------