@happyview/oauth-client-browser
v1.4.0
Published
HappyView OAuth client for browser-based ATProto DPoP authentication
Readme
@happyview/oauth-client-browser
Browser OAuth client for authenticating with a HappyView instance using AT Protocol.
Built on top of @happyview/oauth-client with Web Crypto and localStorage adapters included.
Installation
npm install @happyview/oauth-client-browserUsage
Setup
import { HappyViewBrowserClient } from "@happyview/oauth-client-browser";
const client = new HappyViewBrowserClient({
instanceUrl: "https://happyview.example.com",
clientKey: "hvc_your_client_key",
});Sign In
Redirects the user to their PDS authorization server:
await client.signIn("alice.bsky.social");
// User is redirected to their PDS for authorizationOr sign in via a popup:
const session = await client.signIn("alice.bsky.social", {
display: "popup",
});If you need the authorization URL without an immediate redirect, use prepareLogin:
const { authorizationUrl, did, state } =
await client.prepareLogin("alice.bsky.social");Initialization
On page load, call init() to restore a session or process an OAuth callback:
const result = await client.init();
if (result) {
const { session } = result;
// User is logged in
}Authenticated Requests
The session's fetchHandler attaches DPoP proof headers automatically. Pass it a path (relative to the HappyView instance) or a full URL:
const response = await session.fetchHandler(
"/xrpc/com.example.getStuff?limit=10",
{ method: "GET" },
);Using with @atproto/api
HappyViewSession works directly with @atproto/api's Agent:
import { Agent } from "@atproto/api";
const result = await client.init();
if (result) {
const agent = new Agent(result.session);
const profile = await agent.getProfile({ actor: agent.did });
}Revoke Session
await client.revoke("did:plc:abc123");Exports
This package re-exports everything from @happyview/oauth-client, plus:
HappyViewBrowserClient-- the main browser clientLocalStorageAdapter--StorageAdapterbacked bywindow.localStorageWebCryptoAdapter--CryptoAdapterbacked by the Web Crypto APIresolveHandleToDid-- resolve an AT Protocol handle to a DIDresolveDidDocument-- fetch a DID documentresolvePdsUrl-- extract the PDS URL from a DID documentresolveAuthServerMetadata-- fetch OAuth authorization server metadata from a PDS
