def reset_with_token(token): try: password_reset_serializer = URLSafeTimedSerializer( app.config['SECRET_KEY']) email = password_reset_serializer.loads(token, salt='password-reset-salt', max_age=3600) except: flash('The password reset link is invalid or has expired.', 'error') return redirect(url_for('users.login')) form = PasswordForm() if form.validate_on_submit(): try: user = User.query.filter_by(email=email).first_or_404() except: flash('Invalid email address!', 'error') return redirect(url_for('users.login')) user.password = form.password.data db.session.add(user) db.session.commit() flash('Your password has been updated!', 'success') return redirect(url_for('users.login')) return render_template('reset_password_with_token.html', form=form, token=token)
def password_edit(request): user = request.user email = user.email player = Player.objects.get(user=user) if request.method == 'POST': form = PasswordForm(request.POST) if form.is_valid(): # print form.cleaned_data password = form.cleaned_data['password'] user.set_password() user.save() player.save() return HttpResponseRedirect('/') else: player = Player.objects.get(user=user) form = PasswordForm()
def reset_with_token(token): try: password_reset_serializer = URLSafeTimedSerializer( DevConfig.SECRET_KEY) email = password_reset_serializer.loads(token, salt='password-reset-salt', max_age=3600) except RuntimeError: message_body = 'The password reset link is invalid or has expired.' message_title = 'Error!' return render_template('message.html', message_title=message_title, message_body=message_body) form = PasswordForm() if form.validate_on_submit(): try: user = User.query.filter_by(email=email).first_or_404() except ValueError: message_body = 'Invalid email address!' message_title = 'Error!' return render_template('message.html', message_title=message_title, message_body=message_body) user.password_hash = generate_password_hash(form.password.data) db.session.add(user) db.session.commit() flash('Your password has been updated!', 'success') return redirect(url_for('library.login')) return render_template('reset_password_with_token.html', form=form, token=token, error=form.errors)