browserClient

tcmts.atproto.browserClient · #4mnumjxw · function · runs in the browser

uses location

This page as an OAuth client: the loopback one on localhost (no document), else the document at client. Either comes back to the app's home page.

import { askedScope } from "#codebase/tcmts/atproto";
import type { SignInOptions } from "#codebase/tcmts/atproto";
import { href } from "#codebase/tcmts/atproto/lib/react";

export function browserClient(o: SignInOptions): { client_id: string; redirect_uri: string } {
  const local = location.hostname === "localhost" || location.hostname === "127.0.0.1";
  // (a loopback client must come back to 127.0.0.1, not localhost)
  const redirect = `${location.origin.replace("//localhost", "//127.0.0.1")}${href("/")}`;
  return local
    ? {
        client_id: `http://localhost?redirect_uri=${encodeURIComponent(redirect)}&scope=${encodeURIComponent(askedScope(o))}`,
        redirect_uri: redirect,
      }
    : {
        client_id: `${location.origin}${href(o.client ?? "/oauth/client")}`,
        redirect_uri: redirect,
      };
}

uses

used by