我希望Flask应用中的用户能够通过提供新电子邮件及其当前密码来更改其电子邮件。但是,当我尝试使用User.query.filter_by(password=form.password.data)
输入的密码查找用户时,它永远找不到用户。如何查询用户以便在验证密码时更改电子邮件?
@app.route('/changeemail', methods=['GET', 'POST'])
def change_email():
form = ChangeEmailForm(request.form)
if form.validate_on_submit():
user = User.query.filter_by(password=form.password.data).first()
if user and bcrypt.check_password_hash(user.password,form.password.data):
user.email = form.email.data
db.session.commit()
return redirect(url_for("user.confirmed"))
return render_template('user/changeemail.html',form=form)
class ChangeEmailForm(Form):
email = TextField('email', validators=[DataRequired()])
password = PasswordField('password', validators=[DataRequired()])
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String, unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
答案 0 :(得分:4)
存储散列密码的整点是从不存储原始密码。查询您正在编辑的用户,然后验证密码。
@app.route('/<int:id>/change-email', methods=['GET', 'POST'])
def change_email(id):
user = User.query.get_or_404(id)
if user.check_password(form.password.data):
...