From 14c4704fb94bbd6fbc909cc87ba5b8b44d8236b5 Mon Sep 17 00:00:00 2001 From: Jared Zhao Date: Wed, 27 Apr 2022 12:19:15 -0700 Subject: [PATCH] Add ability to set api token via environment variable (#13) --- dashboard/src/components/Dashboard.tsx | 4 +++- server/config.py | 1 + server/logic/auth.py | 9 +++++++++ server/main.py | 6 +++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/dashboard/src/components/Dashboard.tsx b/dashboard/src/components/Dashboard.tsx index aa353a8..8387e82 100644 --- a/dashboard/src/components/Dashboard.tsx +++ b/dashboard/src/components/Dashboard.tsx @@ -15,7 +15,9 @@ function Dashboard(props: Props) { const [data, setData] = useState<{[name: string]: Data}>(); const getData = () => { - getDataAPI(Date.now() / 1000 - (60 * 60)).then((data) => { + // TODO: Retrieve all data for now, until we implement selector + // e.g. Something like: Date.now() / 1000 - (60 * 60) + getDataAPI(0).then((data) => { setData(data.data); }); } diff --git a/server/config.py b/server/config.py index d7af769..8ca1425 100644 --- a/server/config.py +++ b/server/config.py @@ -6,3 +6,4 @@ SQL_MODE = "sqlite" SQL_VERSION = 0 DEFAULT_USER = True +DEFAULT_API_KEY = os.environ.get("DEFAULT_API_KEY", "default") diff --git a/server/logic/auth.py b/server/logic/auth.py index 40ba178..4e3afbb 100644 --- a/server/logic/auth.py +++ b/server/logic/auth.py @@ -14,6 +14,15 @@ def create_user(email: str) -> uuid.UUID: return user_id +def get_user_id(email: str) -> uuid.UUID: + """ + Get a user. + """ + user_id = queries.get_user_id(email=email) + + return user_id + + def generate_api_token(user_id: uuid.UUID, api_token: Optional[str] = None) -> None: """ Generate a api_token for a user. diff --git a/server/main.py b/server/main.py index 6645600..10a634b 100644 --- a/server/main.py +++ b/server/main.py @@ -127,10 +127,14 @@ def startup() -> None: if config.DEFAULT_USER: try: user_id = auth.create_user(email="default") - auth.generate_api_token(user_id=user_id, api_token="default") except: # Default user already exists pass + finally: + # Update default token (in case it changed) + user_id = auth.get_user_id(email="default") + auth.generate_api_token(user_id=user_id, api_token=config.DEFAULT_API_KEY) + logger.info("") logger.info("READY")