oauth
@tynd/plugin-oauth starts a listener on 127.0.0.1, captures the provider’s redirect query and returns the parsed params. It runs in the Bun backend and tears itself down after the redirect. Installing it adds the network host feature.
Install
Section titled “Install”tynd add oauthor bun add @tynd/plugin-oauth and list it in tynd.config.ts:
export default { plugins: ["@tynd/plugin-oauth"],}Install the required plugin:
tynd add shellimport { oauth } from "@tynd/plugin-oauth";import { shell } from "@tynd/plugin-shell/client";
const state = crypto.randomUUID();const flow = await oauth.start({ path: "/callback", expectedState: state });
await shell.open(`https://provider.example/authorize?client_id=ID&redirect_uri=${encodeURIComponent(flow.redirectUri)}&state=${state}`);
const { params } = await flow.wait({ timeoutMs: 120_000 });console.log(params.code);oauth.start(opts?): Promise<OAuthFlow>
| Option | Default | Notes |
|---|---|---|
port |
0 (OS-picked) |
Pin a port when the provider needs a fixed redirect_uri |
path |
"/" |
Single leading /, ASCII, at most 256 bytes, no ? or # |
successHtml |
built-in | HTML shown in the browser; at most 4 KiB and ASCII only |
expectedState |
none | CSRF nonce. A callback with a missing, duplicated or different state gets HTTP 400 and surfaces as oauth:error |
An OAuthFlow has id, port, redirectUri, wait({ timeoutMs? }) (default 5 minutes) and cancel(). oauth.list() returns the active flow ids.
Security
Section titled “Security”- Always pass
expectedStatewhen the flow usesstate. successHtmlis served raw. Never put provider responses, query parameters or other untrusted data in it. The response carries a strictContent-Security-Policythat blocks scripts and remote loads as defense in depth.- Loopback only. Only
GETcompletes a callback (other methods get 405 and leave the flow active), and eachstart()accepts one callback. - Call
flow.cancel()when the user abandons the flow. Otherwise the listener stays until thewait()timeout cancels it; a flow nobody waits on is closed 10 minutes afterstart(). At most 16 flows run at once.