0

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

1
  • Try to add action="/login" attribute to form :) Commented Jun 22, 2021 at 19:43

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.