Vanilla JavaScript Quickstart
Use this guide to add Choreo Identity & Access Management authentication to a browser-based application using the @thunderid/browser SDK. No framework required.
What You Will Learn
- Create a new JavaScript app using Vite
- Install the
@thunderid/browserpackage - Add working sign-in and sign-out
- Display the signed-in user's profile
Prerequisites
- About 15 minutes
- Node.js installed on your system
- npm, yarn, or pnpm
- Your preferred code editor
Create an Application
- Sign in to the Console at https://console.cloud.choreolabs.com/iam/. Select your project in the project switcher in the header.
- Navigate to Applications.
- Click Add application.
- From the Choose a type page, select JavaScript.
- Enter a name (e.g.
My JavaScript App). - Click Create.
- Select how you want to sign in users (e.g., email/password, social login, etc.).
- Select Theme settings and other options.
- In the Configuration step, click on the banner to add Vite's development server URL to the list of Authorized redirect URIs & CORS allowed origins. The default is
http://localhost:5173(adjust the port if your dev server uses a different one).
Copy the Client ID from the General tab. You'll need it when configuring the SDK.
Code in this guide uses https://<DATA_PLANE_URL> for your environment's data plane URL, where users sign in and tokens are issued. Replace it with the URL on the environment's Endpoints page. To open it, select your project, go to Deployment pipeline, and click Open environment on the environment's card. See Control Plane and Data Plane URLs.
Saving a change does not make it live. You author in the control plane, and your users sign in on a separate data plane, so a change takes effect in an environment only after you deploy it. Go to Deploy, deploy the latest version to your first environment, then promote it to the next environment when you are ready.
Auto-sync deploys saved changes to your first environment within seconds. Turning it on is your choice, per project. See Deploy a Version, or learn why you deploy and how auto-sync works.
Create a JavaScript App
Create your new JavaScript app using Vite:
- npm
- Yarn
- pnpm
- Bun
- Deno
npm create vite@latest my-js-app -- --template vanilla
cd my-js-app
npm install
yarn create vite my-js-app --template vanilla
cd my-js-app
yarn install
pnpm create vite my-js-app --template vanilla
cd my-js-app
pnpm install
bun create vite my-js-app --template vanilla
cd my-js-app
bun install
deno run -A npm:create-vite@latest my-js-app --template vanilla
cd my-js-app
deno install
Install @thunderid/browser
Install the Choreo Identity & Access Management Browser SDK in your project:
- npm
- Yarn
- pnpm
- Bun
- Deno
npm install @thunderid/browser
yarn add @thunderid/browser
pnpm add @thunderid/browser
bun add @thunderid/browser
deno add npm:@thunderid/browser
Initialize the SDK
The ThunderIDBrowserClient serves as the main entry point for the SDK. Create a new file to initialize and export the client instance.
Create src/auth.js with the following:
import { ThunderIDBrowserClient } from '@thunderid/browser'
const auth = new ThunderIDBrowserClient()
await auth.initialize({
clientId: '<your-client-id>',
baseUrl: 'https://<DATA_PLANE_URL>',
afterSignInUrl: window.location.origin,
afterSignOutUrl: window.location.origin,
})
export default auth
Replace <your-client-id> with the Client ID you obtained when creating the application in Choreo Identity & Access Management.
Configuration Parameters
| Parameter | Description |
|---|---|
clientId | The Client ID from your Choreo Identity & Access Management application |
baseUrl | Your Choreo Identity & Access Management instance URL (e.g., https://<DATA_PLANE_URL>) |
afterSignInUrl | URL to redirect to after sign-in (defaults to current origin) |
afterSignOutUrl | URL to redirect to after sign-out (defaults to current origin) |
Add Sign-In and Sign-Out
Replace the content of src/main.js with the following to add sign-in and sign-out functionality:
import './style.css'
import auth from "./auth.js"
async function renderApp() {
const isSignedIn = await auth.isSignedIn()
if (isSignedIn) {
const user = await auth.getUser()
document.querySelector('#app').innerHTML = `
<div>
<h1>Choreo Identity & Access Management Auth Demo</h1>
<div class="card">
<h2>Welcome, ${user.displayName || user.username}!</h2>
<div class="user-details">
<p><strong>Email:</strong> ${user.email || 'N/A'}</p>
<p><strong>Username:</strong> ${user.username || 'N/A'}</p>
</div>
<button id="sign-out-btn" type="button">Sign Out</button>
</div>
</div>
`
document.querySelector('#sign-out-btn')
.addEventListener('click', () => auth.signOut())
} else {
document.querySelector('#app').innerHTML = `
<div>
<h1>Choreo Identity & Access Management Auth Demo</h1>
<div class="card">
<p>You are not signed in.</p>
<button id="sign-in-btn" type="button">Sign In</button>
</div>
</div>
`
document.querySelector('#sign-in-btn')
.addEventListener('click', () => auth.signIn())
}
}
renderApp()
Update the HTML Entry Point
Replace the content of index.html to provide a clean entry point:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Choreo Identity & Access Management JavaScript Demo</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
Run Your App
Start the development server:
- npm
- Yarn
- pnpm
- Bun
- Deno
npm run dev
yarn dev
pnpm run dev
bun run dev
deno task dev
Visit your app at http://localhost:5173
You'll need a user to sign in with. If you haven't created one yet, open https://console.cloud.choreolabs.com/iam/, navigate to Users, and add a test user with an email and password.
You should see the sign-in button. Click it to be redirected to the Choreo Identity & Access Management-hosted sign-in page. Authenticate with your test user, then return to your app with the user profile displayed.
Run the Sample App
The JavaScript sample is a working version of the app this guide builds. To run it instead of building the app yourself:
-
Download the sample from its releases and open its folder.
-
Create
.envin the sample's folder with your application's values:.envVITE_THUNDERID_BASE_URL=https://<DATA_PLANE_URL>VITE_THUNDERID_CLIENT_ID=<your-client-id> -
Install the dependencies and start the app:
npm installnpm run dev -
Open http://localhost:5173 and sign in.
What's Next
- Upgrade the Sign-In Flow: Add MFA, passkeys, or social login to your flow. No app code changes required.
- Browser SDK Reference: Explore the full API reference for the Browser SDK.
Was this page helpful?