aboutsummaryrefslogtreecommitdiff
path: root/app/routes
diff options
context:
space:
mode:
authorParker <contact@pkrm.dev>2024-11-06 01:08:04 -0600
committerParker <contact@pkrm.dev>2024-11-06 01:08:04 -0600
commit4c1dd74db3f9ac37134db13c8e5a499a9f37594a (patch)
tree5f9b72c25b66586e22d18ec5fd55e262e6eb3e2c /app/routes
parenteadc9289338e8537c1ee70274007eea2dc395bde (diff)
Lots more functionality - working towards UI
Diffstat (limited to 'app/routes')
-rw-r--r--app/routes/auth_routes.py27
-rw-r--r--app/routes/links_routes.py25
-rw-r--r--app/routes/user_routes.py6
3 files changed, 30 insertions, 28 deletions
diff --git a/app/routes/auth_routes.py b/app/routes/auth_routes.py
index 4d1c25e..ac75228 100644
--- a/app/routes/auth_routes.py
+++ b/app/routes/auth_routes.py
@@ -1,6 +1,6 @@
from fastapi import Depends, APIRouter, status, HTTPException
from fastapi.security import OAuth2PasswordRequestForm
-from fastapi.responses import Response
+from fastapi.responses import Response, JSONResponse
from datetime import timedelta
from typing import Annotated
@@ -21,7 +21,7 @@ async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
response: Response,
db=Depends(get_db),
-) -> Token:
+):
"""
Return an access token for the user, if the given authentication details are correct
"""
@@ -45,20 +45,19 @@ async def login_for_access_token(
data={"sub": user.id, "username": user.username, "refresh": True},
expires_delta=refresh_token_expires,
)
- # response = JSONResponse(content={"success": True})
- # response.set_cookie(
- # key="access_token", value=access_token, httponly=True, samesite="lax"
- # )
- # response.set_cookie(
- # key="refresh_token", value=refresh_token, httponly=True, samesite="lax"
- # )
+ response = JSONResponse(content={"success": True})
+ response.set_cookie(key="access_token", value=access_token, httponly=True)
+ response.set_cookie(
+ key="refresh_token", value=refresh_token, httponly=True
+ )
+ return response
# For Swagger UI to work, must return the token
- return Token(
- access_token=access_token,
- refresh_token=refresh_token,
- token_type="bearer",
- )
+ # return Token(
+ # access_token=access_token,
+ # refresh_token=refresh_token,
+ # token_type="bearer",
+ # )
# Full native JWT support is not complete in FastAPI yet :(
diff --git a/app/routes/links_routes.py b/app/routes/links_routes.py
index 77811c8..90ca1bd 100644
--- a/app/routes/links_routes.py
+++ b/app/routes/links_routes.py
@@ -10,7 +10,7 @@ from app.util.db_dependency import get_db
from models import Link, Log
from app.schemas.links_schemas import URLSchema
from app.schemas.auth_schemas import User
-from app.util.authentication import get_current_user_from_token
+from app.util.authentication import get_current_user
router = APIRouter(prefix="/links", tags=["links"])
@@ -18,7 +18,7 @@ router = APIRouter(prefix="/links", tags=["links"])
@router.get("/", summary="Get all of the links associated with your account")
async def get_links(
- current_user: Annotated[User, Depends(get_current_user_from_token)],
+ current_user: Annotated[User, Depends(get_current_user)],
db=Depends(get_db),
):
links = db.query(Link).filter(Link.owner == current_user.id).all()
@@ -32,7 +32,7 @@ async def get_links(
@router.post("/", summary="Create a new link")
async def create_link(
url: URLSchema,
- current_user: Annotated[User, Depends(get_current_user_from_token)],
+ current_user: Annotated[User, Depends(get_current_user)],
db=Depends(get_db),
):
# Check if the URL is valid
@@ -51,8 +51,6 @@ async def create_link(
link=link_path,
owner=current_user.id,
redirect_link=url.url,
- expire_date=datetime.datetime.now()
- + datetime.timedelta(days=30),
)
db.add(new_link)
db.commit()
@@ -60,13 +58,13 @@ async def create_link(
except:
continue
- return new_link
+ return {"link": link_path, "expire_date": new_link.expire_date}
@router.delete("/{link}", summary="Delete a link")
async def delete_link(
link: Annotated[str, Path(title="Link to delete")],
- current_user: Annotated[User, Depends(get_current_user_from_token)],
+ current_user: Annotated[User, Depends(get_current_user)],
db=Depends(get_db),
):
"""
@@ -99,7 +97,7 @@ async def delete_link(
@router.get("/{link}/logs", summary="Get all logs associated with a link")
async def get_link_logs(
link: Annotated[str, Path(title="Link to get logs for")],
- current_user: Annotated[User, Depends(get_current_user_from_token)],
+ current_user: Annotated[User, Depends(get_current_user)],
db=Depends(get_db),
):
"""
@@ -118,15 +116,20 @@ async def get_link_logs(
detail="Link not associated with your account",
)
- # Get and return all of the logs
- logs = db.query(Log).filter(Log.link == link.link).all()
+ # Get and return all of the logs - ordered by timestamp
+ logs = (
+ db.query(Log)
+ .filter(Log.link == link.link)
+ .order_by(Log.timestamp.desc())
+ .all()
+ )
return logs
@router.delete("/{link}/logs", summary="Delete logs associated with a link")
async def delete_link_logs(
link: Annotated[str, Path(title="Link to delete logs for")],
- current_user: Annotated[User, Depends(get_current_user_from_token)],
+ current_user: Annotated[User, Depends(get_current_user)],
db=Depends(get_db),
):
"""
diff --git a/app/routes/user_routes.py b/app/routes/user_routes.py
index 7fcc768..12b2828 100644
--- a/app/routes/user_routes.py
+++ b/app/routes/user_routes.py
@@ -13,7 +13,7 @@ from app.schemas.user_schemas import *
from models import User as UserModel
from app.util.authentication import (
verify_password,
- get_current_user_from_token,
+ get_current_user,
)
@@ -23,7 +23,7 @@ router = APIRouter(prefix="/users", tags=["users"])
@router.delete("/{user_id}", summary="Delete your account")
async def delete_user(
user_id: Annotated[int, Path(title="Link to delete")],
- current_user: Annotated[User, Depends(get_current_user_from_token)],
+ current_user: Annotated[User, Depends(get_current_user)],
db=Depends(get_db),
):
"""
@@ -53,7 +53,7 @@ async def delete_user(
async def update_pass(
user_id: Annotated[int, Path(title="Link to update")],
update_data: UpdatePasswordSchema,
- current_user: Annotated[User, Depends(get_current_user_from_token)],
+ current_user: Annotated[User, Depends(get_current_user)],
db=Depends(get_db),
):
"""