go-auth
GuidesClient

OAuth linking

oauthApi — link, unlink, and list connected OAuth providers on an existing account, built on the apiRequest helper.

OAuth linking client

Wraps every endpoint from the OAuth linking guide into oauthApi, built on the apiRequest helper.

import { apiRequest } from "./client";

const API_BASE = "https://api.myapp.com";

export const oauthApi = {
  link: async (provider: string) => {
    const { url } = await apiRequest(API_BASE, "POST", `/auth/oauth/${provider}/link`);
    window.location.href = url;
  },

  unlink: (provider: string) =>
    apiRequest(API_BASE, "POST", `/auth/oauth/${provider}/unlink`),

  listConnected: () => apiRequest(API_BASE, "GET", "/auth/oauth/providers"),
};

Using it

// Settings page — "Connect GitHub" button
<button onClick={() => oauthApi.link("github")}>Connect GitHub</button>
try {
  await oauthApi.unlink("github");
} catch (err) {
  if (err.error === "cannot_unlink_last_provider") {
    // prompt the user to set a password first — see the Security client
  }
}
const { providers } = await oauthApi.listConnected();
// providers.map(p => p.provider) — e.g. ["github", "google"]

Next

  • Setup — the apiRequest helper this is built on
  • OAuth linking — full request/response/error reference
  • Security client — setting a password before unlinking the last provider

On this page