This commit is contained in:
Timothy J. Baek 2024-04-02 09:27:35 -07:00
parent ba0523cd69
commit da8646cae9
3 changed files with 19 additions and 19 deletions

View file

@ -167,22 +167,6 @@ class AuthsTable:
except: except:
return False return False
def update_api_key_by_id(self, id: str, api_key: str) -> str:
try:
query = Auth.update(api_key=api_key).where(Auth.id == id)
result = query.execute()
return True if result == 1 else False
except:
return False
def get_api_key_by_id(self, id: str) -> Optional[str]:
try:
auth = Auth.get(Auth.id == id)
return auth.api_key
except:
return None
def delete_auth_by_id(self, id: str) -> bool: def delete_auth_by_id(self, id: str) -> bool:
try: try:
# Delete User # Delete User

View file

@ -158,5 +158,21 @@ class UsersTable:
except: except:
return False return False
def update_user_api_key_by_id(self, id: str, api_key: str) -> str:
try:
query = User.update(api_key=api_key).where(User.id == id)
result = query.execute()
return True if result == 1 else False
except:
return False
def get_user_api_key_by_id(self, id: str) -> Optional[str]:
try:
user = User.get(User.id == id)
return user.api_key
except:
return None
Users = UsersTable(DB) Users = UsersTable(DB)

View file

@ -277,7 +277,7 @@ async def update_token_expires_duration(
@router.post("/api_key", response_model=ApiKey) @router.post("/api_key", response_model=ApiKey)
async def create_api_key_(user=Depends(get_current_user)): async def create_api_key_(user=Depends(get_current_user)):
api_key = create_api_key() api_key = create_api_key()
success = Auths.update_api_key_by_id(user.id, api_key) success = Users.update_user_api_key_by_id(user.id, api_key)
if success: if success:
return { return {
"api_key": api_key, "api_key": api_key,
@ -289,14 +289,14 @@ async def create_api_key_(user=Depends(get_current_user)):
# delete api key # delete api key
@router.delete("/api_key", response_model=bool) @router.delete("/api_key", response_model=bool)
async def delete_api_key(user=Depends(get_current_user)): async def delete_api_key(user=Depends(get_current_user)):
success = Auths.update_api_key_by_id(user.id, None) success = Users.update_user_api_key_by_id(user.id, None)
return success return success
# get api key # get api key
@router.get("/api_key", response_model=ApiKey) @router.get("/api_key", response_model=ApiKey)
async def get_api_key(user=Depends(get_current_user)): async def get_api_key(user=Depends(get_current_user)):
api_key = Auths.get_api_key_by_id(user.id, None) api_key = Users.get_user_api_key_by_id(user.id)
if api_key: if api_key:
return { return {
"api_key": api_key, "api_key": api_key,