Skip to content

Commit

Permalink
Chapter 8: Password updates (8f)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jun 9, 2019
1 parent b345154 commit 74eb936
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/auth/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ def validate_email(self, field):
def validate_username(self, field):
if User.query.filter_by(username=field.data).first():
raise ValidationError('Username already in use.')


class ChangePasswordForm(FlaskForm):
old_password = PasswordField('Old password', validators=[DataRequired()])
password = PasswordField('New password', validators=[
DataRequired(), EqualTo('password2', message='Passwords must match.')])
password2 = PasswordField('Confirm new password',
validators=[DataRequired()])
submit = SubmitField('Update Password')
18 changes: 17 additions & 1 deletion app/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .. import db
from ..models import User
from ..email import send_email
from .forms import LoginForm, RegistrationForm
from .forms import LoginForm, RegistrationForm, ChangePasswordForm


@auth.before_app_request
Expand Down Expand Up @@ -86,3 +86,19 @@ def resend_confirmation():
'auth/email/confirm', user=current_user, token=token)
flash('A new confirmation email has been sent to you by email.')
return redirect(url_for('main.index'))


@auth.route('/change-password', methods=['GET', 'POST'])
@login_required
def change_password():
form = ChangePasswordForm()
if form.validate_on_submit():
if current_user.verify_password(form.old_password.data):
current_user.password = form.password.data
db.session.add(current_user)
db.session.commit()
flash('Your password has been updated.')
return redirect(url_for('main.index'))
else:
flash('Invalid password.')
return render_template("auth/change_password.html", form=form)
13 changes: 13 additions & 0 deletions app/templates/auth/change_password.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}Flasky - Change Password{% endblock %}

{% block page_content %}
<div class="page-header">
<h1>Change Your Password</h1>
</div>
<div class="col-md-4">
{{ wtf.quick_form(form) }}
</div>
{% endblock %}
8 changes: 7 additions & 1 deletion app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
</ul>
<ul class="nav navbar-nav navbar-right">
{% if current_user.is_authenticated %}
<li><a href="{{ url_for('auth.logout') }}">Log Out</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Account <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="{{ url_for('auth.change_password') }}">Change Password</a></li>
<li><a href="{{ url_for('auth.logout') }}">Log Out</a></li>
</ul>
</li>
{% else %}
<li><a href="{{ url_for('auth.login') }}">Log In</a></li>
{% endif %}
Expand Down

0 comments on commit 74eb936

Please sign in to comment.