0

Lets say I have email of user

user = User.objects.get(email="anyone@anymail.com")

Now I want to login this user like:

user = authenticate(username=username, password=user.password)

Authenticate do not take hashed password. But here i can only get hashed password. How can I login this user lets say to change its password

Thank you

gamer
  • 5,673
  • 13
  • 58
  • 91

1 Answers1

3

You don't need to log in a user to change the password. You can use the Django's set_password() helper function to change the password.

from django.contrib.auth.models import User

user = User.objects.get(email="anyone@anymail.com")
user.set_password('new_password')
user.save() # call save explicitly

As per the docs,

set_password(raw_password)
Sets the user’s password to the given raw string, taking care of the password hashing. Doesn’t save the User object.

Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126