File tree Expand file tree Collapse file tree 4 files changed +46
-2
lines changed
Expand file tree Collapse file tree 4 files changed +46
-2
lines changed Original file line number Diff line number Diff 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' )
Original file line number Diff line number Diff line change 55from .. import db
66from ..models import User
77from ..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 )
Original file line number Diff line number Diff line change 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 %}
Original file line number Diff line number Diff line change 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 %}
You can’t perform that action at this time.
0 commit comments