From cc62a0db3e00d7fac53103182537e7ef734fb9d8 Mon Sep 17 00:00:00 2001 From: yaso-meth Date: Mon, 22 Jul 2024 12:17:55 +0200 Subject: [PATCH] add supertokens api to API hub --- backend/__init__.py | 27 +++++++++++++++++++++++++++ backend/main.py | 36 ++++++++++++++++++++++++++++-------- backend/requirements.txt | 3 ++- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/backend/__init__.py b/backend/__init__.py index e69de29b..4097a68f 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -0,0 +1,27 @@ +from supertokens_python import init, InputAppInfo, SupertokensConfig +from supertokens_python.recipe import emailpassword, session, dashboard + +init( + app_info=InputAppInfo( + app_name="MIH_API_HUB", + api_domain="http://localhost:8080/", + website_domain="http://mzansi-innovation-hub.co.za", + api_base_path="/auth", + website_base_path="/auth" + ), + supertokens_config=SupertokensConfig( + # https://try.supertokens.com is for demo purposes. Replace this with the address of your core instance (sign up on supertokens.com), or self host a core. + connection_uri="http://MIH-SuperTokens:3567/", + api_key="leatucczyixqwkqqdrhayiwzeofkltds" + ), + framework='fastapi', + recipe_list=[ + session.init(), # initializes session features + emailpassword.init(), + dashboard.init(admins=[ + "yasienmeth@gmail.com", + ], + ) + ], + mode='asgi' # use wsgi if you are running using gunicorn +) \ No newline at end of file diff --git a/backend/main.py b/backend/main.py index ed65e305..fd71522c 100644 --- a/backend/main.py +++ b/backend/main.py @@ -3,6 +3,11 @@ from pydantic import BaseModel from .routers import docOffices, patients, patients_files, patients_notes, users, fileStorage, medicine from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware import Middleware +from supertokens_python import get_all_cors_headers +from supertokens_python.framework.fastapi import get_middleware +from supertokens_python.recipe.session.framework.fastapi import verify_session +from supertokens_python.recipe.session import SessionContainer +from fastapi import Depends origins = [ "http://localhost", @@ -14,17 +19,25 @@ origins = [ "*", ] -middleware = [ - Middleware( +# middleware = [ +# Middleware( +# CORSMiddleware, +# allow_origins=origins, +# allow_credentials=True, +# allow_methods=["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"], +# allow_headers=["Content-Type"] + get_all_cors_headers(), +# ) +# ] + +app = FastAPI()#middleware=middleware) +app.add_middleware(get_middleware()) +app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], - ) -] - -app = FastAPI(middleware=middleware) + allow_methods=["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"], + allow_headers=["Content-Type"] + get_all_cors_headers() +) app.include_router(docOffices.router) app.include_router(patients.router) app.include_router(patients_files.router) @@ -39,6 +52,13 @@ app.include_router(medicine.router) def read_root(): return serverRunning() +# Check if server is up +@app.get("/session") +def read_root(): + async def like_comment(session: SessionContainer = Depends(verify_session())): + user_id = session.get_user_id() + + return {"Session id": user_id} def serverRunning(): return {"Status": "Server is Up and Running"} diff --git a/backend/requirements.txt b/backend/requirements.txt index 5cc7f77e..519c8119 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -6,4 +6,5 @@ reportlab requests watchfiles==0.21.0 python-multipart -xlrd \ No newline at end of file +xlrd +supertokens-python \ No newline at end of file