Skip to content

Commit

Permalink
everything is good EXCEPT for interceptors, which do not work
Browse files Browse the repository at this point in the history
  • Loading branch information
chennisden committed Jun 6, 2024
1 parent 0a7b8fb commit d7c972f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
23 changes: 13 additions & 10 deletions frontend/src/hooks/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,24 @@ export const AuthenticationProvider = (props: AuthenticationProviderProps) => {
withCredentials: true,
});

if (apiKey !== null) {
// Adds the API key to the request header since it is set.
api.interceptors.request.use(
(config) => {
config.headers.Authorization = `Bearer ${apiKey}`;
return config;
},
(error) => {
return Promise.reject(error);
},
);
}

useEffect(() => {
if (apiKey === null) {
deleteLocalStorageApiKey();
} else {
setLocalStorageApiKey(apiKey);
// Adds the API key to the request header since it is set.
api.interceptors.request.use(
(config) => {
config.headers.Authorization = `Bearer ${apiKey}`;
return config;
},
(error) => {
return Promise.reject(error);
},
);
}
}, [apiKey]);

Expand Down
2 changes: 2 additions & 0 deletions store/app/api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

from store.app.api.crud.base import BaseCrud
from store.app.api.crud.users import UserCrud
from store.app.api.crud.robots import RobotCrud


class Crud(
UserCrud,
RobotCrud,
BaseCrud,
):
"""Composes the various CRUD classes into a single class."""
Expand Down
11 changes: 9 additions & 2 deletions store/app/api/routers/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Defines the main API endpoint."""

import logging
import uuid
from typing import Annotated, Dict, List

from fastapi import APIRouter, Depends, FastAPI, HTTPException, status
Expand All @@ -12,6 +13,8 @@
from store.app.api.routers.users import users_router
from store.settings import settings

from store.app.api.routers.users import ApiKeyData, get_api_key

logger = logging.getLogger(__name__)

app = FastAPI()
Expand Down Expand Up @@ -149,8 +152,12 @@ async def list_parts(crud: Annotated[Crud, Depends(Crud.get)]) -> List[Part]:


@api_router.post("/add/robot/")
async def add_robot(api_key: str, robot: Robot, crud: Annotated[Crud, Depends(Crud.get)]) -> bool:
user_id = await crud.get_user_id_from_api_key(api_key)
async def add_robot(
robot: Robot,
data: Annotated[ApiKeyData, Depends(get_api_key)],
crud: Annotated[Crud, Depends(Crud.get)],
) -> bool:
user_id = await crud.get_user_id_from_api_key(data.api_key)
if user_id is None:
raise HTTPException(status_code=401, detail="Must be logged in to add a robot")
robot.owner = str(user_id)
Expand Down

0 comments on commit d7c972f

Please sign in to comment.