Something went wrong. Try again.
Send a link from your phone's share sheet and have it open automatically on another device.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394// Unified login for the app and the share extension. A Session is what the UI// and public-read paths need; `authedAgent` produces a write-capable Agent for// it. Sign-in is atproto OAuth only — the tokens and DPoP key live in MMKV and// the pointer below in the Keychain, both inside the App Group container, so the// share-extension process resolves the same session the app signed in.
import { resolveIdentity, type Agent } from "@linkport/atproto";import { oauthRestore, oauthSignIn, oauthSignOut } from "./oauth";import { clearOAuthRef, clearStaleCredentials, loadOAuthRef, saveOAuthRef, type StaleReason,} from "./store";
export interface Session { /** Handle or DID — for display and resolving the public-read subject. */ identifier: string; /** PDS base URL, for public (unauthenticated) reads. */ service: string; /** * Set when the grant stopped working and the user has to sign in again. The * session is still worth having in this state — everything public still reads * — so callers that only read can ignore it, and callers that write must not. */ stale?: StaleReason;}
/** The active login, or null when signed out. */export async function loadSession(): Promise<Session | null> { const oauth = await loadOAuthRef(); if (!oauth) { // An install that last signed in with an app password has no OAuth pointer // and can't be restored, so it lands here — drop the orphaned credentials // rather than leaving them in the Keychain indefinitely. await clearStaleCredentials(); return null; } return { identifier: oauth.handle, service: oauth.service, ...(oauth.stale ? { stale: oauth.stale } : {}), };}
/** * A write-capable Agent for the active session, restored from the tokens and * DPoP key in shared MMKV — so this works in the share-extension process too. */export async function authedAgent(): Promise<Agent> { const ref = await loadOAuthRef(); if (!ref) throw new Error("Signed out — no OAuth session stored"); // Known dead: the OAuth client has already deleted the tokens it would restore // from, so attempting it can only fail again, one round-trip later. A `scope` // failure is different — the session itself still works, and only the one // caller that needs the missing scope has a problem — so it passes through. if (ref.stale === "expired") { throw new Error("Your linkport sign-in expired — sign in again"); } // The stored handle spares a getSession round-trip on every restore. return oauthRestore(ref.did, ref.handle);}
/** * Complete the interactive OAuth flow, persist the pointer, return a Session. * * Also how a stale grant is repaired: the ref written below carries no `stale` * field, so a successful sign-in is what clears one. Nothing else needs to. */export async function signInWithOAuth(handle: string): Promise<Session> { const agent = await oauthSignIn(handle); // The OAuth session carries the DID + handle but not the PDS; resolve it once // now so cold starts (and the share extension) need no network to read. const { pds } = await resolveIdentity(agent.did); await saveOAuthRef({ did: agent.did, handle: agent.handle, service: pds }); return { identifier: agent.handle, service: pds };}
/** * Clear the active login, best-effort revoking the OAuth tokens. * * Drops the pointer *before* revoking, not after. Revoking can itself discover * that the session was already dead, which marks the pointer stale — and a * pointer resurrected by the act of signing out would leave the user looking at * "sign in again" when what they asked for was to be signed out. With the * pointer already gone there is nothing left to mark. */export async function signOut(): Promise<void> { const oauth = await loadOAuthRef(); await clearOAuthRef(); if (oauth) await oauthSignOut(oauth.did);}