Web
Sign a user in through Tempo ID's dialog on the page of the app.
Overview
The dialog shows Tempo ID's sign-in in a frame over the app's page, which never sees the user's email code or passkey. It is the default sign-in flow for a web page, and needs no registration. For a full-page sign-in, use a redirect.
The demo signs a user in from a button, shows the user, and signs the user out.
Walkthrough
The web example runs these steps.
Create the instance
An instance with no sign-in plugin uses the dialog, so the code names none.
import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Sign the user in
Call dialog.signIn in a click handler, before any await, since its popup fallback opens only within a click. Without an access key request, the sign-in requests an access key that may transfer up to 5 OUSD for a day.
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<button onClick={() => accounts.dialog.signIn()} type="button">
Sign in with Tempo
</button>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})If the user closes the dialog, or denies the app on the approval page, the call rejects. Errors shows which errors the app ignores.
Read the session
accounts.useSession returns the session, and restores the saved one at the first render, so a later visit opens no dialog.
import { accounts } from './tempo.config.js'
export function App() {
const { isPending, session } = accounts.useSession()
if (isPending) return <p>Checking the session…</p>
if (!session)
return (
<button onClick={() => accounts.dialog.signIn()} type="button">
Sign in with Tempo
</button>
)
return <p>Signed in as {session.user.email}</p>
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Sign the user out
signOut empties the session, and revokes its refresh token. The user stays signed in to Tempo ID.
import { accounts } from './tempo.config.js'
export function SignOut() {
return (
<button onClick={() => accounts.signOut()} type="button">
Sign out
</button>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Use cases
Each use case sets one option of dialog.signIn, or of the dialog plugin.
Request an access key
accessKey requests an access key with the sign-in. The session then signs transactions within these limits, with no prompt. Without it, the sign-in takes the instance's own access key, if the instance sets one.
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<button
onClick={() =>
accounts.dialog.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>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Bind a challenge
challenge has the user sign a server's message at the sign-in. A function fetches the message after the dialog opens, so the click still opens it. The server checks the session's challengeSignature, as Sign challenges shows.
import { accounts } from './tempo.config.js'
export function SignIn() {
async function signIn() {
const session = await accounts.dialog.signIn({
challenge: async (signal) => {
const response = await fetch('/challenge', { method: 'POST', signal })
return response.text()
},
})
await fetch('/verify', {
body: JSON.stringify({
address: session.address,
signature: session.challengeSignature,
}),
headers: { 'content-type': 'application/json' },
method: 'POST',
})
}
return (
<button onClick={signIn} type="button">
Sign in with Tempo
</button>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})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 sign-in. The call resolves once that page returns or closes.
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<button
onClick={() =>
accounts.dialog.signIn({
funding: { amount: '10', currency: 'USD' },
})
}
type="button"
>
Sign in with Tempo
</button>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Fill in the email
loginHint fills in the email field of Tempo ID's sign-in, and the user can still change it. An app passes it for a session that ended, such as after a refused refresh. A browser with a Tempo ID session ignores it.
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<button
onClick={() =>
accounts.dialog.signIn({
loginHint: 'alice@example.com',
})
}
type="button"
>
Sign in with Tempo
</button>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Sign in in a popup
mode: 'popup' shows the sign-in in a popup window, with no frame. The plugin's mode sets it for every sign-in, and loads no frame ahead.
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<button
onClick={() => accounts.dialog.signIn({ mode: 'popup' })}
type="button"
>
Sign in with Tempo
</button>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})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. A sign-in with a resource runs in a popup.
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<button
onClick={() =>
accounts.dialog.signIn({
resource: 'https://mcp.example.com/mcp',
})
}
type="button"
>
Sign in with Tempo
</button>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Cancel a sign-in
signal ends a sign-in from the page, such as after a time limit. Its abort closes the dialog, and rejects the call with the signal's reason.
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<button
onClick={() =>
accounts.dialog.signIn({
// The dialog closes after two minutes without a sign-in.
signal: AbortSignal.timeout(120_000),
})
}
type="button"
>
Sign in with Tempo
</button>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Load the frame at the first sign-in
A page without a session loads the dialog's frame, hidden, as the instance starts, so the first sign-in shows at once. preload: false loads the frame at the first sign-in instead.
import { Accounts } from 'accounts'
import { dialog } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [dialog({ preload: false }), react()],
})Isolate the page from popups
A page that sends the Cross-Origin-Opener-Policy: same-origin header cuts the popup off from the page. crossOriginOpenerPolicy: 'same-origin' then has the page redeem each sign-in's code itself.
import { Accounts } from 'accounts'
import { dialog } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [dialog({ crossOriginOpenerPolicy: 'same-origin' }), react()],
})Push each request
par: true sends each request to Tempo ID first, as Pushed authorization requests describes. A pushed request signs in with a popup only.
import { Accounts } from 'accounts'
import { dialog } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [dialog({ par: true }), react()],
})Keep the session on a server
backend runs the dialog's sign-in through the app's server, which keeps the session, so the page holds no token. Backend shows the server and the page.
import { Accounts } from 'accounts'
import { dialog } from 'accounts/plugins'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [dialog({ backend: true })],
})