Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement Account Linking Functionality with Cloudflare KV
Overview
This PR implements account linking functionality for Auth Kit, enabling secure cross-application authentication between OpenGame (provider) and consumer applications. The implementation follows the design outlined in the Account Linking Implementation Plan and introduces role-specific client and server components for both provider and consumer applications.
Key Features
Implementation Details
New Types and Interfaces
LinkedAccount,LinkToken, and other types for account linkingProviderAuthStateandConsumerAuthStateProviderAuthClientandConsumerAuthClientServer-Side Changes
createProviderAuthRouterandcreateConsumerAuthRouterfunctionsClient-Side Changes
createProviderAuthClientandcreateConsumerAuthClientfunctionsReact Integration
Hook Implementation Examples with Cloudflare KV and Zod
Below are examples of how to implement the Auth Kit hooks using Cloudflare KV, with proper class-based Worker structure and Zod for type-safe parsing of data.
Zod Schemas for KV Data
Environment Type Definition
Worker Implementation with KV and Zod
Router Setup Examples
Provider Auth Router with KV
Consumer Auth Router with KV
Cloudflare KV Configuration
To use Cloudflare KV with Auth Kit, configure your
wrangler.toml:Benefits of Using Cloudflare KV with Zod
Global Distribution: KV data is replicated globally, providing low-latency access from any Cloudflare edge location.
Shared State: Unlike Durable Objects, KV allows sharing state across multiple workers and regions, making it ideal for authentication systems.
Type Safety: Zod provides runtime type validation for KV data, ensuring data integrity across distributed systems.
Automatic Expiration: KV supports automatic expiration for items like verification codes and cached profiles.
High Read Performance: KV is optimized for high-performance reads, which is ideal for authentication systems.
Simplicity: KV provides a straightforward key-value API that's easy to use and understand.
Proper Worker Structure: Using
WorkerEntrypointfollows the recommended Cloudflare Workers pattern for class-based workers.API Key Management
For API key management, this implementation supports both environment variables and KV storage:
Environment Variables: Set API keys as secrets in your Cloudflare Worker
KV Storage: Store and retrieve API keys using KV
The implementation prioritizes environment variables for security, falling back to stored keys if needed.
Key Structure for KV
When using KV for auth data, a good key structure helps organize your data:
user:{userId}- User dataemail:{email}- Maps email to userIdverification:{email}- Verification codesaccountLink:{openGameUserId}:{gameId}- Account links from provider perspectivegameLink:{gameId}:{gameUserId}- Account links from consumer perspectiveapiKey:{apiKey}- Maps API keys to game IDsprofile:{openGameUserId}- Cached OpenGame profilesThis structure makes it easy to find and manage related data.
Testing
All tests have been updated and are passing. The implementation includes tests for:
Next Steps
Related Documentation