|
6 | 6 | from ..models import User
|
7 | 7 | from ..email import send_email
|
8 | 8 | from .forms import LoginForm, RegistrationForm, ChangePasswordForm,\
|
9 |
| - PasswordResetRequestForm, PasswordResetForm |
| 9 | + PasswordResetRequestForm, PasswordResetForm, ChangeEmailForm |
10 | 10 |
|
11 | 11 |
|
12 | 12 | @auth.before_app_request
|
@@ -136,3 +136,33 @@ def password_reset(token):
|
136 | 136 | else:
|
137 | 137 | return redirect(url_for('main.index'))
|
138 | 138 | return render_template('auth/reset_password.html', form=form)
|
| 139 | + |
| 140 | + |
| 141 | +@auth.route('/change_email', methods=['GET', 'POST']) |
| 142 | +@login_required |
| 143 | +def change_email_request(): |
| 144 | + form = ChangeEmailForm() |
| 145 | + if form.validate_on_submit(): |
| 146 | + if current_user.verify_password(form.password.data): |
| 147 | + new_email = form.email.data.lower() |
| 148 | + token = current_user.generate_email_change_token(new_email) |
| 149 | + send_email(new_email, 'Confirm your email address', |
| 150 | + 'auth/email/change_email', |
| 151 | + user=current_user, token=token) |
| 152 | + flash('An email with instructions to confirm your new email ' |
| 153 | + 'address has been sent to you.') |
| 154 | + return redirect(url_for('main.index')) |
| 155 | + else: |
| 156 | + flash('Invalid email or password.') |
| 157 | + return render_template("auth/change_email.html", form=form) |
| 158 | + |
| 159 | + |
| 160 | +@auth.route('/change_email/<token>') |
| 161 | +@login_required |
| 162 | +def change_email(token): |
| 163 | + if current_user.change_email(token): |
| 164 | + db.session.commit() |
| 165 | + flash('Your email address has been updated.') |
| 166 | + else: |
| 167 | + flash('Invalid request.') |
| 168 | + return redirect(url_for('main.index')) |
0 commit comments