score:8

Accepted answer

Here's how I'd approach it: I'd use a JSON Web Token (JWT) for authentication and authorization.

You'd use your back-end to protect ALL API requests from invalid JWT's except for routes where a user won't have a token (ie, registration/log-in pages).

Here's how the flow of the application will go:

  1. A new user registers to your app with standard credentials such as email and password.
  2. Your back-end will create a new user, sign a new JWT token (usually with the user's ID). You'll probably use a third-party library to sign/verify tokens (I don't have experience in the Django community but I am sure a quick Google search will give you answers). Your back-end will send back this token. This is the only time the back-end will receive email, passwords or any other sensitive information on registration.
  3. From this point on React will only use this token for authorization. React will save this token somewhere (ie, localStorage) and send this token along with the other parts of a request to the API routes you created with your back-end. You'll send this token in the authorization headers in the request.
  4. Your back-end will validate this token using a third-party library. If it's invalid the request stops and an unauthorized error is returned. If it's valid the request continues.

This achieves the following:

  1. Your API routes are protected against unauthenticated users
  2. Each request to your API is verified for authorized users which protects anyone from requesting any part of your API.
  3. You can further solidify this by only allowing requests for users to modify their own data. For example, protect Suzy's profile from being modified by people other than herself by only allowing her token with her ID to modify her account/data.

Important Note- Your backend will never save these tokens in storage. It will verify the token on each request. Read more about JSON Web Tokens (JWT) and how it works.

score:2

Django Rest Framework has built-in token authentication and a third party package for JWT Token Auth.

If you the standard token auth would work for you, then it could be pretty simple with drf-redux-auth. If you need JWT for some reason, as suggested by Keith above, you could easily fork the above...


Related Query

More Query from same tag