Django authentification dosn't work

Hi everyone,

I am currently in the middle of creating a django application, with a custom user model and a custom backend , the issue commes when i try to login a user.

the authetification function :

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.views import LoginView
from .models import *
from .forms import *
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import get_user_model
from django.contrib.auth.views import LoginView

User = get_user_model()


@login_required
def create_ticket(request):
    if request.method == 'POST':
        form = TicketForm(request.POST, request.FILES)
        if form.is_valid():
            ticket = form.save(commit=False)
            ticket.created_by = request.user.personnel
            ticket.save()
            return redirect('ticket_list')
    else:
        form = TicketForm()
    return render(request, 'myapp/create_ticket.html', {'form': form})


@login_required
def ticket_list(request):
    tickets = Ticket.objects.all()
    return render(request, 'myapp/ticket_list.html', {'tickets': tickets})


@login_required
def assign_ticket(request, ticket_id):
    ticket = Ticket.objects.get(id=ticket_id)
    if request.method == 'POST':
        technician_id = request.POST.get('technician')
        ticket.assigned_to_id = technician_id
        ticket.status = 'in_progress'
        ticket.save()
        return redirect('ticket_list')
    technicians = Technician.objects.all()
    return render(request, 'myapp/assign_ticket.html', {'ticket': ticket, 'technicians': technicians})


@login_required
def delete_ticket(request, ticket_id):
    ticket = Ticket.objects.get(id=ticket_id)
    ticket.delete()

class LView(LoginView):
    authentication_form = LoginForm
    template_name = 'registration/login.html'

def login_form(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)

        if form.is_valid():
            user = authenticate(username=form.cleaned_data["email"], password=form.cleaned_data["password"])
            if user is not None:
                login(request, user)
            else:
                return HttpResponse(user)
        else:
            return HttpResponse(form.is_valid())
    else:
        return render(request,"registration/login.html",{'form': LoginForm})

always returns none even if the user existes in the database

after a bit of testing , i found that the error commes from the authentification function defined in my custom backend

from django.contrib.auth import get_user_model

User = get_user_model()

class EmailBackend(object):

    def authenticate(self,username, password, **kwargs):

        try:
            user = User.objects.get(email=username)
        except User.MultipleObjectsReturned:
            user =User.objects.get(email=username).order_by('id').first()
        except User.DoesNotExist:
            return None
        if getattr(user, 'is_active') and user.check_password(password):
            return user
    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

for some reason always sets the parameters as None , does any one now the reason for this behavior and how to solve it

Welcome @codinghaytam !

Side note: When posting code (or templates, error messages, tracebacks, etc), enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your original post. Please remember to do this in the future.)

I think you missing the request parameter as Django docs states here: Using the Django authentication system | Django documentation | Django

Try this:

class EmailBackend(object):

    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            # Try to get the user with the given email address
            user = User.objects.get(email=username)
        except User.MultipleObjectsReturned:
            # If multiple users are found with the same email, get the first one
            user = User.objects.filter(email=username).order_by('id').first()
        except User.DoesNotExist:
            # If no user is found with the given email address, return None
            return None

        # Check if the user is active and the password is correct
        if getattr(user, 'is_active') and user.check_password(password):
            return user

        # If authentication fails, return None
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None