Spaces:
Sleeping
Sleeping
Update api/routes.py
Browse files- api/routes.py +11 -4
api/routes.py
CHANGED
|
@@ -2,7 +2,7 @@ from fastapi import APIRouter, HTTPException, Depends
|
|
| 2 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 3 |
from models.schemas import SignupForm, TokenResponse
|
| 4 |
from db.mongo import users_collection
|
| 5 |
-
from core.security import hash_password, verify_password, create_access_token
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
router = APIRouter()
|
|
@@ -13,7 +13,7 @@ async def signup(data: SignupForm):
|
|
| 13 |
existing = await users_collection.find_one({"email": email})
|
| 14 |
if existing:
|
| 15 |
raise HTTPException(status_code=409, detail="Email already exists")
|
| 16 |
-
|
| 17 |
hashed_pw = hash_password(data.password)
|
| 18 |
user_doc = {
|
| 19 |
"email": email,
|
|
@@ -29,6 +29,13 @@ async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
| 29 |
user = await users_collection.find_one({"email": email})
|
| 30 |
if not user or not verify_password(form_data.password, user["password"]):
|
| 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"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 3 |
from models.schemas import SignupForm, TokenResponse
|
| 4 |
from db.mongo import users_collection
|
| 5 |
+
from core.security import hash_password, verify_password, create_access_token, get_current_user
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
router = APIRouter()
|
|
|
|
| 13 |
existing = await users_collection.find_one({"email": email})
|
| 14 |
if existing:
|
| 15 |
raise HTTPException(status_code=409, detail="Email already exists")
|
| 16 |
+
|
| 17 |
hashed_pw = hash_password(data.password)
|
| 18 |
user_doc = {
|
| 19 |
"email": email,
|
|
|
|
| 29 |
user = await users_collection.find_one({"email": email})
|
| 30 |
if not user or not verify_password(form_data.password, user["password"]):
|
| 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"}
|
| 35 |
+
|
| 36 |
+
@router.get("/me")
|
| 37 |
+
async def get_me(current_user: dict = Depends(get_current_user)):
|
| 38 |
+
return {
|
| 39 |
+
"email": current_user["email"],
|
| 40 |
+
"created_at": current_user.get("created_at")
|
| 41 |
+
}
|