Skip to content

Commit

Permalink
hotfix auth upsert + apn response
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Nov 1, 2024
1 parent 11a8b24 commit f34a999
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/apn.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def generate_jwt_token(key_id: str, team_id: str, private_key: str) -> str:
return jwt.encode(payload, private_key, algorithm="ES256", headers=headers)


def send_push_notification(device_token: str, title: str, body: str) -> dict:
def send_push_notification(device_token: str, title: str, body: str):
"""
Send a push notification to a user's device.
Expand Down Expand Up @@ -71,4 +71,4 @@ def send_push_notification(device_token: str, title: str, body: str) -> dict:
logging.error(f"APNs error: {response.status_code}, {error_payload}")
raise ValueError(f"APNs rejected the request: {error_payload}")

return response.json()
return response
4 changes: 4 additions & 0 deletions src/auth_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from src.email_manager import send_alert_email
from src.supabase_client import (
get_device_token,
get_user_auth,
upsert_user,
upsert_user_auth,
Expand Down Expand Up @@ -79,12 +80,14 @@ def refresh_and_update_user_token(athlete_id: int, refresh_token: str) -> UserAu
new_jwt_token = generate_jwt(
athlete_id=athlete_id, expires_at=access_info["expires_at"]
)

user_auth = UserAuthRow(
athlete_id=athlete_id,
access_token=access_info["access_token"],
refresh_token=access_info["refresh_token"],
expires_at=access_info["expires_at"],
jwt_token=new_jwt_token,
device_token=get_device_token(athlete_id),
)
upsert_user_auth(user_auth)
return user_auth
Expand Down Expand Up @@ -136,6 +139,7 @@ def authenticate_with_code(code: str) -> UserAuthRow:
refresh_token=strava_client.refresh_token,
expires_at=strava_client.token_expires_at,
jwt_token=jwt_token,
device_token=get_device_token(athlete.id),
)
upsert_user_auth(user_auth_row)
return user_auth_row
Expand Down
15 changes: 15 additions & 0 deletions src/supabase_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
from datetime import timedelta, timezone
from typing import Optional

from dotenv import load_dotenv
from postgrest.base_request_builder import APIResponse
Expand Down Expand Up @@ -269,3 +270,17 @@ def update_user_device_token(athlete_id: str, device_token: str) -> None:
client.table("user_auth").update({"device_token": device_token}).eq(
"athlete_id", athlete_id
).execute()


def get_device_token(athlete_id: int) -> Optional[str]:
"""
Get the device token for a user in the database.
:param athlete_id: The athlete's ID
:return: The device token for the user, or None if the user does not exist
"""
try:
user_auth = get_user_auth(athlete_id)
return user_auth.device_token
except ValueError:
return None

0 comments on commit f34a999

Please sign in to comment.