Skip to content

Commit b7c4701

Browse files
Chapter 8: Password updates (8f)
1 parent edb7ecb commit b7c4701

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

app/auth/forms.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ def validate_email(self, field):
3333
def validate_username(self, field):
3434
if User.query.filter_by(username=field.data).first():
3535
raise ValidationError('Username already in use.')
36+
37+
38+
class ChangePasswordForm(FlaskForm):
39+
old_password = PasswordField('Old password', validators=[DataRequired()])
40+
password = PasswordField('New password', validators=[
41+
DataRequired(), EqualTo('password2', message='Passwords must match.')])
42+
password2 = PasswordField('Confirm new password',
43+
validators=[DataRequired()])
44+
submit = SubmitField('Update Password')

app/auth/views.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .. import db
66
from ..models import User
77
from ..email import send_email
8-
from .forms import LoginForm, RegistrationForm
8+
from .forms import LoginForm, RegistrationForm, ChangePasswordForm
99

1010

1111
@auth.before_app_request
@@ -86,3 +86,19 @@ def resend_confirmation():
8686
'auth/email/confirm', user=current_user, token=token)
8787
flash('A new confirmation email has been sent to you by email.')
8888
return redirect(url_for('main.index'))
89+
90+
91+
@auth.route('/change-password', methods=['GET', 'POST'])
92+
@login_required
93+
def change_password():
94+
form = ChangePasswordForm()
95+
if form.validate_on_submit():
96+
if current_user.verify_password(form.old_password.data):
97+
current_user.password = form.password.data
98+
db.session.add(current_user)
99+
db.session.commit()
100+
flash('Your password has been updated.')
101+
return redirect(url_for('main.index'))
102+
else:
103+
flash('Invalid password.')
104+
return render_template("auth/change_password.html", form=form)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends "base.html" %}
2+
{% import "bootstrap/wtf.html" as wtf %}
3+
4+
{% block title %}Flasky - Change Password{% endblock %}
5+
6+
{% block page_content %}
7+
<div class="page-header">
8+
<h1>Change Your Password</h1>
9+
</div>
10+
<div class="col-md-4">
11+
{{ wtf.quick_form(form) }}
12+
</div>
13+
{% endblock %}

app/templates/base.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
</ul>
2727
<ul class="nav navbar-nav navbar-right">
2828
{% if current_user.is_authenticated %}
29-
<li><a href="{{ url_for('auth.logout') }}">Log Out</a></li>
29+
<li class="dropdown">
30+
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Account <b class="caret"></b></a>
31+
<ul class="dropdown-menu">
32+
<li><a href="{{ url_for('auth.change_password') }}">Change Password</a></li>
33+
<li><a href="{{ url_for('auth.logout') }}">Log Out</a></li>
34+
</ul>
35+
</li>
3036
{% else %}
3137
<li><a href="{{ url_for('auth.login') }}">Log In</a></li>
3238
{% endif %}

0 commit comments

Comments
 (0)