Skip to content

Commit abc3613

Browse files
author
Matheus Galvao
committed
Checking username and password while doing login
1 parent 229f295 commit abc3613

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

app/models/user.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
from werkzeug.security import generate_password_hash, check_password_hash
2+
13
class User:
24
def __init__(self, username, password):
35
self.username = username
4-
self.password = password
6+
self.password_hash = generate_password_hash(password)
7+
8+
def check_password(self, password):
9+
return check_password_hash(self.password_hash, password)
510

611
def __repr__(self):
712
return f"User(username={self.username})"

app/services/auth_service.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def login_user(data):
7070
if not data or "username" not in data or "password" not in data:
7171
return jsonify({"error": "Username and password are required"}), 400
7272

73+
# Find user and validate credentials
74+
user = next((user for user in users if user.username == data["username"]), None)
75+
if not user or not user.check_password(data["password"]):
76+
return jsonify({"error": "Invalid username or password"}), 401
77+
7378
if auth_config.auth_method == AuthMethod.JWT:
7479
access_token, refresh_token = generate_jwt_token(data["username"])
7580
return jsonify({

0 commit comments

Comments
 (0)