Skip to content

Commit d9f5409

Browse files
authored
Add person existence check in login procedure (#14)
* Add login procedures with person existence check and retrieval of hashed password * Refine error messages in update_person procedure for clarity
1 parent 4f1db93 commit d9f5409

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

functions/person_exists.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE FUNCTION person_exists(
7+
IN p_person_id INT,
8+
IN p_email VARCHAR(100)
9+
) RETURNS BOOLEAN
10+
BEGIN
11+
DECLARE v_exists BOOLEAN;
12+
13+
SELECT EXISTS (
14+
SELECT 1
15+
FROM person
16+
WHERE (person_id = p_person_id OR email = p_email)
17+
) INTO v_exists;
18+
19+
RETURN v_exists;
20+
END //
21+
22+
DELIMITER ;

procedures/auth.sql

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,41 @@ BEGIN
4444
END //
4545

4646
CREATE OR REPLACE PROCEDURE login_person(
47+
IN p_person_id INT,
4748
IN p_email VARCHAR(100)
4849
)
4950
BEGIN
51+
DECLARE v_person_id INT;
52+
DECLARE v_hashed_password VARCHAR(100);
53+
DECLARE v_salt BINARY(16);
54+
5055
-- Check if the person exists
51-
IF NOT EXISTS (
52-
SELECT 1
53-
FROM person
54-
WHERE email = p_email
55-
) THEN
56+
IF NOT person_exists(p_person_id, p_email) THEN
5657
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'User not found';
5758
END IF;
5859

59-
-- Retrieve the hashed password and salt as a result set
60+
-- Retrieve the person_id, hashed password, and salt
6061
SELECT person_id, hashed_password, salt
62+
INTO v_person_id, v_hashed_password, v_salt
6163
FROM person
62-
WHERE email = p_email;
64+
WHERE (person_id = p_person_id OR email = p_email);
65+
66+
-- Return the result set
67+
SELECT v_person_id AS person_id, v_hashed_password AS hashed_password, v_salt AS salt;
68+
END //
69+
70+
CREATE OR REPLACE PROCEDURE login_person_by_id(
71+
IN p_person_id INT
72+
)
73+
BEGIN
74+
CALL login_person(p_person_id, NULL);
75+
END //
76+
77+
CREATE OR REPLACE PROCEDURE login_person_by_email(
78+
IN p_email VARCHAR(100)
79+
)
80+
BEGIN
81+
CALL login_person(NULL, p_email);
6382
END //
6483

6584
DELIMITER ;

procedures/update/person.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ BEGIN
3333
FROM person;
3434

3535
IF name_exists > 0 THEN
36-
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'User name already exists for another record';
36+
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'User name already exists';
3737
END IF;
3838

3939
IF email_exists > 0 THEN
40-
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Email already exists for another record';
40+
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Email already exists';
4141
END IF;
4242

4343
-- Retrieve the language ID if provided

0 commit comments

Comments
 (0)