Password not getting encrypted when I update

戏子无情 提交于 2019-12-04 22:01:43
Michael Merickel

The issue here is the way that you are updating the password via your User.update method. This method is skipping the ORM entirely and updating the row directly in the database. It should be obvious that the code to hash the password will not run when you do this. The User model that you pasted is just fine and similar to what I use. You need to use it though. This means that to update a password you should load the user, and set their password.

user = DBSession.query(User).filter_by(email=email).first()
if user:
    user.password = new_password

and later when the transaction is committed things will be the way you expect.

Marboni

You should store password hash in database, so field of your model must contain hash value, not raw password. To set password, you should use methods, that makes hashing and set hash to instance. To check, if password is correct, you should hash user-defined password and compare result with hash stored in your instance. Yo will not be able to decode password from hash - it's unsecure.

class User(Base):
    __tablename__ = 'user'

    email = Column('email', String(80))
    password = Column('password', String(80))

    def set_password(raw_password):
        self.password = hash(raw_password)

    def check_password(raw_password):
        return self.password == hash(raw_password)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!