Search code examples
pythonsqliteflaskpasswordsflask-sqlalchemy

How do I update the password?


I am working on a web app and I want a user to be able to reset their password but I'm failing to do so how can I do it?

I also tried

email = request.form.get('email')
new_password = request.form.get('password')

user = User.query.filter_by(email=email).first()
If user :
user.password = new_password. #The new_password 

Solution

  • I think below should work for your requirements

    Here you are getting the user by filtering ,you can also get the current logged in user as that will be the one you'll updating password for, and you can update password using below snippet

    from flask import Flask, session
    
    #current user using
    flask_login.current_user 
    
    #or get the user_id just like below
    user_id = session["user_id"]
    
    user = User.objects.get(id=user_id)
    user.modify(password=password)
    user.hash_password()
    user.save()