Skip to content
LogoLogo

End app sessions

An app signs the user out. A second call revokes the session's access key.

Overview

accounts.signOut() ends the app's session, and revokes its refresh token (RFC 7009). The user's Tempo ID session stays, so other apps stay signed in. The access token and the access key outlive the sign-out, since APIs verify the token on their own, and the chain holds the access key:

Part of the sessionAfter signOut
Session of the appEmpty at once. The saved session is deleted
Refresh tokenRevoked at Tempo ID
Access tokenValid until it expires, at most 5 minutes later
Access keyValid until its expiry, unless the app revokes it
Tempo ID sessionStays. The next sign-in asks for no email address

The demo signs a user in and out of the app, and shows the session's status after each step.

End an app sessionDemo
pnpx gitpick tempoxyz/accounts/tree/main/examples/webSource

Walkthrough

The web example runs these steps in a React app.

Create the instance

The react plugin adds the useSession hook.

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

Render the status

useSession returns the session and its status, such as pending before the restore ends, and renders again at each change. Outside React, session.status holds the same status, and its listener receives the new status and the previous one.

import { accounts } from './tempo.config.js'
 
export function App() {
  const { session, status } = accounts.useSession() 
 
  if (status === 'pending') return <p>Checking the session…</p> 
  if (!session)
    return (
      <button onClick={() => void accounts.dialog.signIn()} type="button">
        Sign in with Tempo
      </button>
    )
  return <p>Signed in as {session.user.email}</p>
}

Sign the user out

signOut empties the session at once, also when the revocation fails.

import { accounts } from './tempo.config.js'
 
export function App() {
  const { session, status } = accounts.useSession()
 
  if (status === 'pending') return <p>Checking the session…</p>
  if (!session)
    return (
      <button onClick={() => void accounts.dialog.signIn()} type="button">
        Sign in with Tempo
      </button>
    )
  return <p>Signed in as {session.user.email}</p> 
  return ( 
    <main> 
      <p>Signed in as {session.user.email}</p> 
      <button onClick={() => void accounts.signOut()} type="button"> 
        Sign out 
      </button> 
    </main> 
  ) 
}

The instance saves a signed-out mark, so a reload restores no session until the next sign-in.

If Tempo ID does not answer the revocation, such as offline, signOut rejects with server_error and status 0, but the session is empty. The store keeps the refresh token for the next signOut to revoke.

Revoke the access key

A sign-out leaves the access key valid on the chain. To revoke it, call dialog.revokeAccessKey before signOut, in a click handler, since a popup opens only within the gesture. The account owner signs the revocation on Tempo ID's approval page, because the chain refuses keychain changes from an access key.

import { accounts } from './tempo.config.js'
 
export function SignOut() {
  async function signOut() {
    const { session } = accounts.session.status.get()
 
    if (session?.accessKey) await accounts.dialog.revokeAccessKey() 
    await accounts.signOut()
  }
 
  return (
    <button onClick={signOut} type="button">
      Sign out
    </button>
  )
}

It returns the revoking transaction's hash, or null for an access key revoked already. A Cancel throws Accounts.RequestError with access_denied, and the session stays.

With the viem plugin, accessKey.revokeSync revokes the session's own access key with no page, for any sign-in plugin. It sends the session's access token, and Tempo ID's cloud wallet signs as the owner.

The session then keeps the access key without its private key. A transaction before the sign-out thus throws Accounts.AccessKeyRequiredError, and on a page a transfer opens Tempo ID's payment page.

An account that only a passkey owns throws Accounts.RequestError with webauthn_required. Revoke its access key in the dialog.

Use cases

The use case sets the only option of signOut.

Sign out of every app

everywhere also ends the user's Tempo ID session, which signs the user out of every app. Only a first-party app may set it, as End all sessions shows.

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

Next steps