🚀 OAuth Migration to @atproto/oauth-client-browser#
Your app has been successfully set up with a new, simplified OAuth implementation using the official @atproto/oauth-client-browser package. This migration will replace ~1000 lines of custom OAuth code with a robust, officially-maintained solution.
📁 New Files Created#
Core OAuth Implementation#
app/src/lib/oauth-client.ts- OAuth client setup and configurationapp/src/lib/auth-context-new.tsx- New auth context using OAuth clientapp/src/lib/api-client.ts- Simplified API calls using OAuth sessions
Updated Pages#
app/src/app/auth/login/page-new.tsx- Simplified login pageapp/src/app/auth/callback/page-new.tsx- Simplified callback handling
Documentation & Scripts#
OAUTH_MIGRATION_GUIDE.md- Detailed migration guidetest-new-oauth.js- Script to test new implementationrestore-oauth.js- Script to restore original implementation
🧪 How to Test the New Implementation#
1. Test the New OAuth System#
# Run the test setup script
./test-new-oauth.js
# Start the development server
cd app && npm run dev
2. Test Authentication Flow#
- Visit http://localhost:3000
- Click Login to go to the new login page
- Test with different handles:
- Bluesky:
yourhandle.bsky.social - Third-party PDS:
handle.geese.blue - Custom domain:
yourhandle.yourdomain.com
- Bluesky:
- Verify the callback completes successfully
- Check that you're authenticated on the home page
3. Test Session Management#
- Sign in and verify it works
- Refresh the page - should stay signed in
- Close and reopen browser - should restore session
- Test sign out - should clear session properly
4. Restore Original (if needed)#
# Restore the original implementation
./restore-oauth.js
✨ Key Benefits of Migration#
Simplified Codebase#
- Removes ~1000 lines of custom OAuth code
- No more manual PKCE flow implementation
- No more custom DPoP token generation
- No more complex nonce handling
- Eliminates custom API routes (
/api/auth/token,/api/auth/nonce)
Better Reliability#
- Official implementation tested across many applications
- Automatic token refresh with proper retry logic
- Better error handling and recovery
- Proper session lifecycle management
Enhanced Security#
- Uses secure IndexedDB storage instead of localStorage
- Follows latest AT Protocol OAuth specifications
- Automatic DPoP implementation
- Better session invalidation handling
Improved Developer Experience#
- Direct integration with
@atproto/apiAgent - Automatic handle resolution
- Built-in support for third-party PDS servers
- Event listeners for session changes
- Better TypeScript support
🔄 Migration Process (When Ready)#
Phase 1: Backup & Prepare#
# Already done - scripts handle this automatically
Phase 2: Switch to New Implementation#
# Replace the auth context import in layout.tsx
# From: '@/lib/auth-context'
# To: '@/lib/auth-context-new'
Phase 3: Update Pages#
# Replace login page
mv app/src/app/auth/login/page.tsx app/src/app/auth/login/page-old.tsx
mv app/src/app/auth/login/page-new.tsx app/src/app/auth/login/page.tsx
# Replace callback page
mv app/src/app/auth/callback/page.tsx app/src/app/auth/callback/page-old.tsx
mv app/src/app/auth/callback/page-new.tsx app/src/app/auth/callback/page.tsx
Phase 4: Update API Calls#
Replace complex API calls throughout your app:
// Before
import { getProfile } from '@/lib/bluesky-api'
const profile = await getProfile(accessToken, keyPair, dpopNonce, handle, pdsEndpoint)
// After
import { getProfile } from '@/lib/api-client'
const profile = await getProfile(session)
Phase 5: Cleanup (After Testing)#
Remove old files when confident in the new implementation:
app/src/lib/bluesky-auth.tsapp/src/lib/auth-context.tsx(old version)app/src/lib/storage-util.tsapp/src/app/api/auth/token/route.tsapp/src/app/api/auth/nonce/route.ts
🛠 Compatibility Notes#
Legacy Code Support#
The new auth context provides backward compatibility:
accessToken→session?.accessTokenrefreshToken→session?.refreshTokendid→session?.subhandle→session?.info?.handlepdsEndpoint→ extracted from session info
Third-Party PDS Support#
Full support maintained for:
- ✅ Bluesky (bsky.social)
- ✅ Custom domains (alice.example.com)
- ✅ Third-party PDS (geese.blue, etc.)
- ✅ Self-hosted instances
Existing API Calls#
Most existing API calls will continue to work during transition period due to legacy compatibility properties.
🐛 Troubleshooting#
If Login Fails#
- Check browser console for errors
- Verify client metadata is accessible at https://flushes.app/client-metadata.json
- Ensure handle resolution is working
- Test with a simple Bluesky handle first
If Session Not Restored#
- Check if IndexedDB is enabled in browser
- Verify no browser extensions blocking storage
- Check for console errors during initialization
If API Calls Fail#
- Verify session object has required properties
- Check if using new API client methods
- Ensure proper error handling for session expiration
📞 Support#
If you encounter any issues:
- Check the logs - The new implementation provides detailed console logging
- Test incrementally - Use the test scripts to verify each step
- Rollback if needed - The restore script quickly reverts changes
- Reference the guide - See
OAUTH_MIGRATION_GUIDE.mdfor detailed steps
🎉 Next Steps#
- Test thoroughly with the new implementation
- Update your components to use the new auth context
- Migrate API calls to use the new client
- Remove old files once confident in the new system
- Enjoy the simplified, more reliable OAuth flow!
The migration significantly reduces complexity while providing better reliability, security, and developer experience. The official @atproto/oauth-client-browser package handles all the OAuth complexity for you.