> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect and authenticate

> Configure the installation URL and pass the signed-in person's token to the client

Make one client for the app's installation. The Vite starter reads the URL from
`VITE_BIJECTION_URL`:

```ts apps/main/src/client.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api, connect } from "@bijectionhq/bijection/client";
import type App from "../../../bijection/app";

export const app = api<typeof App>();
export const client = connect(import.meta.env.VITE_BIJECTION_URL, { contract: "discover" });
```

Browsers can read their bundled environment variables, so put only public
configuration there, such as the installation URL.

## Sign people in

`connect(url, { contract: "discover", token })` takes an auth adapter. The client asks it for the
current token on every call and every live connection, and reconnects when
the adapter reports a sign-in, a sign-out or a switch. The adapters are on
`@bijectionhq/bijection/client`:

| Adapter | Provider | Feed it |
| - | - | - |
| `authkit()` | WorkOS AuthKit | `useAuth()` from `@workos-inc/authkit-react` |
| `clerk({ template })` | Clerk | `useAuth()` from `@clerk/clerk-react`; `template` names the JWT template the installation verifies |
| `bijectionAuth()` | Bijection Auth | `createAuthSession(url)` snapshot from `@bijectionhq/library-auth/client` |

```ts apps/main/src/auth.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api, authkit, connect } from "@bijectionhq/bijection/client";
import type App from "../../../bijection/app";

export const auth = authkit();
export const app = api<typeof App>();
export const client = connect(import.meta.env.VITE_BIJECTION_URL, { contract: "discover", token: auth });
```

Feed the adapter its provider's state with `useClientAuth` inside the
provider. Render the caller-specific subtree once the hook returns true.

```tsx apps/main/src/App.tsx theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { useAuth } from "@workos-inc/authkit-react";
import { Bijection, useClientAuth } from "@bijectionhq/bijection/react";
import { auth, client } from "./auth";

export default function App() {
  const state = useAuth();
  const ready = useClientAuth(auth, state);
  if (state.isLoading || !ready) return <p>Connecting…</p>;
  return state.user ? <Bijection client={client}><Notes /></Bijection> : <p>Sign in to continue.</p>;
}
```

The adapter waits while the provider restores its session, and answers no
token when nobody is signed in: the client then calls as the signed-out
principal, which holds only the `role.anyone` class grant.

`bijectionAuth()` signs HTTP calls and watches with the browser's session key.
Pass its token adapter and HTTP proof signer to `connect`.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const auth = bijectionAuth();
const client = connect(url, {
  contract: "discover",
  token: auth,
  httpProof: auth.httpProof,
});
```

## Use a local development principal

Run `bijection dev --as person:alice` and open the loopback app URL it prints.
The browser and CLI calls with `--as person:alice` use the same person id in
that installation. In a test installation, `k.as("person:alice")` acts as
the same person, and `person:alice` is the id to use for grants.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { connect, development } from "@bijectionhq/bijection/client";

const client = connect(url, {
  contract: "discover",
  token: import.meta.env.DEV ? development("alice") : auth,
});
```

The app server supplies a short-lived token for its own local installation.
Plain `bijection dev` selects `person:developer`. Production uses the configured
auth adapter.

## Servers, scripts and other providers

A server or a script can pass a fixed token or an async callback that renews
the token within one authorization scope. Make one client per identity.

For another provider, implement `ClientAuth`. Have `token({ refresh })`
return the current token or return `null` after sign-out. Reject the request
if a signed-in credential is unavailable. Use `subscribe(changed)` to
notify the client synchronously when the principal, session, or audience
changes.

## Accept pending invitations

`acceptPendingInvitations(auth, accept, onError)` runs `accept` each time a
person signs in, for an app that mounts the members library:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { acceptPendingInvitations } from "@bijectionhq/library-members/client";

acceptPendingInvitations(auth, () => client.mutate(app.team.acceptPending));
```

The callback queues contact discovery for the signed-in person. Read a
returned receipt with `team.acceptance` to follow the lookup and its
redemptions. The helper returns a cleanup function.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.