-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.py
More file actions
102 lines (80 loc) · 3.45 KB
/
auth.py
File metadata and controls
102 lines (80 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from flask import Blueprint, render_template, redirect, url_for, request, flash
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import login_user, logout_user, login_required, current_user
from models import db, User, Attempt
auth = Blueprint('auth', __name__)
@auth.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
user = User.query.filter_by(email=email).first()
if user and check_password_hash(user.password_hash, password):
login_user(user, remember=True)
return redirect(url_for('home'))
else:
flash('Invalid email or password.', 'error')
return render_template('auth/login.html')
@auth.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
email = request.form.get('email')
username = request.form.get('username')
password = request.form.get('password')
# Check if user already exists
user_exists = User.query.filter_by(email=email).first()
if user_exists:
flash('Email already registered.', 'error')
return redirect(url_for('auth.register'))
# Create new user
new_user = User(
email=email,
username=username,
password_hash=generate_password_hash(password, method='pbkdf2:sha256')
)
db.session.add(new_user)
db.session.commit()
login_user(new_user, remember=True)
return redirect(url_for('home'))
return render_template('auth/register.html')
@auth.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('home'))
@auth.route('/profile')
@login_required
def profile():
# Fetch all attempts for the current logged-in user
user_attempts = Attempt.query.filter_by(user_id=current_user.id).all()
# Calculate successful catches and failed clicks
successful_catches = sum(1 for attempt in user_attempts if attempt.success_bool)
failed_clicks = sum(1 for attempt in user_attempts if not attempt.success_bool)
# Pass the calculated stats to the profile template
return render_template('auth/profile.html',
successful_catches=successful_catches,
failed_clicks=failed_clicks)
# Add this to the bottom of auth.py
@auth.route('/change-password', methods=['POST'])
@login_required
def change_password():
current_password = request.form.get('current_password')
new_password = request.form.get('new_password')
if not check_password_hash(current_user.password_hash, current_password):
flash('Incorrect current password.', 'error')
return redirect(url_for('auth.profile'))
# Hash the new password and update
current_user.password_hash = generate_password_hash(new_password, method='pbkdf2:sha256')
db.session.commit()
flash('Password updated successfully.', 'success')
return redirect(url_for('auth.profile'))
@auth.route('/reset-score', methods=['POST'])
@login_required
def reset_score():
# Reset the user's score
current_user.score = 0
# Delete all previous attempts to clear the history
Attempt.query.filter_by(user_id=current_user.id).delete()
db.session.commit()
flash('Score and history reset successfully.', 'success')
return redirect(url_for('auth.profile'))