Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- api/routes.py +1 -1
- core/config.py +9 -0
- core/security.py +18 -0
- db/mongo.py +7 -0
- main.py +19 -0
- models/schemas.py +9 -0
api/routes.py
CHANGED
|
@@ -31,4 +31,4 @@ async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
| 31 |
raise HTTPException(status_code=401, detail="Invalid credentials")
|
| 32 |
|
| 33 |
access_token = create_access_token(data={"sub": user["email"]})
|
| 34 |
-
return {"access_token": access_token, "token_type": "bearer"}
|
|
|
|
| 31 |
raise HTTPException(status_code=401, detail="Invalid credentials")
|
| 32 |
|
| 33 |
access_token = create_access_token(data={"sub": user["email"]})
|
| 34 |
+
return {"access_token": access_token, "token_type": "bearer"}
|
core/config.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key")
|
| 4 |
+
ALGORITHM = "HS256"
|
| 5 |
+
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
| 6 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
| 7 |
+
|
| 8 |
+
if not MONGO_URI:
|
| 9 |
+
raise RuntimeError("MONGO_URI not set!")
|
core/security.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime, timedelta
|
| 2 |
+
from passlib.context import CryptContext
|
| 3 |
+
from jose import jwt
|
| 4 |
+
from app.core.config import SECRET_KEY, ALGORITHM, ACCESS_TOKEN_EXPIRE_MINUTES
|
| 5 |
+
|
| 6 |
+
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 7 |
+
|
| 8 |
+
def hash_password(password: str) -> str:
|
| 9 |
+
return pwd_context.hash(password)
|
| 10 |
+
|
| 11 |
+
def verify_password(plain: str, hashed: str) -> bool:
|
| 12 |
+
return pwd_context.verify(plain, hashed)
|
| 13 |
+
|
| 14 |
+
def create_access_token(data: dict, expires_delta: timedelta = None):
|
| 15 |
+
to_encode = data.copy()
|
| 16 |
+
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
|
| 17 |
+
to_encode.update({"exp": expire})
|
| 18 |
+
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
db/mongo.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import certifi
|
| 2 |
+
from motor.motor_asyncio import AsyncIOMotorClient
|
| 3 |
+
from app.core.config import MONGO_URI
|
| 4 |
+
|
| 5 |
+
client = AsyncIOMotorClient(MONGO_URI, tls=True, tlsCAFile=certifi.where())
|
| 6 |
+
db = client["cps_db"]
|
| 7 |
+
users_collection = db["users"]
|
main.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from app.api.routes import router as api_router
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
app.add_middleware(
|
| 8 |
+
CORSMiddleware,
|
| 9 |
+
allow_origins=["*"],
|
| 10 |
+
allow_credentials=True,
|
| 11 |
+
allow_methods=["*"],
|
| 12 |
+
allow_headers=["*"],
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
app.include_router(api_router)
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def root():
|
| 19 |
+
return {"message": "🚀 FastAPI MongoDB with JWT & bcrypt is running"}
|
models/schemas.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, EmailStr
|
| 2 |
+
|
| 3 |
+
class SignupForm(BaseModel):
|
| 4 |
+
email: EmailStr
|
| 5 |
+
password: str
|
| 6 |
+
|
| 7 |
+
class TokenResponse(BaseModel):
|
| 8 |
+
access_token: str
|
| 9 |
+
token_type: str
|