Skip to content
LogoLogo

Redirect (Web)

An app signs a user in on Tempo ID's pages, and completes the sign-in on return.

Overview

The redirect plugin runs an OAuth authorization code flow with PKCE in the page's own window: the browser leaves for Tempo ID's pages, and returns to the redirect URI with a code. It needs no registration.

Among the sign-in flows, choose it over the dialog for a full-page sign-in with no frame or popup. To keep the session on a server, use the backend flow.

The demo leaves this page for Tempo ID's sign-in, returns through the site's root, and shows the user.

Sign in through a redirectDemo

Walkthrough

The steps build the demo's flow in a browser app.

Install the packages

Create the instance

tempo.config.ts
import { Accounts } from 'accounts'
import { redirect } from 'accounts/plugins'
import { react } from 'accounts/react'
 
export const accounts = Accounts.create({
  chain: 'testnet',
  plugins: [react(), redirect()],
})

The page's origin is the client ID, and its root is the default redirect URI.

Sign the user in

redirect.signIn leaves the page. On the return, the first render of accounts.useSession redeems the code, saves the session, and removes the callback's parameters from the address bar. Read the session before any routing that reads the URL.

import { accounts } from './tempo.config.js'
 
export function App() {
  const { isPending, session } = accounts.useSession() 
 
  // The sign-in completes, or the saved session loads.
  if (isPending) return <p>Loading…</p>
 
  if (!session)
    return (
      <button onClick={() => accounts.redirect.signIn()} type="button">
        Sign in with Tempo
      </button>
    )
 
  return <p>Signed in as {session.user.email}</p>
}

Sign the user out

signOut ends the app's session. The browser keeps its Tempo ID session, so the next sign-in returns with no prompt.

import { accounts } from './tempo.config.js'
 
export function SignOut() {
  return (
    <button onClick={() => accounts.signOut()} type="button">
      Sign out
    </button>
  )
}

Use cases

Each use case sets one option of redirect.signIn, of the redirect plugin, or of the instance.

Request an access key

accessKey requests an access key with the sign-in. The user approves its limits on Tempo ID's page, so they may exceed the headless ceiling of a sign-in with no page. Without it, the sign-in takes the instance's accessKey, if the instance sets one.

import { accounts } from './tempo.config.js'
 
export function SignIn() {
  return (
    <button
      onClick={() =>
        accounts.redirect.signIn({
          // The access key sends up to 100 OUSD for a day.
          accessKey: {
            expiry: '1d',
            limits: [{ limit: '100', token: 'ousd' }],
          },
        })
      }
      type="button"
    >
      Sign in with Tempo
    </button>
  )
}

Bind a challenge

challenge has the user sign a server's message at the sign-in. On the return, the session's challengeSignature holds the signature, which the server checks, as Sign challenges shows.

import { accounts } from './tempo.config.js'
 
export function SignIn() {
  async function signIn() {
    // The server issues the message to sign.
    const response = await fetch('/challenge', { method: 'POST' })
    await accounts.redirect.signIn({ challenge: await response.text() }) 
  }
 
  return (
    <button onClick={signIn} type="button">
      Sign in with Tempo
    </button>
  )
}

Ask for funds

funding names the amount that the app needs in the user's Tempo Account. If the account holds less, Tempo ID's deposit page opens after the approval, before the return.

import { accounts } from './tempo.config.js'
 
export function SignIn() {
  return (
    <button
      onClick={() =>
        accounts.redirect.signIn({
          funding: { amount: '10', currency: 'USD' }, 
        })
      }
      type="button"
    >
      Sign in with Tempo
    </button>
  )
}

Fill in the email

loginHint fills in the email field of Tempo ID's sign-in page, and the user can still change it. It defaults to the saved session's email, so an app passes it only for a session that ended, such as after a refused refresh.

import { accounts } from './tempo.config.js'
 
export function SignIn() {
  return (
    <button
      onClick={() =>
        accounts.redirect.signIn({
          loginHint: 'alice@example.com', 
        })
      }
      type="button"
    >
      Sign in with Tempo
    </button>
  )
}

Sign in for an API

resource names a registered resource, such as an MCP server. Each access token then names it as its audience, so only that API accepts the token.

import { accounts } from './tempo.config.js'
 
export function SignIn() {
  return (
    <button
      onClick={() =>
        accounts.redirect.signIn({
          resource: 'https://mcp.example.com/mcp', 
        })
      }
      type="button"
    >
      Sign in with Tempo
    </button>
  )
}

Push each request

par: true sends each request to Tempo ID first, so the URL holds only a reference to it, as Pushed authorization requests describes. A pushed request names at most one resource.

tempo.config.ts
import { Accounts } from 'accounts'
import { redirect } from 'accounts/plugins'
import { react } from 'accounts/react'
 
export const accounts = Accounts.create({
  chain: 'testnet',
  plugins: [react(), redirect({ par: true })], 
})

Return to another page

Tempo ID returns only to a redirect URI that matches one of the app's exactly. For another URI, it shows an error page.

Client IDRedirect URIs of the app
An https originThe root, or the redirect_uris of its web app manifest
A localhost originEach path of the origin, on the same port
The client ID of an OAuth appThe redirect URIs of its registration

To return to another page of an https origin, list it in the web app manifest that the origin's root links. The list replaces the root, so keep the root in it if the app uses it.

manifest.webmanifest
{
  "redirect_uris": ["https://app.example/", "https://app.example/callback"]
}

Pass the same URI as redirectUri, and render accounts.useSession on that page.

tempo.config.ts
import { Accounts } from 'accounts'
import { redirect } from 'accounts/plugins'
import { react } from 'accounts/react'
 
export const accounts = Accounts.create({
  chain: 'testnet',
  plugins: [react(), redirect()],
  redirectUri: 'https://app.example/callback', 
})

Next steps