Spaces:
Sleeping
Sleeping
Download app/routers/auth.py from flowmind-ai/flowmind-core-api: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/spaces/flowmind-ai/flowmind-core-api/resolve/main/app/routers/auth.py
- Command line
-
hf download hf://spaces/flowmind-ai/flowmind-core-api/app/routers/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/flowmind-ai/flowmind-core-api/resolve/main/app/routers/auth.py
2.64 kB
| # app/routers/auth.py | |
| from datetime import datetime | |
| from typing import Optional | |
| import hashlib | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from pydantic import BaseModel, Field | |
| from sqlalchemy.orm import Session | |
| from app.db import get_db | |
| from app import models | |
| router = APIRouter( | |
| prefix="/auth", | |
| tags=["auth"], | |
| ) | |
| # ----------------------- | |
| # Helpers | |
| # ----------------------- | |
| def _hash_password(password: str) -> str: | |
| return hashlib.sha256(password.encode("utf-8")).hexdigest() | |
| def _verify_password(password: str, password_hash: str) -> bool: | |
| return _hash_password(password) == password_hash | |
| # ----------------------- | |
| # Schemas | |
| # ----------------------- | |
| class RegisterRequest(BaseModel): | |
| full_name: Optional[str] = None | |
| email: str # ❗ EmailStr kaldırıldı | |
| password: str = Field(min_length=6) | |
| class AuthUser(BaseModel): | |
| id: int | |
| full_name: Optional[str] = None | |
| email: str | |
| is_active: bool | |
| created_at: datetime | |
| class Config: | |
| from_attributes = True | |
| class LoginRequest(BaseModel): | |
| email: str | |
| password: str | |
| class LoginResponse(BaseModel): | |
| access_token: str | |
| token_type: str = "bearer" | |
| user: AuthUser | |
| # ----------------------- | |
| # Register | |
| # ----------------------- | |
| def register_user(payload: RegisterRequest, db: Session = Depends(get_db)): | |
| # email varsa hata | |
| existing = db.query(models.User).filter_by(email=payload.email).first() | |
| if existing: | |
| raise HTTPException(status_code=400, detail="Email already registered.") | |
| user = models.User( | |
| full_name=payload.full_name, | |
| email=payload.email, | |
| password_hash=_hash_password(payload.password), | |
| is_active=True, # doğrulama yok → direk aktif | |
| ) | |
| db.add(user) | |
| db.commit() | |
| db.refresh(user) | |
| return user | |
| # ----------------------- | |
| # Login | |
| # ----------------------- | |
| def login(payload: LoginRequest, db: Session = Depends(get_db)): | |
| user = db.query(models.User).filter_by(email=payload.email).first() | |
| if not user or not _verify_password(payload.password, user.password_hash): | |
| raise HTTPException(status_code=401, detail="Invalid email or password.") | |
| if not user.is_active: | |
| raise HTTPException(status_code=403, detail="User disabled.") | |
| user.last_login = datetime.utcnow() | |
| db.add(user) | |
| db.commit() | |
| token = str(user.id) # ❗ Çok basit token — Bearer <id> | |
| return LoginResponse(access_token=token, user=user) | |