Add user and team models with auth routes and admin endpoints

This commit is contained in:
Esa Kataja
2025-05-05 17:54:39 +03:00
parent ed10e7bf80
commit 6706abfc97
5 changed files with 162 additions and 28 deletions
+34 -4
View File
@@ -1,7 +1,37 @@
from fastapi import APIRouter
from fastapi import APIRouter, HTTPException
from lib.db import get_connection
from models.user import User, UserLogin
from lib.helpers import verify_password
router = APIRouter(prefix="/auth", tags=["auth"])
@router.post("/token")
async def login():
return {"message": "Login successful"}
@router.post("/token", response_model=User)
async def login(user_login: UserLogin):
with get_connection() as conn:
user_df = conn.execute(
'SELECT * FROM "User" WHERE username = ?', (user_login.username,)
).fetchdf()
if user_df.empty:
raise HTTPException(status_code=401, detail="Invalid username or password")
user_data = user_df.to_dict(orient="records")[0]
if not verify_password(user_login.password, user_data["hashed_password"]):
raise HTTPException(status_code=401, detail="Invalid username or password")
# Update the login timestamp in the database
with get_connection() as conn:
conn.execute(
'UPDATE "User" SET last_login = CURRENT_TIMESTAMP WHERE id = ?',
(user_data["id"],),
)
# Fetch the updated user data with the new last_login timestamp
updated_user = conn.execute(
'SELECT * FROM "User" WHERE id = ?',
(user_data["id"],)
).fetchdf().to_dict(orient="records")[0]
return User(**updated_user)