Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Secure authentication between ReactJS and Django

Been reading and watching quite a bit, and asking a lot of questions regarding ReactJS and Django.

This particularly helped me to understand the the flow of data from Django REST Framework to ReactJS and from ReactJS to Django REST Framework.

Django Forms and Authentication with Front-end Framework (AngularJS/ReactJS)

However, the one thing I am trying to understand is authentication to the Django REST Framework. I understand from the documentation that it has built in authentication. Since this is sensitive data, I would obviously want it protected people retrieving it just by going to http://www.my_site.com/info/api.

I would need to setup ReactJS to be the only thing that can request data from the API whether that is through a key or username/password credentials. I am just curious how this is handled? Obviously I don't want that hard coded in ReactJS because it will compile with the rest of ReactJS.

like image 423
cjones Avatar asked Feb 03 '17 00:02

cjones


People also ask

Can React and Django be used together?

Setting up our React app to integrate with Django We'll use Bootstrap with React for styling with the powerful reactstrap package. We'll also use the promise-based HTTP client Axios to make HTTP request calls to our Django API.

Does Django use JWT?

A JSON Web Token authentication plugin for the Django REST Framework. Simple JWT provides a JSON Web Token authentication backend for the Django REST Framework. It aims to cover the most common use cases of JWTs by offering a conservative set of default features.

How does Django handle user authentication?

The Django authentication system handles both authentication and authorization. Briefly, authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do. Here the term authentication is used to refer to both tasks.


2 Answers

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.

like image 140
KA01 Avatar answered Sep 28 '22 07:09

KA01


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...

like image 45
jamstooks Avatar answered Sep 28 '22 06:09

jamstooks