from fastapi import FastAPI, HTTPException
from authx import AuthX, AuthXConfig
from pydantic import BaseModel
app = FastAPI()
config = AuthXConfig()
config.JWT_SECRET_KEY = "SECRET_KEY"
config.JWT_ACCESS_COOKIE_NAME = "MY_ACCESS_TOKEN"
config.JWT_TOKEN_LOCATION = ["cookies"]
security = AuthX(config=config)
class UserLoginSchema(BaseModel):
username: str
password: str
@app.post("/login")
def login(creds: UserLoginSchema):
if creds.username == "test" and creds.password == "test":
token = security.create_access_token(uid="12345")
return {"access_token": token}
raise HTTPException(status_code=401, detail="Incorrect username or password")
@app.get("/protected")
def protected():
pass
fastapi dev main.py
FastAPI Starting development server ?
Searching for package file structure from directories with __init__.py files
Importing from C:\Users\Shamil\PycharmProjects\PythonProject1234
module ? main.py
code Importing the FastAPI app object from the module with the following code:
from main import app
app Using import string: main:app
server Server started at http://127.0.0.1:8000
server Documentation at http://127.0.0.1:8000/docs
tip Running in development mode, for production use: fastapi run
Logs:
INFO Will watch for changes in these directories: ['C:\\Users\\Shamil\\PycharmProjects\\PythonProject1234']
INFO:uvicorn.error:Will watch for changes in these directories: ['C:\\Users\\Shamil\\PycharmProjects\\PythonProject1234']
INFO Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:uvicorn.error:Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO Started reloader process [9992] using WatchFiles
INFO:uvicorn.error:Started reloader process [9992] using WatchFiles
INFO Started server process [17872]
INFO:uvicorn.error:Started server process [17872]
INFO Waiting for application startup.
INFO:uvicorn.error:Waiting for application startup.
INFO Application startup complete.
INFO:uvicorn.error:Application startup complete.