Arreglos de seguridad e historial

This commit is contained in:
Mireya Cueto Garrido
2026-03-27 12:46:45 +01:00
parent e19e971cd6
commit 111acc632e
4 changed files with 69 additions and 24 deletions
+27
View File
@@ -3,6 +3,13 @@ from api.database.mongodb import users_collection
from api.models.user_models import UserCreate, UserLogin
from api.utils.security import hash_password, verify_password, generate_token
from bson import ObjectId
from fastapi import APIRouter, HTTPException, status, Depends
from api.utils.security import (
hash_password,
verify_password,
generate_token,
get_current_user,
)
router = APIRouter(prefix="/auth", tags=["auth"])
@@ -87,3 +94,23 @@ async def logout_user(user_id: str):
)
return {"message": "Sesión cerrada correctamente"}
@router.post("/logout")
async def logout_user(current_user: dict = Depends(get_current_user)):
user_id = current_user["_id"]
await users_collection.update_one(
{"_id": user_id},
{"$set": {"token": None}},
)
return {"message": "Sesión cerrada correctamente"}
@router.get("/me")
async def get_me(current_user: dict = Depends(get_current_user)):
return {
"user_id": str(current_user["_id"]),
"username": current_user["username"],
"email": current_user["email"],
}