66from flask import Blueprint , jsonify , request
77from pymysql import MySQLError
88
9- from db import get_db_connection
9+ from db import database_cursor
1010from jwt_helper import (
1111 TokenError ,
1212 extract_token_from_header ,
@@ -42,6 +42,25 @@ def validate_password(password):
4242 )
4343
4444
45+ def get_person_by_email (email ):
46+ with database_cursor () as cursor :
47+ cursor .callproc ("login_person" , (email ,))
48+ return cursor .fetchone ()
49+
50+
51+ def verify_password (password , stored_password , salt ):
52+ seasoned_password = password .encode ("utf-8" ) + salt + PEPPER
53+ try :
54+ return ph .verify (stored_password , seasoned_password )
55+ except exceptions .VerifyMismatchError :
56+ return False
57+
58+
59+ def update_last_login (person_id ):
60+ with database_cursor () as cursor :
61+ cursor .callproc ("update_last_login" , (person_id ,))
62+
63+
4564@authentication_blueprint .route ("/register" , methods = ["POST" ])
4665def register ():
4766 data = request .get_json ()
@@ -58,23 +77,19 @@ def register():
5877
5978 hashed_password , salt = hash_password_with_salt_and_pepper (password )
6079
61- db = get_db_connection ()
62- with db .cursor () as cursor :
63- try :
80+ try :
81+ with database_cursor () as cursor :
6482 cursor .callproc (
6583 "register_person" , (name , email , hashed_password , salt , language_code )
6684 )
67- db .commit ()
68- except MySQLError as e :
69- # Check for specific error messages in the SQL error
70- if "User name already exists" in str (e ):
71- return jsonify (message = "User name already exists" ), 400
72- elif "Email already exists" in str (e ):
73- return jsonify (message = "Email already exists" ), 400
74- else :
75- return jsonify (message = "An error occurred during registration" ), 500
76-
77- db .close ()
85+ except MySQLError as e :
86+ if "User name already exists" in str (e ):
87+ return jsonify (message = "User name already exists" ), 400
88+ elif "Email already exists" in str (e ):
89+ return jsonify (message = "Email already exists" ), 400
90+ else :
91+ return jsonify (message = "An error occurred during registration" ), 500
92+
7893 return jsonify (message = "User created successfully" ), 201
7994
8095
@@ -87,30 +102,26 @@ def login():
87102 if not email or not password :
88103 return jsonify (message = "Email and password are required" ), 400
89104
90- db = get_db_connection ()
91- with db .cursor () as cursor :
92- cursor .callproc ("login_person" , (email ,))
93- person = cursor .fetchone ()
94-
95- if not person :
96- return jsonify (message = "Invalid credentials" ), 401
105+ person = get_person_by_email (email )
97106
98- person_id = person ["person_id" ]
99- stored_password = person ["hashed_password" ]
100- salt = person ["salt" ]
101- seasoned_password = password .encode ("utf-8" ) + salt + PEPPER
102-
103- try :
104- ph .verify (stored_password , seasoned_password )
105- access_token = generate_access_token (person_id )
106- refresh_token = generate_refresh_token (person_id )
107- return jsonify (
108- message = "Login successful" ,
109- access_token = access_token ,
110- refresh_token = refresh_token ,
111- )
112- except exceptions .VerifyMismatchError :
107+ try :
108+ if not person or not verify_password (
109+ password , person ["hashed_password" ], person ["salt" ]
110+ ):
113111 return jsonify (message = "Invalid credentials" ), 401
112+ except Exception :
113+ return jsonify (message = "An internal error occurred" ), 500
114+
115+ person_id = person ["person_id" ]
116+ access_token = generate_access_token (person_id )
117+ refresh_token = generate_refresh_token (person_id )
118+ update_last_login (person_id )
119+
120+ return jsonify (
121+ message = "Login successful" ,
122+ access_token = access_token ,
123+ refresh_token = refresh_token ,
124+ )
114125
115126
116127@authentication_blueprint .route ("/refresh" , methods = ["POST" ])
0 commit comments