-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
75 lines (64 loc) · 2.62 KB
/
user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from fastapi import HTTPException, status
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError
from dependencies.auth import create_access_token, get_current_user
from dependencies.database import get_db
from models.user import User
from schemas.user import UserCreate, User, UserLogin, Token
from passlib.context import CryptContext
# Load environment variables
from .config import settings
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
class UserService:
def __init__(self, db: Session = Depends(get_db)):
self.db = db
async def create_user(self, request: UserCreate):
hashed_password = pwd_context.hash(request.password)
new_user = User(username=request.username, email=request.email, password=hashed_password)
try:
self.db.add(new_user)
self.db.commit()
self.db.refresh(new_user)
return new_user
except IntegrityError as e:
self.db.rollback()
if "users_username_key" in str(e):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Username already exists",
)
if "users_email_key" in str(e):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Email already exists",
)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Error creating user",
)
async def authenticate_user(self, request: UserLogin):
user = self.db.query(User).filter(User.email == request.email).first()
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid credentials",
headers={"WWW-Authenticate": "Bearer"},
)
if not pwd_context.verify(request.password, user.password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect password",
headers={"WWW-Authenticate": "Bearer"},
)
return user
async def get_user_by_id(self, user_id: int):
user = self.db.query(User).filter(User.id == user_id).first()
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found",
)
return user
user_service = UserService()