Skip to content

Commit

Permalink
更新 存储密码 小节的代码示例
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Mar 6, 2019
1 parent 4ab7dbe commit 68df8b4
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions zh/12-handling_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ class User(db.Model):
return self._password

@password.setter
def _set_password(self, plaintext):
self._password = bcrypt.generate_password_hash(plaintext)
def password(self, plaintext):
self._password = bcrypt.generate_password_hash(plaintext).decode("utf-8")
```

我们使用SQLAlchemy的hybird(混合)拓展来定义一个同时供众多函数调用的接口属性。当赋值给`user.password`属性时,我们的setter会被自动调用。而在setter内,我们会hash纯文本密码并存储在用户表里的`_password`列里。既然我们定义`user.password`为混合属性,那么就可以通过这个属性来获取`_password`的值。
Expand Down Expand Up @@ -231,7 +231,7 @@ class User(db.Model):

# [...] columns and properties

def is_correct_password(self, plaintext)
def is_correct_password(self, plaintext):
if bcrypt.check_password_hash(self._password, plaintext):
return True

Expand Down

0 comments on commit 68df8b4

Please sign in to comment.