added token and details
Browse files- App/Users/Schema.py +6 -1
- App/Users/UserRoutes.py +12 -1
- App/Users/dependencies.py +28 -0
App/Users/Schema.py
CHANGED
@@ -2,6 +2,7 @@ from pydantic import BaseModel, Field, constr, ConfigDict
|
|
2 |
from typing import Optional
|
3 |
from datetime import datetime
|
4 |
from passlib.context import CryptContext
|
|
|
5 |
|
6 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
7 |
|
@@ -12,11 +13,15 @@ MAC_PATTERN = r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
|
|
12 |
|
13 |
class UserResponse(BaseModel):
|
14 |
id: str
|
15 |
-
phoneNumber: str
|
16 |
name: str
|
|
|
|
|
17 |
account_locked: bool
|
18 |
model_config = ConfigDict(from_attributes=True)
|
19 |
|
|
|
|
|
|
|
20 |
|
21 |
# Register User Request
|
22 |
class RegisterUserRequest(BaseModel):
|
|
|
2 |
from typing import Optional
|
3 |
from datetime import datetime
|
4 |
from passlib.context import CryptContext
|
5 |
+
from decimal import Decimal
|
6 |
|
7 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
8 |
|
|
|
13 |
|
14 |
class UserResponse(BaseModel):
|
15 |
id: str
|
|
|
16 |
name: str
|
17 |
+
phoneNumber: str
|
18 |
+
balance: Decimal
|
19 |
account_locked: bool
|
20 |
model_config = ConfigDict(from_attributes=True)
|
21 |
|
22 |
+
# class Config:
|
23 |
+
# orm_mode = True
|
24 |
+
|
25 |
|
26 |
# Register User Request
|
27 |
class RegisterUserRequest(BaseModel):
|
App/Users/UserRoutes.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import APIRouter, HTTPException, status
|
2 |
from .Schema import (
|
3 |
RegisterUserRequest,
|
4 |
LoginUserRequest,
|
@@ -17,6 +17,7 @@ from App.Subscriptions.Model import Subscription
|
|
17 |
from App.Android.Android import AndroidClient
|
18 |
from App.Android.Schema import RegisterUserRequest as AndroidRegister
|
19 |
from App.Templates.Templates import MessageTemplate
|
|
|
20 |
|
21 |
# JWT Configurations
|
22 |
SECRET_KEY = "your_secret_key_here"
|
@@ -173,3 +174,13 @@ async def toggle_user_status(user_id: str):
|
|
173 |
else "User enabled successfully."
|
174 |
)
|
175 |
return BaseResponse(code=200, message=message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, HTTPException, status, Depends
|
2 |
from .Schema import (
|
3 |
RegisterUserRequest,
|
4 |
LoginUserRequest,
|
|
|
17 |
from App.Android.Android import AndroidClient
|
18 |
from App.Android.Schema import RegisterUserRequest as AndroidRegister
|
19 |
from App.Templates.Templates import MessageTemplate
|
20 |
+
from .dependencies import get_current_active_user
|
21 |
|
22 |
# JWT Configurations
|
23 |
SECRET_KEY = "your_secret_key_here"
|
|
|
174 |
else "User enabled successfully."
|
175 |
)
|
176 |
return BaseResponse(code=200, message=message)
|
177 |
+
|
178 |
+
|
179 |
+
@user_router.get(
|
180 |
+
"/user/me", response_model=UserResponse, status_code=status.HTTP_200_OK
|
181 |
+
)
|
182 |
+
async def get_user_details(current_user: User = Depends(get_current_active_user)):
|
183 |
+
"""
|
184 |
+
Get the current user's details and balance.
|
185 |
+
"""
|
186 |
+
return UserResponse.from_orm(current_user)
|
App/Users/dependencies.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import Depends, HTTPException, status
|
2 |
+
from jose import jwt
|
3 |
+
from App.Users.Model import User
|
4 |
+
from fastapi.security import OAuth2PasswordBearer
|
5 |
+
|
6 |
+
SECRET_KEY = "your_secret_key_here"
|
7 |
+
ALGORITHM = "HS256"
|
8 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="user/login")
|
9 |
+
|
10 |
+
|
11 |
+
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
12 |
+
credentials_exception = HTTPException(
|
13 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
14 |
+
detail="Could not validate credentials",
|
15 |
+
headers={"WWW-Authenticate": "Bearer"},
|
16 |
+
)
|
17 |
+
try:
|
18 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
19 |
+
user = await User.get_or_none(phoneNumber=payload.get("sub"))
|
20 |
+
if user is None:
|
21 |
+
raise credentials_exception
|
22 |
+
except jwt.JWTError:
|
23 |
+
raise credentials_exception
|
24 |
+
return user
|
25 |
+
|
26 |
+
|
27 |
+
async def get_current_active_user(current_user: User = Depends(get_current_user)):
|
28 |
+
return current_user
|