|
5 | 5 | from app import db |
6 | 6 | from ..models import User |
7 | 7 | from ..email import send_email |
8 | | -from .forms import LoginForm, RegistrationForm |
| 8 | +from .forms import * |
9 | 9 |
|
10 | 10 | @auth.before_app_request |
11 | 11 | def before_request(): |
@@ -74,3 +74,82 @@ def unconfirmed(): |
74 | 74 | if current_user.is_anonymous() or current_user.confirmed: |
75 | 75 | return redirect(url_for('main.index')) |
76 | 76 | return render_template('auth/unconfirmed.html') |
| 77 | + |
| 78 | +@auth.route('/change-password', methods=['GET', 'POST']) |
| 79 | +@login_required |
| 80 | +def change_password(): |
| 81 | + form = ChangePasswordForm() |
| 82 | + if form.validate_on_submit(): |
| 83 | + if current_user.verify_password(form.old_password.data): |
| 84 | + current_user.password = form.password.data |
| 85 | + db.session.add(current_user) |
| 86 | + db.session.commit() |
| 87 | + flash('Your password has been updated.') |
| 88 | + return redirect(url_for('main.index')) |
| 89 | + else: |
| 90 | + flash('Invalid password.') |
| 91 | + return render_template("auth/change_password.html", form=form) |
| 92 | + |
| 93 | +@auth.route('/reset', methods=['GET', 'POST']) |
| 94 | +def password_reset_request(): |
| 95 | + if not current_user.is_anonymous: |
| 96 | + return redirect(url_for('main.index')) |
| 97 | + form = PasswordResetRequestForm() |
| 98 | + if form.validate_on_submit(): |
| 99 | + user = User.query.filter_by(email=form.email.data).first() |
| 100 | + if user: |
| 101 | + token = user.generate_reset_token() |
| 102 | + send_email(user.email, 'Reset Your Password', |
| 103 | + 'auth/email/reset_password', |
| 104 | + user=user, token=token, |
| 105 | + next=request.args.get('next')) |
| 106 | + flash('An email with instructions to reset your password has been ' |
| 107 | + 'sent to you.') |
| 108 | + return redirect(url_for('auth.login')) |
| 109 | + return render_template('auth/reset_password.html', form=form) |
| 110 | + |
| 111 | + |
| 112 | +@auth.route('/reset/<token>', methods=['GET', 'POST']) |
| 113 | +def password_reset(token): |
| 114 | + if not current_user.is_anonymous: |
| 115 | + return redirect(url_for('main.index')) |
| 116 | + form = PasswordResetForm() |
| 117 | + if form.validate_on_submit(): |
| 118 | + user = User.query.filter_by(email=form.email.data).first() |
| 119 | + if user is None: |
| 120 | + return redirect(url_for('main.index')) |
| 121 | + if user.reset_password(token, form.password.data): |
| 122 | + flash('Your password has been updated.') |
| 123 | + return redirect(url_for('auth.login')) |
| 124 | + else: |
| 125 | + return redirect(url_for('main.index')) |
| 126 | + return render_template('auth/reset_password.html', form=form) |
| 127 | + |
| 128 | + |
| 129 | +@auth.route('/change-email', methods=['GET', 'POST']) |
| 130 | +@login_required |
| 131 | +def change_email_request(): |
| 132 | + form = ChangeEmailForm() |
| 133 | + if form.validate_on_submit(): |
| 134 | + if current_user.verify_password(form.password.data): |
| 135 | + new_email = form.email.data |
| 136 | + token = current_user.generate_email_change_token(new_email) |
| 137 | + send_email(new_email, 'Confirm your email address', |
| 138 | + 'auth/email/change_email', |
| 139 | + user=current_user, token=token) |
| 140 | + flash('An email with instructions to confirm your new email ' |
| 141 | + 'address has been sent to you.') |
| 142 | + return redirect(url_for('main.index')) |
| 143 | + else: |
| 144 | + flash('Invalid email or password.') |
| 145 | + return render_template("auth/change_email.html", form=form) |
| 146 | + |
| 147 | + |
| 148 | +@auth.route('/change-email/<token>') |
| 149 | +@login_required |
| 150 | +def change_email(token): |
| 151 | + if current_user.change_email(token): |
| 152 | + flash('Your email address has been updated.') |
| 153 | + else: |
| 154 | + flash('Invalid request.') |
| 155 | + return redirect(url_for('main.index')) |
0 commit comments