Skip to content

Commit 7924541

Browse files
Update auth.py
1 parent daeffa3 commit 7924541

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

app/auth.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,41 @@ def generate_qr_code(user):
169169
except Exception as e:
170170
print(f"ERROR generating QR code: {str(e)}")
171171
raise
172+
173+
@auth_bp.route('/change-password', methods=['GET', 'POST'])
174+
@login_required
175+
def change_password():
176+
"""Change user password"""
177+
if request.method == 'POST':
178+
current_password = request.form.get('current_password')
179+
new_password = request.form.get('new_password')
180+
confirm_password = request.form.get('confirm_password')
181+
182+
# Validate current password
183+
if not current_user.check_password(current_password):
184+
flash('Current password is incorrect.', 'error')
185+
return render_template('change_password.html')
186+
187+
# Validate new password
188+
if not new_password or len(new_password) < 8:
189+
flash('New password must be at least 8 characters long.', 'error')
190+
return render_template('change_password.html')
191+
192+
if new_password != confirm_password:
193+
flash('New passwords do not match.', 'error')
194+
return render_template('change_password.html')
195+
196+
# Update password
197+
try:
198+
current_user.set_password(new_password)
199+
db.session.commit()
200+
201+
flash('Password changed successfully!', 'success')
202+
return redirect(url_for('auth.profile'))
203+
204+
except Exception as e:
205+
print(f"Error changing password: {e}")
206+
db.session.rollback()
207+
flash('Error changing password. Please try again.', 'error')
208+
209+
return render_template('change_password.html')

0 commit comments

Comments
 (0)