我目前正在通過Miguel Grinberg的書學習Flask。如果你熟悉你可能知道我目前在第8 Flasky(米格爾書過程中使用的應用程序)通過其危險標記獲取用戶信息
,處理密碼重置,這裏的原代碼(你也可以找到它的回購。它的標籤8G):
models.py
class User(UserMixin, db.Model):
__tablename__ = 'users'
...
def generate_reset_token(self, expiration=3600):
s = Serializer(current_app.config['SECRET_KEY'], expiration)
return s.dumps({'reset': self.id})
def reset_password(self, token, new_password):
s = Serializer(current_app.config['SECRET_KEY'])
try:
data = s.loads(token)
except:
return False
if data.get('reset') != self.id:
return False
self.password = new_password
db.session.add(self)
return True
認證/ views.py
@auth.route('/reset/<token>', methods=['GET', 'POST'])
def password_reset(token):
if not current_user.is_anonymous:
return redirect(url_for('main.index'))
form = PasswordResetForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user is None:
return redirect(url_for('main.index'))
if user.reset_password(token, form.password.data):
flash('Your password has been updated.')
return redirect(url_for('auth.login'))
else:
return redirect(url_for('main.index'))
return render_template('auth/reset_password.html', form=form)
認證/ forms.py
class PasswordResetForm(Form):
email = StringField('Email', validators=[Required(), Length(1, 64),
Email()])
password = PasswordField('New Password', validators=[
Required(), EqualTo('password2', message='Passwords must match')])
password2 = PasswordField('Confirm password', validators=[Required()])
submit = SubmitField('Reset Password')
def validate_email(self, field):
if User.query.filter_by(email=field.data).first() is None:
raise ValidationError('Unknown email address.')
我的問題:
我不想再次要求他們的電子郵件用戶,因爲它們通過所收到的電子郵件更改密碼。有沒有辦法從該令牌獲取用戶或用戶的電子郵件?
好吧,現在你提到它,這是真的!我沒有想過它 –
我改變了整個問題,你有什麼線索怎麼能做到這一點? –