The check_password_hash of flask cannot match the same

I use flask

from werkzeug.security import generate_password_hash, check_password_hash

encrypt the password registered by the user, and perform the password hash operation in the model User

class User(Base):
    id = Column(Integer, primary_key=True)
    nickname = Column(String(24),nullable=False)
    phone_number = Column(String(18),unique=True)
    _password = Column("password",String(64))
    email = Column(String(50),unique=True,nullable=True)
    confirmed = Column(Boolean,default=False)
    beans = Column(Float,default=0)
    send_counter = Column(Integer,default=0)
    receive_counter = Column(Integer,default=0)
    wx_open_id = Column(String(50))
    wx_name = Column(String(32))

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self,raw):
        self._password = generate_password_hash(raw)

    def check_password(self, raw):
        return check_password_hash(self._password, raw)

this is my login and register view functions

@web.route("/register",methods=["GET","POST"])
def register():
    form = RegisterForm(request.form)
    if request.method == "POST" and form.validate():
        user = User(nickname = request.form["nickname"],
                    email = request.form["email"],
                    _password = request.form["password"]
        )
        user.set_attrs(form.data)
        -sharp user.password = generate_password_hash(form.password.data)
        db.session.add(user)
        db.session.commit()
        return redirect(url_for("web.login"))
    return render_template("auth/register.html",form=form)

@web.route("/login",methods=["GET","POST"])
def login():
    form = LoginForm(request.form)
    if request.method == "POST" and form.validate():
        user = User.query.filter_by(email=form.email.data).first()
        if user and user.check_password(form.password.data):    -sharp hash
            print("XXX")
        else:
            flash("")
    return render_template("auth/login.html",form=form)

I failed to log in with the registered account password. I tracked the specific parameters at my breakpoint. Here are the specific tracking results

form.email.data = {str} "ngd001@qq.com"
form.password.data = {str} "wen001"
user._password = {str} "pbkdf2:sha256:50000$2PYfV5uq$a5874b931603e0a2325b3b1bd4ab0b9f32f"

this means that I use the generate_password_hash string wen001 and the string pbkdf2:sha256:50000$2PYfV5uq$a5874b931603e0a2325b3b1bd4ab0b9f32f of the savings database cannot match the wen001 entered by my user login. What"s wrong with me? This question has been bothering me for several days, and my hair has fallen out

.
Mar.11,2022

change the User in your models.py to the following:

...
...
...
@property
    def password(self):
        return self._password

    @password.setter
    def password(self,password):
        self._password = generate_password_hash(password)

    def check_password(self, raw):
        return check_password_hash(self._password, password)
...
...
...        

that is, change all the raw parameters to password.


I think the length of your _ password = Column ('password',String (64)) is too short of 64, truncating the hash string, and there is no complete storage.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c114-2b82e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c114-2b82e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?