Spaces:
Runtime error
Runtime error
| from fastapi import APIRouter, Depends, status | |
| from fastapi.security import OAuth2PasswordBearer | |
| from sqlalchemy.orm import Session | |
| from backend.app.core.database import get_db | |
| from backend.app.services.auth import AuthService | |
| from backend.app.schemas.auth import UserRegister, UserLogin, UserResponse, Token | |
| from backend.app.models.users import User | |
| router = APIRouter(prefix="/auth", tags=["auth"]) | |
| oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/login", auto_error=False) | |
| def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)) -> User: | |
| """Dependency to retrieve the currently logged in user using their JWT token.""" | |
| from fastapi import HTTPException | |
| if not token: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Not authenticated. Missing bearer token." | |
| ) | |
| auth_service = AuthService(db) | |
| return auth_service.get_current_user_by_token(token) | |
| def signup(data: UserRegister, db: Session = Depends(get_db)): | |
| """Register a new customer or worker account.""" | |
| auth_service = AuthService(db) | |
| return auth_service.signup(data) | |
| def login(data: UserLogin, db: Session = Depends(get_db)): | |
| """Verify phone + OTP (demo otp: 123456) and return JWT credentials.""" | |
| auth_service = AuthService(db) | |
| return auth_service.login(data) | |
| def get_me(current_user: User = Depends(get_current_user)): | |
| """Fetch profile details of the currently authenticated user.""" | |
| return current_user | |