Files
fantasy-watch/FantasyWatch/Shared/Networking/OAuth/OAuthTokenStorage.swift
Michael Simard 1ade3b39ff Initial implementation of Fantasy Hockey watchOS app
Implemented complete TCA architecture for iOS and watchOS targets:
- Authentication flow (Sign in with Apple + Yahoo OAuth)
- OAuth token management with iCloud Key-Value Storage
- Yahoo Fantasy Sports API client with async/await
- Watch Connectivity for iPhone ↔ Watch data sync
- Complete UI for both iPhone and Watch platforms

Core features:
- Matchup score display
- Category breakdown with win/loss/tie indicators
- Roster status tracking
- Manual refresh functionality
- Persistent data caching on Watch

Technical stack:
- The Composable Architecture for state management
- Swift Concurrency (async/await, actors)
- WatchConnectivity framework
- Sign in with Apple
- OAuth 2.0 authentication flow

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 00:40:31 -06:00

60 lines
2.0 KiB
Swift

//
// OAuthTokenStorage.swift
// FantasyWatch
//
// Created by Claude Code
//
import Foundation
actor OAuthTokenStorage {
private let ubiquitousStore = NSUbiquitousKeyValueStore.default
private func accessTokenKey(for userID: String) -> String {
"com.fantasyhockey.user.\(userID).yahoo.accessToken"
}
private func refreshTokenKey(for userID: String) -> String {
"com.fantasyhockey.user.\(userID).yahoo.refreshToken"
}
private func tokenExpiryKey(for userID: String) -> String {
"com.fantasyhockey.user.\(userID).yahoo.tokenExpiry"
}
func saveTokenPair(_ tokenPair: TokenPair, for userID: String) {
ubiquitousStore.set(tokenPair.accessToken, forKey: accessTokenKey(for: userID))
ubiquitousStore.set(tokenPair.refreshToken, forKey: refreshTokenKey(for: userID))
ubiquitousStore.set(tokenPair.expiryDate.timeIntervalSince1970, forKey: tokenExpiryKey(for: userID))
ubiquitousStore.synchronize()
}
func getAccessToken(for userID: String) -> String? {
ubiquitousStore.string(forKey: accessTokenKey(for: userID))
}
func getRefreshToken(for userID: String) -> String? {
ubiquitousStore.string(forKey: refreshTokenKey(for: userID))
}
func getTokenExpiry(for userID: String) -> Date? {
let timestamp = ubiquitousStore.double(forKey: tokenExpiryKey(for: userID))
guard timestamp > 0 else { return nil }
return Date(timeIntervalSince1970: timestamp)
}
func isTokenValid(for userID: String) -> Bool {
guard let expiryDate = getTokenExpiry(for: userID) else {
return false
}
return expiryDate > Date().addingTimeInterval(60)
}
func clearTokens(for userID: String) {
ubiquitousStore.removeObject(forKey: accessTokenKey(for: userID))
ubiquitousStore.removeObject(forKey: refreshTokenKey(for: userID))
ubiquitousStore.removeObject(forKey: tokenExpiryKey(for: userID))
ubiquitousStore.synchronize()
}
}