Dhruv singhal
Dhruv singhal

Reputation: 61

how to use default django auth model in react , i want to access api that needs authentication in react?

i am using default django auth User model for authentication purposes like registering a new user, login a user and logout a user and now i want to access the authentication required API's in React so how to achieve that if that's not possible what should i do? i am using Django Rest Framework

my urls.py

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from authentication import views as authentication_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('liveclass_api.urls')),
    path('login/', auth_views.LoginView.as_view(template_name='authentication/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='authentication/logout.html'), name='logout'),
    path('register/', include('authentication.urls')),
  

my login.html

**

<div class="content-section">
  <form method="POST">
    {% csrf_token %}
    <fieldset class="form-group">
      <legend class="border-bottom mb-4">Login</legend>
      {{form}}
    </fieldset>
    <div class="form-group">
      <button class="btn btn-outline-info pt-3" type="submit">Login</button>
    </div>
  </form>
  <div class='border-top pt-3'>
      <small class='text-muted'>
      Want to get an account? 
      <a class='ml-2' href={% url 'register' %}>Sign up now</a>
    </small>
  </div>
</div>

I am writing register or logput template as they are quite same, i want to modify this authentication or will have to use a new one to work with react

Upvotes: 0

Views: 777

Answers (1)

Hussain Pettiwala
Hussain Pettiwala

Reputation: 1684

I suggest you use Token Authentication How it works is every user is given a Token (an alpha-numeric string).

When an API request comes from that user the user's Token is send as a HTTP Header.

The user's Token can be stored in some form of localstorage. You can find how to get started by Token Authentication step-by-step in Django Rest Framework's docs here.

Upvotes: 2

Related Questions