diff --git a/app/auth/forms.py b/app/auth/forms.py index a540495..de82c48 100644 --- a/app/auth/forms.py +++ b/app/auth/forms.py @@ -33,3 +33,12 @@ class RegistrationForm(FlaskForm): 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') diff --git a/app/auth/views.py b/app/auth/views.py index 0f1bb20..a060c52 100644 --- a/app/auth/views.py +++ b/app/auth/views.py @@ -4,7 +4,7 @@ from . import auth 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 @@ -84,3 +84,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') + redirect(url_for('main.index')) + else: + flash('Invalid password.') + return render_template('auth/change_password.html', form=form) diff --git a/app/templates/auth/change_password.html b/app/templates/auth/change_password.html new file mode 100644 index 0000000..f9e79f8 --- /dev/null +++ b/app/templates/auth/change_password.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} +{% import "bootstrap/wtf.html" as wtf %} + +{% block title %}Flasky - Change Password{% endblock %} + +{% block page_content %} + +
+ {{ wtf.quick_form(form) }} +
+{% endblock %} diff --git a/app/templates/base.html b/app/templates/base.html index 9851a28..7e58752 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -27,7 +27,13 @@