Download app/api/v1/auth.py from isold/meloon: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/spaces/isold/meloon/resolve/main/app/api/v1/auth.py
- Command line
-
hf download hf://spaces/isold/meloon/app/api/v1/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/isold/meloon/resolve/main/app/api/v1/auth.py
2.17 kB
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.orm import Session | |
| from datetime import timedelta | |
| from app.api import deps | |
| from app.core import security | |
| from app.core.config import settings | |
| from app.models.models import User | |
| from app.schemas import schemas | |
| router = APIRouter() | |
| def register( | |
| user_in: schemas.UserCreate, | |
| db: Session = Depends(deps.get_db) | |
| ): | |
| # Check if email already exists | |
| user_by_email = db.query(User).filter(User.email == user_in.email).first() | |
| if user_by_email: | |
| raise HTTPException( | |
| status_code=400, | |
| detail="The user with this email already exists.", | |
| ) | |
| # Generate username from email | |
| base_username = user_in.email.split("@")[0] | |
| username = base_username | |
| # Check if username exists - NO LONGER REQUIRED as username needs not be unique | |
| # We just use the base_username derived from email | |
| user = User( | |
| email=user_in.email, | |
| password_hash=security.get_password_hash(user_in.password), | |
| username=username | |
| ) | |
| db.add(user) | |
| db.commit() | |
| db.refresh(user) | |
| return None | |
| def login( | |
| user_in: schemas.UserLogin, | |
| db: Session = Depends(deps.get_db) | |
| ): | |
| user = db.query(User).filter(User.email == user_in.email).first() | |
| if not user or not security.verify_password(user_in.password, user.password_hash): | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Incorrect username or password", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) | |
| access_token = security.create_access_token( | |
| data={"sub": user.email}, expires_delta=access_token_expires | |
| ) | |
| return { | |
| "token": access_token, | |
| "user": user | |
| } | |
| def get_current_user_info( | |
| current_user: User = Depends(deps.get_current_user) | |
| ): | |
| return current_user | |