peace2024 commited on
Commit
2a4b4c6
·
1 Parent(s): 3f8cf16

update profile

Browse files
Files changed (1) hide show
  1. app/auth.py +62 -0
app/auth.py CHANGED
@@ -9,6 +9,8 @@ from app.models import User
9
  import os
10
  import logging
11
  from dotenv import load_dotenv
 
 
12
 
13
  router = APIRouter()
14
  logger = logging.getLogger(__name__)
@@ -22,6 +24,29 @@ ALGORITHM = "HS256"
22
  # Password hashing config
23
  pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  # Request Schemas
27
  class SignUp(BaseModel):
@@ -38,6 +63,43 @@ class Login(BaseModel):
38
  password: str
39
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  @router.post("/auth/signup")
42
  async def signup(data: SignUp, db: AsyncSession = Depends(get_db)):
43
  # Check if user already exists
 
9
  import os
10
  import logging
11
  from dotenv import load_dotenv
12
+ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
13
+ from jose import JWTError
14
 
15
  router = APIRouter()
16
  logger = logging.getLogger(__name__)
 
24
  # Password hashing config
25
  pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
26
 
27
+ security = HTTPBearer()
28
+
29
+ async def get_current_user(token: HTTPAuthorizationCredentials = Depends(security),
30
+ db: AsyncSession = Depends(get_db)):
31
+ credentials_exception = HTTPException(
32
+ status_code=401,
33
+ detail="Could not validate credentials",
34
+ headers={"WWW-Authenticate": "Bearer"},
35
+ )
36
+ try:
37
+ payload = jwt.decode(token.credentials, SECRET_KEY, algorithms=[ALGORITHM])
38
+ user_id: int = payload.get("user_id")
39
+ if user_id is None:
40
+ raise credentials_exception
41
+ except JWTError:
42
+ raise credentials_exception
43
+
44
+ result = await db.execute(select(User).where(User.id == user_id))
45
+ user = result.scalar_one_or_none()
46
+ if user is None:
47
+ raise credentials_exception
48
+ return user
49
+
50
 
51
  # Request Schemas
52
  class SignUp(BaseModel):
 
63
  password: str
64
 
65
 
66
+ class UpdateProfile(BaseModel):
67
+ mobile: str | None = None
68
+ name: str | None = None
69
+ dob: str | None = None
70
+ preparing_for: str | None = None
71
+
72
+
73
+ @router.put("/auth/profile")
74
+ async def update_profile(data: UpdateProfile,
75
+ current_user: User = Depends(get_current_user),
76
+ db: AsyncSession = Depends(get_db)):
77
+ # Update user fields if provided
78
+ if data.mobile is not None:
79
+ current_user.mobile = data.mobile
80
+ if data.name is not None:
81
+ current_user.name = data.name
82
+ if data.dob is not None:
83
+ current_user.dob = data.dob
84
+ if data.preparing_for is not None:
85
+ current_user.preparing_for = data.preparing_for
86
+
87
+ try:
88
+ await db.commit()
89
+ await db.refresh(current_user)
90
+ return {"message": "Profile updated successfully",
91
+ "user": {"id": current_user.id,
92
+ "email": current_user.email,
93
+ "mobile": current_user.mobile,
94
+ "name": current_user.name,
95
+ "dob": current_user.dob,
96
+ "preparing_for": current_user.preparing_for}}
97
+ except Exception as e:
98
+ await db.rollback()
99
+ logger.error(f"Profile update error: {e}")
100
+ raise HTTPException(status_code=500, detail="Internal Server Error")
101
+
102
+
103
  @router.post("/auth/signup")
104
  async def signup(data: SignUp, db: AsyncSession = Depends(get_db)):
105
  # Check if user already exists