Skip to main content

Better Auth

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

Choreo Identity & Access Management provider helper for the Better Auth Generic OAuth plugin. It returns a typed GenericOAuthConfig for a Choreo Identity & Access Management issuer, so there is no Choreo Identity & Access Management SDK dependency and no protocol logic to keep in sync.

  • @thunderid/better-auth
  • Apache 2.0 licence

Better Auth docs

Get started​

Works with a new project or one you already have. Every path below is relative to the project root, and the App Router layout is what the sample uses. Requires Node.js 18 or later and a Choreo Identity & Access Management application with an OAuth 2.0 or OIDC (authorization_code) client.

1. Start from a Next.js app​

Create one, or skip this step and use the app you already have. An existing App Router project needs no restructuring.

npm

npx create-next-app@latest my-app --typescript --app
cd my-app

Yarn

yarn create next-app my-app --typescript --app
cd my-app

pnpm

pnpm create next-app my-app --typescript --app
cd my-app

Bun

bunx create-next-app@latest my-app --typescript --app
cd my-app

Deno

deno run -A npm:create-next-app@latest my-app --typescript --app
cd my-app

2. Install the packages​

Better Auth does the OAuth 2.0 and OIDC work; the helper supplies the Choreo Identity & Access Management configuration.

npm

npm install better-auth @thunderid/better-auth

Yarn

yarn add better-auth @thunderid/better-auth

pnpm

pnpm add better-auth @thunderid/better-auth

Bun

bun add better-auth @thunderid/better-auth

Deno

deno add npm:better-auth npm:@thunderid/better-auth

3. Add the environment variables​

THUNDERID_CLIENT_ID and THUNDERID_CLIENT_SECRET come from the application's Credentials tab in the Choreo Identity & Access Management console. BETTER_AUTH_URL is the base URL this app is served from, and the callback URL is derived from it.

.env
BETTER_AUTH_SECRET=<run: openssl rand -base64 32>
BETTER_AUTH_URL=http://localhost:3000
THUNDERID_ISSUER=https://<DATA_PLANE_URL>
THUNDERID_CLIENT_ID=<your-client-id>
THUNDERID_CLIENT_SECRET=<your-client-secret>
note

Against a Choreo Identity & Access Management instance with a self-signed certificate, local development also needs NODE_TLS_REJECT_UNAUTHORIZED=0. It disables TLS verification for the whole process, so never set it outside local development.

4. Create the Better Auth server instance​

This is the file that wires Choreo Identity & Access Management in: thunderid() returns the provider configuration, and the Generic OAuth plugin registers it. nextCookies() must come last in the plugin array, since it only applies cookies set by plugins before it.

lib/auth.ts
import {thunderid} from '@thunderid/better-auth';
import {betterAuth} from 'better-auth';
import {nextCookies} from 'better-auth/next-js';
import {genericOAuth} from 'better-auth/plugins';

export const auth = betterAuth({
// Point this at your own database. See better-auth.com/docs/adapters.
database: yourDatabaseAdapter,
plugins: [
genericOAuth({
config: [
thunderid({
clientId: process.env.THUNDERID_CLIENT_ID!,
clientSecret: process.env.THUNDERID_CLIENT_SECRET!,
issuer: process.env.THUNDERID_ISSUER!,
}),
],
}),
nextCookies(),
],
});

5. Mount the route handler​

A single catch-all route exposes every Better Auth endpoint, including the callback the next step registers.

app/api/auth/[...all]/route.ts
import {toNextJsHandler} from 'better-auth/next-js';
import {auth} from '../../../../lib/auth';

export const {GET, POST} = toNextJsHandler(auth);

6. Register the redirect URI​

Add this callback URL to the same Choreo Identity & Access Management application. The path is fixed by the route handler above and the provider id, so only the origin changes between environments.

http://localhost:3000/api/auth/callback/thunderid

7. Create the client​

No client plugin is needed. The Generic OAuth plugin exposes sign-in through the standard social-provider API.

lib/auth-client.ts
import {createAuthClient} from 'better-auth/react';

export const authClient = createAuthClient();

8. Sign in and out from a component​

Any client component can read the session and start the flow.

app/page.tsx
'use client';

import {authClient} from '../lib/auth-client';

export default function Home() {
const {data: session, isPending} = authClient.useSession();

if (isPending) return null;

if (!session) {
return (
<button onClick={() => authClient.signIn.social({provider: 'thunderid', callbackURL: '/'})}>
Sign in with Choreo Identity & Access Management
</button>
);
}

return (
<>
<p>Signed in as {session.user.email}</p>
<button onClick={() => authClient.signOut()}>Sign out</button>
</>
);
}

Options​

Every option other than issuer is passed through from Better Auth's BaseOAuthProviderOptions and behaves exactly as it does for the built-in provider helpers.

See the full API reference for every export, its signature and an example.

Functions​

  • thunderid() (function): Returns a typed GenericOAuthConfig for a Choreo Identity & Access Management issuer.

Was this page helpful?