1

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 Jul 17 '18 at 12:25
  • 1
    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 : https://docs.djangoproject.com/fr/2.0/topics/auth/default/#authenticating-users -> 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 Jul 17 '18 at 12:30
  • Right. You can login using only username and password by default. – Sam Jul 17 '18 at 12:35
  • 1
    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 Jul 17 '18 at 12:40
  • 1
    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 Jul 17 '18 at 12:47
  • @T.Nel most of website these days use email to register and login. so what should i do to Authenticate using email ? do i have to change in default's ? if yes how do i do it ? – Neeraj Joon Jul 18 '18 at 07:36
  • @WillemVanOnsem i know that,s why i have putted it inside " try: " there – Neeraj Joon Jul 18 '18 at 07:45
  • @NeerajJoon https://stackoverflow.com/questions/37332190/django-login-with-email for authenticating on user's email. I posted an answer if your question is now solved. – T.Nel Jul 18 '18 at 09:02
  • @NeerajJoon It's generally a bad behavior not to be precise about the exception you want to catch. I recommand you to replace `except:` by `except ObjectDoesNotExist:` . You'll have to do `from django.core.exceptions import ObjectDoesNotExist` – T.Nel Jul 18 '18 at 09:17

1 Answers1

1

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.

T.Nel
  • 1,540
  • 2
  • 18
  • 34