- client_id always points to production so PDS can fetch metadata - redirect_uri is dynamic (window.location.origin) so dev login redirects back to localhost instead of production - Add localhost:5173/5174 to allowed redirect_uris in metadata Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { BrowserOAuthClient } from '@atproto/oauth-client-browser'
|
|
import { Agent } from '@atproto/api'
|
|
|
|
// client_id must always point to the publicly accessible metadata file
|
|
// so the PDS can fetch it — even in local dev.
|
|
const PROD_URL = 'https://coffee.apoena.dev'
|
|
|
|
// redirect_uri is dynamic so local dev redirects back to localhost
|
|
const ORIGIN = window.location.origin
|
|
|
|
let _client: BrowserOAuthClient | null = null
|
|
|
|
export function getOAuthClient(): BrowserOAuthClient {
|
|
if (!_client) {
|
|
_client = new BrowserOAuthClient({
|
|
clientMetadata: {
|
|
client_id: `${PROD_URL}/client-metadata.json`,
|
|
client_name: 'Coffee Map',
|
|
client_uri: PROD_URL,
|
|
redirect_uris: [`${ORIGIN}/oauth/callback`],
|
|
grant_types: ['authorization_code', 'refresh_token'],
|
|
response_types: ['code'],
|
|
scope: 'atproto transition:generic',
|
|
dpop_bound_access_tokens: true,
|
|
token_endpoint_auth_method: 'none',
|
|
application_type: 'web',
|
|
},
|
|
handleResolver: 'https://bsky.social',
|
|
})
|
|
}
|
|
return _client
|
|
}
|
|
|
|
export function createAgent(session: Awaited<ReturnType<BrowserOAuthClient['restore']>>): Agent {
|
|
if (!session) throw new Error('No session')
|
|
return new Agent(session)
|
|
}
|