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,293
  • 12
  • 52
  • 86

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
  • 43,515
  • 10
  • 102
  • 118