fix: Change login endpoint to accept JSON instead of OAuth2 form data
- Added UserLogin schema for JSON login request - Updated auth.py to use UserLogin instead of OAuth2PasswordRequestForm - Added file_ids to ChatRequest schema
This commit is contained in:
parent
c32a95fc91
commit
bc20de7567
@ -1,6 +1,5 @@
|
||||
from datetime import timedelta
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.database import get_db
|
||||
from app.core.auth import (
|
||||
@ -13,6 +12,7 @@ from app.core.config import settings
|
||||
from app.models.models import User
|
||||
from app.schemas.schemas import (
|
||||
UserCreate,
|
||||
UserLogin,
|
||||
UserResponse,
|
||||
UserUpdate,
|
||||
Token
|
||||
@ -30,7 +30,7 @@ def register(user_data: UserCreate, db: Session = Depends(get_db)):
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Email already registered"
|
||||
)
|
||||
|
||||
|
||||
# Check if username already exists
|
||||
db_user = db.query(User).filter(User.username == user_data.username).first()
|
||||
if db_user:
|
||||
@ -38,7 +38,7 @@ def register(user_data: UserCreate, db: Session = Depends(get_db)):
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Username already taken"
|
||||
)
|
||||
|
||||
|
||||
# Create new user
|
||||
hashed_password = get_password_hash(user_data.password)
|
||||
new_user = User(
|
||||
@ -51,36 +51,34 @@ def register(user_data: UserCreate, db: Session = Depends(get_db)):
|
||||
db.add(new_user)
|
||||
db.commit()
|
||||
db.refresh(new_user)
|
||||
|
||||
|
||||
return new_user
|
||||
|
||||
|
||||
@router.post("/login", response_model=Token)
|
||||
def login(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):
|
||||
# Find user by username or email
|
||||
user = db.query(User).filter(
|
||||
(User.username == form_data.username) | (User.email == form_data.username)
|
||||
).first()
|
||||
|
||||
if not user or not verify_password(form_data.password, user.hashed_password):
|
||||
def login(login_data: UserLogin, db: Session = Depends(get_db)):
|
||||
# Find user by email
|
||||
user = db.query(User).filter(User.email == login_data.email).first()
|
||||
|
||||
if not user or not verify_password(login_data.password, user.hashed_password):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
detail="Incorrect email or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
|
||||
if not user.is_active:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Inactive user"
|
||||
)
|
||||
|
||||
|
||||
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
access_token = create_access_token(
|
||||
data={"sub": user.id},
|
||||
data={"sub": str(user.id), "email": user.email, "role": user.role},
|
||||
expires_delta=access_token_expires
|
||||
)
|
||||
|
||||
|
||||
return {"access_token": access_token, "token_type": "bearer"}
|
||||
|
||||
|
||||
@ -106,7 +104,7 @@ def update_me(
|
||||
detail="Email already registered"
|
||||
)
|
||||
current_user.email = user_data.email
|
||||
|
||||
|
||||
if user_data.username:
|
||||
existing_user = db.query(User).filter(
|
||||
User.username == user_data.username,
|
||||
@ -118,11 +116,11 @@ def update_me(
|
||||
detail="Username already taken"
|
||||
)
|
||||
current_user.username = user_data.username
|
||||
|
||||
|
||||
if user_data.password:
|
||||
current_user.hashed_password = get_password_hash(user_data.password)
|
||||
|
||||
|
||||
db.commit()
|
||||
db.refresh(current_user)
|
||||
|
||||
|
||||
return current_user
|
||||
|
||||
@ -13,6 +13,11 @@ class UserCreate(UserBase):
|
||||
password: str
|
||||
|
||||
|
||||
class UserLogin(BaseModel):
|
||||
email: EmailStr
|
||||
password: str
|
||||
|
||||
|
||||
class UserUpdate(BaseModel):
|
||||
email: Optional[EmailStr] = None
|
||||
username: Optional[str] = None
|
||||
@ -116,13 +121,14 @@ class UploadedFileResponse(UploadedFileBase):
|
||||
class ChatRequest(BaseModel):
|
||||
message: str
|
||||
endpoint_id: Optional[int] = None
|
||||
conversation_history: Optional[List[ChatMessageBase]] = None
|
||||
file_ids: Optional[List[int]] = None
|
||||
conversation_history: Optional[List[dict]] = None
|
||||
|
||||
|
||||
class ChatResponse(BaseModel):
|
||||
response: str
|
||||
endpoint_id: Optional[int] = None
|
||||
model: Optional[str] = None
|
||||
endpoint_used: Optional[str] = None
|
||||
model_used: Optional[str] = None
|
||||
|
||||
|
||||
# AdminStats schema
|
||||
@ -130,5 +136,4 @@ class AdminStats(BaseModel):
|
||||
total_users: int
|
||||
total_endpoints: int
|
||||
total_messages: int
|
||||
total_files: int
|
||||
active_users: int
|
||||
active_endpoints: int
|
||||
|
||||
Loading…
Reference in New Issue
Block a user