[Answered ]-Failed to authenticate multiple user in django

1👍

the issue i’m facing is related to how i’m checking the user’s role and trying to redirect them after authentication. In my code, i’m checking user.Role.REALTOR and user.Role.OLAROOM, but this is not the correct way to check the user’s role based on my models.

Instead, I should check the role attribute of the user instance to determine their role and redirect accordingly.

def login(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.role == User.Role.REALTOR:
                    auth_login(request, user)
                    return redirect('Realtor_Dashboard')
                elif user.role == User.Role.OLAROOM:
                    auth_login(request, user)
                    return redirect('Rooms')
                else:
                    messages.error(request, 'Invalid Credentials')
    else:
        form = AuthenticationForm()        
    return render(request, 'Login/login.html', {'form': form})

In this updated code, Instead of using user.Role.REALTOR or user.Role.OLAROOM, I’m using user.role == User.Role.REALTOR and user.role == User.Role.OLAROOM which means: If the user’s role is REALTOR, it logs in the user using auth_login and redirects to a Realtor_Dashboard page. If the user role is OLAROOM, it logs in the user and redirects to a Rooms page.

👤Bubble

Leave a comment