1
votes

user is not getting authenticated when i try to login.

this my register code

def register(request):
if request.method == "POST":
    username = request.POST["name"]
    email =  request.POST["email"]
    password = request.POST["password"]
    password2 = request.POST["password2"]

    try:
        if password2 != password:
            messages.error(request, "password did'nt match")
        elif User.objects.get(email=email):
            messages.error(request, "user already exists")
    except: 
        if email and username:
            user = User.objects.create_user(username=username, email=email)
            user.set_password(password)
            user.save()
            messages.success(request, "user created")
        else:
            messages.error(request, "Looks like user already exists")

return render(request, 'register.html', {})

this if my login code if i am using user.object.get for email and check_password for password it,s working but when i am using authenticate it,s not working . printing authenticate is returning None

def login(request):

    if request.method == "POST":
        email = request.POST["email"]
        password = request.POST["password"]
        print email
        print password
        try:
            user = authenticate(email=email , password=password)
            if user is not None:
                login(request, user)                    
                return redirect('dashbord')
            else:
                messages.error(request, "password yesn't match")

        except:

            messages.error(request, "login fail plz check ur password or email again")


    return render(request, 'login.html', {})
1
Your elif User.objects.get(email=email): will raise an error in case the User does not yet exists.Willem Van Onsem
Did you configure Django to authenticate over email? The doc is showing that you should authenticate with args username and password, not email and password. (Edit : docs.djangoproject.com/fr/2.0/topics/auth/default/… -> By default it's username and password, if "PermissionDenied" is raised during the authentication process, the value returned by the function is None.)T.Nel
Right. You can login using only username and password by default.sam
This comment has NOTHING to do with a solution to your problem since there is already valid things to try in the above comments, but please check the syntax (did'nt,yesn't, plz) in your error messages, this is getting out of hand.scharette
The correct contraction for yes+not is "y'ain't", e.g., "Oh hell naw, y'ain't put in the correct password."Rob L

1 Answers

1
votes

According to your comment, you are using default authentication system, but you authenticate with this line :

user = authenticate(email=email , password=password)

The fix, according to your question, is :
email = request.POST["email"]

username = request.POST["name"]
user = authenticate(username=username , password=password)

By default, django log with username, not with email (Cf : the documentation ).

For your question in the comment, it is a duplicate of Django - Login with Email. I invite you to find your solution in the accepted answer.