From ddb984d0cde0458d8e2db9e5230b15d052a703bf Mon Sep 17 00:00:00 2001 From: Parker Date: Tue, 5 Nov 2024 21:23:19 -0600 Subject: [PATCH] Update + Signup works! --- app/main.py | 50 -------------------------------------- app/routes/user_routes.py | 7 +++--- app/templates/login.html | 2 -- app/templates/signup.html | 17 ++++++------- app/util/authentication.py | 2 +- 5 files changed, 12 insertions(+), 66 deletions(-) diff --git a/app/main.py b/app/main.py index 90b3104..2d13ae2 100644 --- a/app/main.py +++ b/app/main.py @@ -49,56 +49,6 @@ async def signup(request: Request): return templates.TemplateResponse("signup.html", {"request": request}) -# TODO: Create users routes -# User - register/create -# User - delete -# User - update - -# @app.route("/signup", methods=["GET", "POST"]) -# def signup(): -# if request.method == "POST": -# username = request.form["username"] -# password = request.form["password"] - -# # Verify the password meets requirements -# if len(password) < 8: -# return {"status": "Password must be at least 8 characters"} -# if not any(char.isdigit() for char in password): -# return {"status": "Password must contain at least one digit"} -# if not any(char.isupper() for char in password): -# return { -# "status": "Password must contain at least one uppercase letter" -# } - -# # Get database session -# db = SessionLocal() - -# user = db.query(User).filter(User.username == username).first() -# if user: -# db.close() -# return {"status": "Username not available"} -# # Add information to the database -# hashed_password = bcrypt.hashpw( -# password.encode("utf-8"), bcrypt.gensalt() -# ).decode("utf-8") -# api_key = "".join( -# random.choices(string.ascii_letters + string.digits, k=20) -# ) -# new_user = User( -# username=username, password=hashed_password, api_key=api_key -# ) -# db.add(new_user) -# db.commit() -# db.close() -# # Log in the newly created user -# flask_user = FlaskUser() -# flask_user.id = username -# login_user(flask_user) - -# return {"status": "success"} -# return render_template("signup.html") - - @app.get("/dashboard") async def dashboard( response: Annotated[ diff --git a/app/routes/user_routes.py b/app/routes/user_routes.py index c356104..7fcc768 100644 --- a/app/routes/user_routes.py +++ b/app/routes/user_routes.py @@ -1,5 +1,6 @@ from fastapi import APIRouter, status, Path, Depends from fastapi.exception_handlers import HTTPException +from fastapi.security import OAuth2PasswordRequestForm from typing import Annotated import string import bcrypt @@ -92,15 +93,15 @@ async def update_pass( @router.post("/register", summary="Register a new user") async def get_links( - login_data: LoginDataSchema, + form_data: Annotated[OAuth2PasswordRequestForm, Depends()], db=Depends(get_db), ): """ Given the login data (username, password) process the registration of a new user account and return either the user or an error message """ - username = login_data.username - password = login_data.password + username = form_data.username + password = form_data.password # Make sure the password meets all of the requirements check_password_reqs(password) diff --git a/app/templates/login.html b/app/templates/login.html index bca8b63..8e59481 100644 --- a/app/templates/login.html +++ b/app/templates/login.html @@ -96,8 +96,6 @@ body: formData }); - console.log(await response.json()); - if (response.status != 200) { document.getElementById('error').style.display = 'block'; } else { diff --git a/app/templates/signup.html b/app/templates/signup.html index a6d8fbf..446aaeb 100644 --- a/app/templates/signup.html +++ b/app/templates/signup.html @@ -91,24 +91,21 @@ // Prevent default form submission event.preventDefault(); - // Get form data const formData = new FormData(this); - - console.log(formData) - // Send POST request to /signup containing form data - const response = await fetch('/signup', { + const response = await fetch('/api/users/register', { method: 'POST', body: formData }); - data = await response.json() + if (response.status != 200) { + const data = await response.json(); - if (data.status != "success") { document.getElementById('error').style.display = 'block'; - document.getElementById('error').innerText = data.status; - } else { - window.location.href = '/dashboard'; + document.getElementById('error').innerText = data.detail; + } + else { + window.location.href = '/login'; } }); \ No newline at end of file diff --git a/app/util/authentication.py b/app/util/authentication.py index 1127451..b270c6d 100644 --- a/app/util/authentication.py +++ b/app/util/authentication.py @@ -86,7 +86,7 @@ async def get_current_user_from_token( return await get_current_user(token, db=db) -# Backwards kinda of way to get refresh token support +# Backwards kind of way to get refresh token support # `refresh_get_current_user` is only called from /refresh # and alerts `get_current_user` that it should expect a refresh token async def refresh_get_current_user(