From 6bf2cba862669640c69306535f4c7f9c1ee20751 Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 30 Sep 2020 23:53:26 +0200 Subject: [PATCH] Set up Cross-Origin Resource Sharing (CORS) --- app/__init__.py | 9 +++++++++ app/routes.py | 7 ++----- shell.nix | 1 + 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index d747fcf..8b6c8ac 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,5 +1,14 @@ from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware from app.routes import router +from constants import origins app = FastAPI() app.include_router(router) +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) diff --git a/app/routes.py b/app/routes.py index 8be6d83..07f58c8 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,5 +1,4 @@ from fastapi import APIRouter, Depends -from fastapi.security import OAuth2PasswordBearer from sqlalchemy.orm import Session from app.external_services import resend_otp, send_otp @@ -8,12 +7,10 @@ from database.crud import add_user, get_db, verify_otp router = APIRouter() -oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") - @router.post("/register", response_model=UserCreateResponse) -def create_user(data: UserCreate, db: Session = Depends(get_db)): - user = add_user(data=data, db=db) +def add_user(data: UserCreate, db: Session = Depends(get_db)): + user = create_user(data=data, db=db) send_otp(data=user, db=db) return user diff --git a/shell.nix b/shell.nix index a197bba..51b7b0a 100644 --- a/shell.nix +++ b/shell.nix @@ -13,6 +13,7 @@ mkShell { python38Packages.alembic python38Packages.pytest python38Packages.twilio + python38Packages.cryptography python38Packages.passlib python38Packages.bcrypt sqlite