diff --git a/Backend/backend.py b/Backend/backend.py index ca78098..b9888d4 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 @@ -176,22 +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: - print("Duplicate username, please choose another") - return jsonify( - {"message": "Duplicate username, please choose another!"} - ) 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 f78cfd7..d3da012 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, ) @@ -126,19 +125,38 @@ def create_image(): ) -@app.route("/users") +@app.route("/users", methods=["GET"]) def get_users(): - users = User.objects.all() - return jsonify( - [ - { - "username": user.username, - "name": user.name, - "user_id": str(user.id), - } - for user in users - ] - ) + data = json.loads(request.data.decode("utf-8")) + user = User.objects(username=data["username"]).first() + print(f"user={user}") + if user: + return ( + jsonify( + { + "error": """Username already exists. + Choose another.""" + } + ), + 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 e197ebf..afd307a 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(); @@ -91,13 +92,22 @@ 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); - router.push("/portfolio"); + router.push("/login"); return response; } catch (error) { + console.log("err", error); + if (error != null) { + if (error.response.data.message.toLowerCase().includes("username")) { + setUsernameError("Username already taken."); + } 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; }