Update config.py
Browse files
config.py
CHANGED
@@ -6,6 +6,7 @@ import asyncio
|
|
6 |
from pyfcm import FCMNotification
|
7 |
from huggingface_hub import get_secret
|
8 |
from datetime import datetime
|
|
|
9 |
|
10 |
# Logging
|
11 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(name)s - %(message)s")
|
@@ -52,11 +53,21 @@ if not FCM_API_KEY or not SECRET_KEY:
|
|
52 |
# FCM settings
|
53 |
push_service = FCMNotification(api_key=FCM_API_KEY)
|
54 |
|
|
|
|
|
|
|
55 |
async def send_push_notification(recipient_email, message):
|
56 |
try:
|
57 |
-
# Fetch the user's device token
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
if not device_token:
|
61 |
logger.warning(f"No device token found for {recipient_email} at {datetime.utcnow().isoformat()}")
|
62 |
return
|
@@ -73,8 +84,24 @@ async def send_push_notification(recipient_email, message):
|
|
73 |
except Exception as e:
|
74 |
logger.error(f"Failed to send push notification to {recipient_email} at {datetime.utcnow().isoformat()}: {str(e)}")
|
75 |
|
76 |
-
# JWT settings
|
|
|
|
|
|
|
77 |
ALGORITHM = "HS256"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
def setup_app(app: FastAPI):
|
80 |
@app.on_event("startup")
|
|
|
6 |
from pyfcm import FCMNotification
|
7 |
from huggingface_hub import get_secret
|
8 |
from datetime import datetime
|
9 |
+
import httpx
|
10 |
|
11 |
# Logging
|
12 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(name)s - %(message)s")
|
|
|
53 |
# FCM settings
|
54 |
push_service = FCMNotification(api_key=FCM_API_KEY)
|
55 |
|
56 |
+
# Auth Space URL
|
57 |
+
AUTH_SPACE_URL = "https://your-auth-space-name.hf.space/auth"
|
58 |
+
|
59 |
async def send_push_notification(recipient_email, message):
|
60 |
try:
|
61 |
+
# Fetch the user's device token from the auth Space
|
62 |
+
async with httpx.AsyncClient() as client:
|
63 |
+
headers = {"Authorization": f"Bearer {create_access_token(recipient_email)}"}
|
64 |
+
response = await client.get(f"{AUTH_SPACE_URL}/me", headers=headers)
|
65 |
+
if response.status_code != 200:
|
66 |
+
logger.warning(f"Failed to fetch user {recipient_email} from auth Space: {response.text}")
|
67 |
+
return
|
68 |
+
user_data = response.json()
|
69 |
+
device_token = user_data.get("device_token")
|
70 |
+
|
71 |
if not device_token:
|
72 |
logger.warning(f"No device token found for {recipient_email} at {datetime.utcnow().isoformat()}")
|
73 |
return
|
|
|
84 |
except Exception as e:
|
85 |
logger.error(f"Failed to send push notification to {recipient_email} at {datetime.utcnow().isoformat()}: {str(e)}")
|
86 |
|
87 |
+
# JWT settings (minimal implementation for token creation)
|
88 |
+
from datetime import timedelta
|
89 |
+
import jwt
|
90 |
+
|
91 |
ALGORITHM = "HS256"
|
92 |
+
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
93 |
+
|
94 |
+
def create_access_token(email: str):
|
95 |
+
to_encode = {"sub": email, "exp": datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)}
|
96 |
+
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
97 |
+
|
98 |
+
def decode_access_token(token: str):
|
99 |
+
try:
|
100 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
101 |
+
return payload
|
102 |
+
except jwt.PyJWTError as e:
|
103 |
+
logger.error(f"Token decode error: {str(e)}")
|
104 |
+
return None
|
105 |
|
106 |
def setup_app(app: FastAPI):
|
107 |
@app.on_event("startup")
|