> ## 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.

# Adding WorkOS AuthKit to an Existing App

Use the `react-vite-authkit` starter (`bijection create --template react-vite-authkit`, described in [First protected application](https://docs.bijection.com/access/quickstart.md)) as a reference.
Its `bijection/sign-in.ts` configures the WorkOS issuers and keys from
`WORKOS_CLIENT_ID`. Match the configuration to the issuer, audience and
JWKS of the token your application receives.

In the frontend, create one `authkit()` adapter for your `AuthKitProvider`.
Wire the provider callbacks and feed the same adapter `useAuth()` from a
component inside the provider.

Declare the WorkOS client ID in the frontend's Vite environment types.

```ts vite-env.d.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
interface ImportMetaEnv {
  readonly VITE_WORKOS_CLIENT_ID: string;
}
```

```tsx theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { AuthKitProvider, useAuth } from "@workos-inc/authkit-react";
import { authkit, connect } from "@bijectionhq/bijection/client";
import { useClientAuth } from "@bijectionhq/bijection/react";

export const auth = authkit();
export const client = connect(import.meta.env.VITE_BIJECTION_URL, { contract: "discover", token: auth });

function App() {
  const state = useAuth();
  const ready = useClientAuth(auth, state);
  function signOut() {
    auth.clear();
    state.signOut();
  }
  return ready && state.user ? <button onClick={signOut}>Sign out</button> : null;
}

export default function Root() {
  return (
    <AuthKitProvider
      clientId={import.meta.env.VITE_WORKOS_CLIENT_ID}
      onRefresh={auth.onRefresh}
      onRefreshFailure={auth.clear}
    >
      <App />
    </AuthKitProvider>
  );
}
```

AuthKit's `useAuth()` provides the user and loading state. Its `onRefresh`
callback provides the session and audience evidence in the access token.
Forward every callback synchronously if you compose it with your own handler.
The adapter supplies no authenticated credential until it has matching
evidence. The access token must identify an issuer, subject, and session, and
carry an application client ID or an audience. The adapter withdraws watch
answers when the token's issuer, subject, session, application client,
audience set, or organization changes. A routine refresh in the same scope
preserves the watch. Call `auth.clear()` before signing out. Create a new
adapter and client if you replace the provider.

In a server request, create a client with the user's token using
`connect(url, { contract: "discover", token })`.

A verified session identifies the caller. The app's labels, `allow` roles and
grants control their data access, and its guards can refuse the operations
they name. Follow [Grants](https://docs.bijection.com/access/grants.md)
for first-owner and membership setup.

The [AuthKit notes example](https://github.com/bijectionhq/bijection/tree/main/examples/authentication-authkit)
declares its owner role in `app(tree, { founders })`. Choose the founder
when creating the installation. After signing in, the founder can invite
other members through the app.


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