Post

Analyzing Stock data Using Django and React [Part 1]

Django

Github : Potato

1. Understand how the src/components/auth/sign-in-form.tsx components is dsigned

  • The sign-in-form component handles its own state internally using react-hook-form, which means I don’t need to pass down email, password, onEmailChange and OnPasswordChange as props from page.tsx

Django Packages

Github : KRX

1. Custom Authentication with Email or Username

  • A custom login function was implemented to allow users to log in using either a username or email along with a password

  • authenticate() function defaults to username, so to enable email authentication, an EmailBackend was implemented. (stockapp/backends.py)

    The settings.py file was updated to include EmailBackend in AUTHENTICATION_BACKENDS.

  • In the CustomTokenCreateSerializer, the code distinguishes between email and username using '@' in the string

2. Django REST Framework Setup for Token Authentication

  • The login endpoint /auth/token/login/ was configured to return a token upon valid credentials

  • To ensure that the login endpoint works without prior authentication, the permission_classes for the view were set to AllowAny

    1
    2
    3
    4
    
       from rest_framework.permissions import AllowAny
       class CustomTokenCreateView(APIView):
           permission_classes = [AllowAny] 
           ...
    
  • Token authentication was set as the default authentication method in settings.py

3. Testing and Debugging

  • curl was used to test the login API, sending POST requests with either a username or email and password

  • The error message "Authentication credentials were not provided" was resolved by ensuring that the login endpoint does not require authentication

This post is licensed under CC BY 4.0 by the author.