Login_required decorator redirecting to login page even after successful login(i made a custom user model)

Background: i made a custom user model in django and i wrote custom backend for authenticate and get_user

the views are all fine and working. when i removed login_required(), it is working very well.
but when i use login required, it is redirecting to the login page

Github link: GitHub - WarLoRDlArS/Prodvi

Login view located in Prodvi/users/views.py:

def login_page(request):
    if request.method == "POST": 
        pid = request.POST.get("pid")
        password = request.POST.get("password")

        if not Users.objects.filter(pid=pid).exists():
            messages.error(request, 'Invalid Username')
            return redirect('users:login')
        
        user = authenticate(request, pid=pid, password=password)
        if user is None:
            messages.error(request, 'Invalid Credentials')
            return redirect('users:login')
            print("User is None")
        
        login(request, user)
        print("Login Successful")
        #  Login is a success
        return redirect('home:index')

        # Test Login
        # pid: 000000
        # password: abc

    return render(request, 'users/login.html')

index view (where login_required is not working properly):
located at Prodvi/home/views.py:

from django.shortcuts import render

# Create your views here.

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse

@login_required(login_url='users:login')
def index(request):
    print("came here")
    return render(request, 'home/index.html')

Custom User Model:
located at Prodvi/users/models.py

class CustomUserManager(BaseUserManager):
    def create_user(self, pid, password=None, **extra_fields):
        if not pid:
            raise ValueError('The PID field must be set')
        user = self.model(pid=pid, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, pid, password=None, **extra_fields): 
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        return self.create_user(pid, password, **extra_fields)

class Users(AbstractUser):

    # Thois is a custom user model i made for no particular reason.
    # To make it as the User model of this project, i have added 
    #  it as AUTH_USER_MODEL = "users.Users" 
    #  in settings.py of Main project Folder
    pid = models.CharField(max_length=6, blank=False, null=False, default="000000", unique=True)
    username = models.CharField(max_length=150, blank=True, null=True, unique=False)
    objects = CustomUserManager()
    # above line sets manager for this very model.
    # A manager in Django is a class that provides methods to interact with the database for a particular model. 


    def __str__(self):
        return self.pid

Custom Backend
located at Prodvi/users/authenticate.py

class CustomBackend(BaseBackend):
    def authenticate(self, request, pid=None, password=None, **kwargs):
        print("AUthenticate was called")
        try:
            user = Users.objects.get(pid=pid)
            if user.check_password(password):
                print("AUthenticate all fine")
                return user
        except Users.DoesNotExist:
            print("Authenticate IN exception")
            return None
            
    def get_user(self, user_id):
        try:
            return Users.objects.get(pid=user_id)
        except Users.DoesNotExist:
            return None

Welcome @larsonseq !

Using the authenticate function to check a password using the check_password function does not log in a user. You need to also use the login function.

Review the docs for How to log a user in.

Found Solution::

in the authentication.py file

for CustomBackend class, for the get_user method,
use primary key, not pid as it uses primary key for checking the issue

so the entire thing will be

class CustomBackend(BaseBackend):
    def authenticate(self, request, pid=None, password=None, **kwargs):
        print("AUthenticate was called")
        try:
            user = Users.objects.get(pid=pid)
            if user.check_password(password):
                print("AUthenticate all fine")
                return user
        except Users.DoesNotExist:
            print("Authenticate IN exception")
            return None
            
    def get_user(self, user_id):
        try:
            return Users.objects.get(pk=user_id)
        except Users.DoesNotExist:
            return None

found the solution.
it was a stupid mistake. i didnt use pid as primary key at all lol

Thanks a lot!!

forgot to thank you