简体   繁体   中英

django authentication with hashed password or convert it to plain

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM