Skip to Content
Otoko Identity

Browser app

In the wizard: No API that Otoko Identity calls · Web app, no server code at sign-in

A single-page app (SPA) with no server of its own, such as React, Vue or Angular served as static files. It signs people in from the browser with PKCE, so it holds no secret. Otoko Identity (OI) runs sign-up, forgot password and invitations on its own pages, and you manage roles in OI's console. For a new app with a server, signing in from the server is recommended: see Which kind of app?.

What you'll build

  • A Sign in button that sends people to OI's sign-in page, which also has Register and Forgot password.
  • A /callback route, where OI sends people back and the app swaps the code for tokens.
  • A front page with the signed-in person's name, email and roles, and a Sign out button.

It uses oidc-client-ts, an OpenID Connect library for the browser, in every framework.

Trying it with Otoko Identity on your own machine?

The examples use https://identity.example.com for Otoko Identity. With OI running locally in Docker, its address is http://localhost:8001 and the console is at http://localhost:3100/admin. Your app runs on localhost as usual.

With a coding agent

To have Claude Code, Cursor or another coding agent do these steps in your project, paste it this prompt. Check what it writes against the steps below.

Agent prompt

Paste this into your coding agent, in your single-page app's project. It tells you what to register on the console, then writes sign-in, the callback and sign-out in the browser with oidc-client-ts, with no backend.

Show the full prompt

Connect this browser app's sign-in to Otoko Identity

You are connecting the sign-in of the single-page app (SPA) you're working in to Otoko Identity (OI), a standard OpenID Connect provider. The app has no server or backend of its own: it signs people in from the browser with PKCE, and holds no secret. Work through the steps below in order, and stop where a step says to wait for the person.

Scope

  • A Sign in button that sends people to OI, a /callback route where the app swaps the code for tokens, a front page with the signed-in person's name, email and roles, and a Sign out button.
  • OI runs sign-up, forgot password and invitations on its own pages, and roles are managed on OI's console. Don't build them.
  • Change nothing unrelated to signing in.

Rules

  • A browser app has no secrets. Never put a client secret, an API key or a webhook secret in it: anything in the page can be read.
  • Use oidc-client-ts, the OpenID Connect library this setup is written for, in any framework. Never write the flow by hand.
  • Keep the tokens where oidc-client-ts puts them, in sessionStorage. Never move them to localStorage.
  • Use https everywhere outside a local machine.

1. Look at the project

2. Tell the person what to register, then wait

The app is registered on OI's console; you can't do this part. Tell the person to open https://identity.otoko.com/admin/apps/new and:

  1. Name your app: type the App name. To Does your app have an API that Otoko Identity calls? answer No.
  2. How people sign in: choose Web app, and to Does your app run server code when someone signs in? answer No. Type the App address, the dev address you found. Open Advanced and change the Sign-in return address to the app address plus /callback, such as http://localhost:5173/callback. Leave the Sign-out return address as the front page.
  3. Email: leave Send this app's emails through Otoko Identity's mail server ticked, with an optional Reply-to address. Only for an app with its own mail server, untick it, type the From address, and fill in the Mail server section that appears: host, port, encryption, username and password. The person types the password there themselves; never ask for it in the chat.
  4. Choose Create, then on Connect your app the Frontend section's Copy all. A browser app never uses the Server section's API key, or any other value outside Frontend: leave them out.
  5. Turn on sign-up: a new client starts with it off. Open Apps, the app, the Clients tab and the client, turn on Allow sign-up, and choose Save client.
  6. Add a role: on the app's Users & roles tab, set Roles managed by to Admin console, choose Add role, type member, and choose Save changes.

Ask them to tell you when it's done, and to give you the Frontend section's values, none of which is secret: IDENTITY_ISSUER, IDENTITY_CLIENT_ID, IDENTITY_REDIRECT_URI and IDENTITY_SCOPES. Wait.

3. Settings

The settings are built into the page. Add roles to the scopes, so the app can read roles.

  • Vite (React, Vue): .env.local, with .env.example beside it, holding VITE_IDENTITY_ISSUER, VITE_IDENTITY_CLIENT_ID, VITE_IDENTITY_REDIRECT_URI and VITE_IDENTITY_SCOPES (such as openid profile email roles). Vite builds only names that start with VITE_ into the page.
  • Angular: src/environments/environment.ts, with identity: { issuer, clientId, redirectUri, scopes }.
  • Another stack: the same four values, with its own public prefix, such as NEXT_PUBLIC_.

4. Install

  • npm install oidc-client-ts, plus a router if the project has none: react-router-dom for React, vue-router for Vue. Angular has its own.

5. The code

The connection to OI, one module everything imports from, such as src/auth/oidc.ts (Angular: an AuthService in src/app/auth/auth.service.ts):

  • A UserManager with authority set to the issuer (it reads OI's discovery document itself), client_id, redirect_uri, post_logout_redirect_uri set to the app's origin plus /, scope, loadUserInfo: true (name, email and roles come from OI's userinfo endpoint), and automaticSilentRenew: false.
  • signIn() calls signinRedirect({ nonce: crypto.randomUUID() }). The library uses the code flow with PKCE (S256) and a state.
  • signOut() calls signoutRedirect(), which sends the ID token as id_token_hint, so OI ends its own session too and returns the browser to the front page.

The signed-in person, shared by every component (a React context and provider, a Vue composable, the Angular service): read getUser() at start, treat an expired user as signed out, and follow the userLoaded, userUnloaded and accessTokenExpired events.

The /callback route:

  • Call signinRedirectCallback() once (React runs effects twice in development, and a code works only once), then go to / with replace, so the used code leaves the address and the history.
  • On an error, go to / if someone is already signed in (a reload or Back on a used callback), and otherwise show the error with a Try again button that calls signIn().

The front page:

  • Signed out: a Sign in button.
  • Signed in: Hello, and the name (or the email), the email, Roles: and the roles from profile.roles (or none), and a Sign out button.

6. Check it works

Run the app (npm run dev, or npm start for Angular) and open it. You can't sign in for the person, so ask them to:

  1. Choose Sign in, then Register on OI's page, and type the code OI emails. They're back on the front page, signed in, with their name and email.
  2. In the console, give themselves the member role, then sign out and in again. The page says Roles: member.
  3. Choose Sign out, then Sign in again. OI asks for their email: its own session ended too.
  4. In the browser's developer tools, under Application, find the tokens in Session storage, and nothing in Local storage.

If OI answers invalid_redirect_uri, the redirect setting and the client's Valid redirect URIs differ. If the browser shows a CORS error from OI, the page's address isn't the origin of one of those redirect addresses. For other errors, read https://identity-docs.otoko.com/troubleshooting.

Finish with a short report: the files you changed, the routes you added, the settings the code reads (by name), and anything the person still has to do, such as adding the live addresses to the client before going live.

More detail

Register the app

  1. In the console, open Connect an app.

  2. Name your app: type the App name. To Does your app have an API that Otoko Identity calls? answer No.

  3. How people sign in: choose Web app, and to Does your app run server code when someone signs in? answer No. Type the App address, such as http://localhost:5173 (Vite) or http://localhost:4200 (Angular). Open Advanced and change the Sign-in return address to the app address plus /callback, such as http://localhost:5173/callback. Leave the Sign-out return address as the front page.

  4. Email:

    Otoko Identity emails sign-up codes, forgot-password codes and invitations. Leave Send this app's emails through Otoko Identity's mail server ticked, and the app needs no mail server of its own; a Reply-to address is optional. Only if your app has its own mail server, untick it, type the From address OI sends from, and fill in the Mail server section that appears: host, port, encryption, username and password, the last two together, and its timeouts. It's saved when the app is made, and the last step offers Send test email.

  5. Choose Create. On Connect your app, choose the Frontend section's Copy all. A browser app never uses the Server section's API key, so leave it out.

  6. Turn on sign-up. A new client starts with it off, and until it's on, OI's sign-in page shows no Register link. Open Apps, your app, the Clients tab, and your client. Turn on Allow sign-up, then choose Save client. (The app's own Sign-up allowed and Forgot password, under Login on its Overview tab, are on for a new app.)

  7. Add a role. On the app's Users & roles tab, set Roles managed by to Admin console (an app with a backend starts on Application backend), choose Add role, type member, and choose Save changes. Once someone has signed up, give them the role with the pencil button in their Roles column in the users list. Your app asks for the roles scope to read them.

Settings

The settings go into the page when it's built. None of them is a secret: a browser app has none.

Copy .env.example to .env.local, and put in the values from the Frontend section's Copy all. Vite builds only names that start with VITE_ into the page. Add roles to the scopes to read roles.

.env.example
# Copy to .env.local. The values come from Connect an app's Frontend section (its Copy all), with VITE_ in front so
# Vite builds them into the page. None is a secret: a browser app has none. Add roles to the scopes to read roles.
VITE_IDENTITY_ISSUER=https://identity.example.com
VITE_IDENTITY_CLIENT_ID=your-client-id
VITE_IDENTITY_REDIRECT_URI=http://localhost:5173/callback
VITE_IDENTITY_SCOPES=openid profile email roles

Install

A new app (skip the first three lines for an app you already have):

npm create vite@latest my-app -- --template react-ts --no-interactive
cd my-app
npm install
npm install oidc-client-ts react-router-dom

Add the code

Where these files go

oidc.ts
useAuth.ts
AuthProvider.tsx
Callback.tsx
App.tsx
main.tsx
  • src/auth/oidc.ts: the connection to OI, with sign-in and sign-out. Everything that signs people in imports from here.
  • src/auth/useAuth.ts and src/auth/AuthProvider.tsx: the signed-in person, for every component.
  • src/pages/Callback.tsx: the /callback route.
  • src/App.tsx and src/main.tsx: the routes, and the provider around the app. These replace the starter's files.
import { UserManager } from 'oidc-client-ts';

// Otoko Identity, through oidc-client-ts. It reads OI's discovery document by itself, signs in with PKCE (no secret),
// and keeps the tokens in this tab's sessionStorage. Never move them to localStorage.
export const userManager = new UserManager({
  authority: import.meta.env.VITE_IDENTITY_ISSUER,
  client_id: import.meta.env.VITE_IDENTITY_CLIENT_ID,
  redirect_uri: import.meta.env.VITE_IDENTITY_REDIRECT_URI,
  post_logout_redirect_uri: `${window.location.origin}/`,
  scope: import.meta.env.VITE_IDENTITY_SCOPES,
  loadUserInfo: true, // name, email and roles come from OI's userinfo endpoint
  automaticSilentRenew: false, // when the access token expires, the person signs in again
});

/** Off to OI's sign-in page, which also has Register and Forgot password. */
export function signIn(): Promise<void> {
  // A fresh nonce ties the ID token to this sign-in. oidc-client-ts checks it when the browser comes back.
  return userManager.signinRedirect({ nonce: crypto.randomUUID() });
}

/** Signs out of the app and of OI, which then sends the browser back to the front page. */
export function signOut(): Promise<void> {
  return userManager.signoutRedirect();
}

Using something else?

The same oidc-client-ts code works in any front end. Put it here:

FrameworkConnection to OISigned-in personCallback route
Svelte (SvelteKit, as a static app)src/lib/auth/oidc.tsa store in src/lib/auth/user.tssrc/routes/callback/+page.svelte
Solidsrc/auth/oidc.tsa context in src/auth/AuthProvider.tsxsrc/routes/callback.tsx
Next.js, exported as a static sitelib/oidc.tsa context in app/providers.tsxapp/callback/page.tsx, a client component
Plain JavaScriptauth.jsa variable in auth.jscallback.html, which calls signinRedirectCallback()

Run and check

npm run dev

Open http://localhost:5173.

  1. Choose Sign in. OI's sign-in page opens.
  2. Choose Register, fill in the form, and type the code OI emails you. You're back on the front page, signed in, with your name and email.
  3. In the console, give yourself the member role, then sign out and in again. The page says Roles: member.
  4. Choose Sign out, then Sign in again. OI asks for your email: its own session ended too.
  5. In the browser's developer tools, under Application, the tokens are in Session storage, and nothing is in Local storage.

When the access token expires, the page shows Sign in again. While OI's own session is still open, signing in comes straight back without a password.

Before going live

  • Use https for your app and for Otoko Identity. Only localhost may use http.
  • Add your live addresses to the client, on its page in the console: the sign-in return address under Valid redirect URIs, and the front page under Valid post-logout redirect URIs. They're compared exactly, character for character.
  • Build once per environment. The settings are built into the page, so each environment (staging, live) gets its own build with its own values.
  • If you set a Content Security Policy, allow connect-src to Otoko Identity's address, as the page calls its token and userinfo endpoints.
  • Keep secrets out of the browser. A browser app has no client secret, and never holds an API key: anything in the page can be read.
  • Keep the tokens where oidc-client-ts puts them, in sessionStorage. Never move them to localStorage.

If something goes wrong

What you seeWhyWhat to do
OI answers invalid_redirect_uri instead of showing its sign-in pageThe sign-in return address isn't on the client's list, exactlyMake VITE_IDENTITY_REDIRECT_URI (or redirectUri) and the client's Valid redirect URIs the same, character for character.
The browser's console shows a CORS error from OIThe page's address isn't the origin of one of the client's redirect addressesAdd the app's real address to the client's Valid redirect URIs.
/callback says No matching state found in storageThe sign-in started in another tab or browser, or the storage was clearedChoose Try again.
OI's sign-in page has no Register linkAllow sign-up is off on the client, or Sign-up allowed is off on the appTurn them on, as in step 1.
Roles: noneroles isn't in the scopes, the person has no role yet, or they haven't signed in since getting itAdd roles to the scopes, give the role in the console, and sign in again.
Signed out after a whileThe access token expiredSign in again. This quickstart doesn't renew tokens in the background.

Tested with React 19.3.1, Vite 8.3.6, react-router-dom 7.18.6 · Vue 3.5.44, vue-router 5.5.1 · Angular 22.3.0 · oidc-client-ts 3.5.0 · TypeScript 6.0 and 7.0.