From a94996d5634f65902b1fa9ae55a8e180afb33576 Mon Sep 17 00:00:00 2001 From: nnayk <79224511+nnayk@users.noreply.github.com> Date: Wed, 29 Nov 2023 17:37:37 -0800 Subject: [PATCH 1/6] Add dummy get user endpoint in DB layer --- Backend/backend.py | 9 ++++++--- Backend/db_access.py | 18 +++++++++--------- Frontend/src/pages/register.js | 6 +++++- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Backend/backend.py b/Backend/backend.py index ca78098..50fceb9 100644 --- a/Backend/backend.py +++ b/Backend/backend.py @@ -82,7 +82,6 @@ def login(): try: # Authenticate the user user = User.objects.get(username=data["username"]) - # Verify password (assuming passwords are hashed before storing) if check_password_hash(user.encrypted_password, data["password"]): # Generate session key/token @@ -177,6 +176,9 @@ def register(): "password": hashed_password, } + d = requests.get(f"{DB_ACCESS_URL}/users", json=user_data) + print(f"d={d}") + # Send the user data with the hashed password to the database access layer response = requests.post(f"{DB_ACCESS_URL}/create_user", json=user_data) @@ -186,8 +188,9 @@ def register(): return jsonify({"message": "User logged successfully!"}) elif response.status_code == 400: print("Duplicate username, please choose another") - return jsonify( - {"message": "Duplicate username, please choose another!"} + return ( + jsonify({"message": "Duplicate username, please choose another!"}), + 400, ) else: print("Failed to create user!") diff --git a/Backend/db_access.py b/Backend/db_access.py index f78cfd7..952b316 100644 --- a/Backend/db_access.py +++ b/Backend/db_access.py @@ -126,18 +126,18 @@ def create_image(): ) -@app.route("/users") +@app.route("/users", methods=["GET"]) def get_users(): - users = User.objects.all() - return jsonify( - [ + data = json.loads(request.data.decode("utf-8")) + print(f"data = {data}") + return ( + jsonify( { - "username": user.username, - "name": user.name, - "user_id": str(user.id), + "error": """Username or email already exists. + Choose another.""" } - for user in users - ] + ), + 400, ) diff --git a/Frontend/src/pages/register.js b/Frontend/src/pages/register.js index e197ebf..a99096d 100644 --- a/Frontend/src/pages/register.js +++ b/Frontend/src/pages/register.js @@ -91,9 +91,13 @@ const Register = () => { try { console.log("try"); const response = await axios.post( - "http://localhost:5000/create_user", + "http://localhost:5000/register", formData ); + console.log(`response = ${response}`); + if (response.status >= 400) { + console.log(`bad something, response=${response}`); + } console.log("response", response); router.push("/portfolio"); return response; From 9affee7fb34cb64fe7419e86660090f0241b3ba1 Mon Sep 17 00:00:00 2001 From: nnayk <79224511+nnayk@users.noreply.github.com> Date: Wed, 29 Nov 2023 17:53:54 -0800 Subject: [PATCH 2/6] Add end-to-end unique username + email check (need to refactor backend + DB layer mock endpoints) --- Backend/backend.py | 4 ++++ Backend/db_access.py | 2 +- Frontend/src/pages/register.js | 13 +++++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Backend/backend.py b/Backend/backend.py index 50fceb9..dc5ac76 100644 --- a/Backend/backend.py +++ b/Backend/backend.py @@ -184,6 +184,10 @@ def register(): # Handle the response from the database access layer if response.status_code == 201: + return ( + jsonify({"message": "Duplicate username, please choose another!"}), + 400, + ) print("User created successfully!") return jsonify({"message": "User logged successfully!"}) elif response.status_code == 400: diff --git a/Backend/db_access.py b/Backend/db_access.py index 952b316..18de19a 100644 --- a/Backend/db_access.py +++ b/Backend/db_access.py @@ -137,7 +137,7 @@ def get_users(): Choose another.""" } ), - 400, + 401, ) diff --git a/Frontend/src/pages/register.js b/Frontend/src/pages/register.js index a99096d..9e80882 100644 --- a/Frontend/src/pages/register.js +++ b/Frontend/src/pages/register.js @@ -94,14 +94,19 @@ const Register = () => { "http://localhost:5000/register", formData ); - console.log(`response = ${response}`); - if (response.status >= 400) { - console.log(`bad something, response=${response}`); - } + console.log(`responsey = ${response}`); console.log("response", response); router.push("/portfolio"); return response; } catch (error) { + console.log("err", error); + if (error != null) { + if (error.response.data.message.toLowerCase().includes("username")) { + setUsernameError("Username already taken."); + } else { + setEmailError("Email already taken."); + } + } console.log(error); return false; } From 4214014a7d84cead1f3280e35e78af01277b357f Mon Sep 17 00:00:00 2001 From: nnayk <79224511+nnayk@users.noreply.github.com> Date: Wed, 29 Nov 2023 18:00:21 -0800 Subject: [PATCH 3/6] Update create_user exception handling --- Backend/db_access.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Backend/db_access.py b/Backend/db_access.py index 18de19a..f7ed6ba 100644 --- a/Backend/db_access.py +++ b/Backend/db_access.py @@ -92,15 +92,14 @@ def create_user(): ), 201, ) - except NotUniqueError: + except: return ( jsonify( { - "error": """Username or email already exists. - Choose another.""" + "message": "Internal server error", } ), - 400, + 500, ) From 2bf9cb9872765ec35234bc6b38c0d1b0c6c168ae Mon Sep 17 00:00:00 2001 From: nnayk <79224511+nnayk@users.noreply.github.com> Date: Wed, 29 Nov 2023 18:00:47 -0800 Subject: [PATCH 4/6] Update backend error response --- Backend/backend.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/Backend/backend.py b/Backend/backend.py index dc5ac76..9a22a08 100644 --- a/Backend/backend.py +++ b/Backend/backend.py @@ -175,25 +175,16 @@ def register(): "email": email, "password": hashed_password, } - - d = requests.get(f"{DB_ACCESS_URL}/users", json=user_data) - print(f"d={d}") - # Send the user data with the hashed password to the database access layer response = requests.post(f"{DB_ACCESS_URL}/create_user", json=user_data) # Handle the response from the database access layer if response.status_code == 201: - return ( - jsonify({"message": "Duplicate username, please choose another!"}), - 400, - ) print("User created successfully!") return jsonify({"message": "User logged successfully!"}) elif response.status_code == 400: - print("Duplicate username, please choose another") return ( - jsonify({"message": "Duplicate username, please choose another!"}), + jsonify({"message": response.message}), 400, ) else: From 3e3a11926ee92705dc48565151718d9db8d56319 Mon Sep 17 00:00:00 2001 From: nnayk <79224511+nnayk@users.noreply.github.com> Date: Wed, 29 Nov 2023 18:16:21 -0800 Subject: [PATCH 5/6] Add username+email unique checking + integrate w/backend and frontend --- Backend/backend.py | 15 +++++++------- Backend/db_access.py | 37 +++++++++++++++++++++++++--------- Frontend/src/pages/register.js | 9 +++++---- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Backend/backend.py b/Backend/backend.py index 9a22a08..b9888d4 100644 --- a/Backend/backend.py +++ b/Backend/backend.py @@ -175,21 +175,22 @@ def register(): "email": email, "password": hashed_password, } + response = requests.get(f"{DB_ACCESS_URL}/users", json=user_data) + if response.status_code == 401: + print(f"responsey={response.text}") + return ( + jsonify({"message": response.text}), + 400, + ) # Send the user data with the hashed password to the database access layer response = requests.post(f"{DB_ACCESS_URL}/create_user", json=user_data) - # Handle the response from the database access layer if response.status_code == 201: print("User created successfully!") return jsonify({"message": "User logged successfully!"}) - elif response.status_code == 400: - return ( - jsonify({"message": response.message}), - 400, - ) else: print("Failed to create user!") - return jsonify({"message": "Failed to create user!!"}) + return jsonify({"message": "Failed to create user!!"}), 500 if __name__ == "__main__": diff --git a/Backend/db_access.py b/Backend/db_access.py index f7ed6ba..d3da012 100644 --- a/Backend/db_access.py +++ b/Backend/db_access.py @@ -128,16 +128,35 @@ def create_image(): @app.route("/users", methods=["GET"]) def get_users(): data = json.loads(request.data.decode("utf-8")) - print(f"data = {data}") - return ( - jsonify( - { - "error": """Username or email already exists. + user = User.objects(username=data["username"]).first() + print(f"user={user}") + if user: + return ( + jsonify( + { + "error": """Username already exists. Choose another.""" - } - ), - 401, - ) + } + ), + 401, + ) + user = User.objects(email=data["email"]).first() + print(f"user={user}") + if user: + return ( + jsonify( + { + "error": """Email already exists. + Choose another.""" + } + ), + 401, + ) + if user is None: + return ( + jsonify({"error": """User credentials are unique"""}), + 200, + ) @app.route("/images") diff --git a/Frontend/src/pages/register.js b/Frontend/src/pages/register.js index 9e80882..f4f30bb 100644 --- a/Frontend/src/pages/register.js +++ b/Frontend/src/pages/register.js @@ -5,6 +5,7 @@ import { useRouter } from "next/router"; import { Tooltip } from "react-tooltip"; import { AiFillQuestionCircle } from "react-icons/ai"; import Link from "next/link"; +import Error from "next/error"; const Register = () => { const router = useRouter(); @@ -94,8 +95,6 @@ const Register = () => { "http://localhost:5000/register", formData ); - console.log(`responsey = ${response}`); - console.log("response", response); router.push("/portfolio"); return response; } catch (error) { @@ -103,9 +102,11 @@ const Register = () => { if (error != null) { if (error.response.data.message.toLowerCase().includes("username")) { setUsernameError("Username already taken."); - } else { + } else if ( + error.response.data.message.toLowerCase().includes("email") + ) { setEmailError("Email already taken."); - } + } else throw new Error("Unknown registration error"); } console.log(error); return false; From 181ee71bcc41ae58372c1895dda9d00ce15d2c83 Mon Sep 17 00:00:00 2001 From: nnayk <79224511+nnayk@users.noreply.github.com> Date: Fri, 1 Dec 2023 07:39:24 -0800 Subject: [PATCH 6/6] Route registration to login page --- Frontend/src/pages/register.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Frontend/src/pages/register.js b/Frontend/src/pages/register.js index f4f30bb..afd307a 100644 --- a/Frontend/src/pages/register.js +++ b/Frontend/src/pages/register.js @@ -95,7 +95,7 @@ const Register = () => { "http://localhost:5000/register", formData ); - router.push("/portfolio"); + router.push("/login"); return response; } catch (error) { console.log("err", error);