Skip to content

Commit

Permalink
Merge pull request #51 from grant-baer/registration
Browse files Browse the repository at this point in the history
Registration
  • Loading branch information
Gannon44 authored Dec 1, 2023
2 parents 3da1d99 + 181ee71 commit 14b2f04
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
17 changes: 8 additions & 9 deletions Backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__":
Expand Down
50 changes: 34 additions & 16 deletions Backend/db_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand All @@ -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")
Expand Down
16 changes: 13 additions & 3 deletions Frontend/src/pages/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 14b2f04

Please sign in to comment.