mirror your GitHub repos to tangled.org automatically
1import { Agent } from '@atproto/api'
2import type { OAuthSession } from '@atproto/oauth-client-node'
3
4/**
5 * Resolve the human-readable handle for the authenticated DID.
6 *
7 * `com.atproto.repo.describeRepo` returns the handle the PDS has verified
8 * against the DID document, so it's the authoritative answer and needs no
9 * appview access. Best-effort: a handle is a display convenience, so any
10 * failure (network, unverified handle) returns null rather than blocking the
11 * sign-in flow. Strips a leading `@` if the PDS includes one.
12 */
13export async function resolveHandle(oauthSession: OAuthSession): Promise<string | null> {
14 try {
15 const agent = new Agent(oauthSession)
16 const { data } = await agent.com.atproto.repo.describeRepo({ repo: oauthSession.did })
17 const handle = data.handle?.trim().replace(/^@/, '')
18 if (!handle || handle === 'handle.invalid') return null
19 return handle
20 }
21 catch {
22 return null
23 }
24}