Mobile
Sign a user in from an Expo app, in an authentication sheet over the app.
Overview
An Expo app signs in through the redirect flow in the system browser, not a web view, so the app never sees the user's credentials. The expo preset shows Tempo ID's pages in an authentication sheet over the app, and keeps the session in the device's secure store. It is the sign-in flow for Expo, and a React Native app without Expo uses the same plugin.
A custom scheme is not an origin, so the app needs a registration with Tempo ID, which issues its client ID and lists its redirect URI.
An Expo app runs only on a device, so the page holds no demo. This component runs the whole flow: a sign-in, the session, and a sign-out.
import { Button, Text, View } from 'react-native'
import { accounts } from './tempo.config.js'
export function App() {
const { isPending, session } = accounts.useSession()
if (isPending) return null
if (!session)
return (
<Button
onPress={() => accounts.redirect.signIn()}
title="Sign in with Tempo"
/>
)
return (
<View>
<Text>Signed in as {session.user.email}</Text>
<Button onPress={() => accounts.signOut()} title="Sign out" />
</View>
)
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [expo({ redirectUri: 'com.example.app:/callback' }), react()],
})Walkthrough
The mobile example runs these steps. The React Native tab of Getting started installs the packages, sets the app's scheme, and configures Metro.
Create the instance
expo returns two plugins. The first sets the instance's store to the secure store, and its redirectUri on the app's scheme. The second is the redirect plugin, whose sign-in opens the sheet.
import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [expo({ redirectUri: 'com.example.app:/callback' }), react()],
})For a local Tempo ID or another chain, Use another Tempo ID or chain sets the instance.
| Platform | Secure store | Authentication sheet |
|---|---|---|
| iOS | Keychain | ASWebAuthenticationSession |
| Android | SharedPreferences, encrypted with the Keystore | Custom Tab |
An app has no page, so only the session's access key sends a payment of accounts.pay, through the viem plugin. Add viem() to plugins to send payments: without it, pay throws Actions.account.PageRequiredError, whose message names the missing plugin.
Sign the user in
redirect.signIn opens Tempo ID in the sheet, redeems the callback's code, saves the session, and returns it.
import { Button } from 'react-native'
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<Button
onPress={async () => {
const session = await accounts.redirect.signIn()
// The user closed the sheet.
if (!session) return
console.log(`Signed in as ${session.user.email}`)
}}
title="Sign in with Tempo"
/>
)
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [expo({ redirectUri: 'com.example.app:/callback' }), react()],
})| The user | redirect.signIn |
|---|---|
| Approves the request | Returns the session |
| Closes the sheet | Returns undefined |
| Denies the request | Throws an Accounts.RequestError with the code access_denied |
If Tempo ID does not know the client ID or the redirect URI, the sheet shows an error page, and no callback returns.
Render the session
accounts.useSession reads the secure store at the first render, with isPending until the read ends, so a restart needs no sign-in.
If the read fails, such as while the device is locked, the hook throws the store's error to the nearest error boundary, and the status stays pending. The next mount reads the store again.
import { Button, Text } from 'react-native'
import { accounts } from './tempo.config.js'
export function App() {
const { isPending, session } = accounts.useSession()
if (isPending) return null
if (!session)
return (
<Button
onPress={() => accounts.redirect.signIn()}
title="Sign in with Tempo"
/>
)
return <Text>Signed in as {session.user.email}</Text>
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [expo({ redirectUri: 'com.example.app:/callback' }), react()],
})The session ends with its access key. After the access key's expiry, the hook restores no session.
Sign the user out
signOut empties the session, deletes it from the secure store, and revokes its refresh token. Every other instance in the app on an expo preset empties its session at once.
import { Button } from 'react-native'
import { accounts } from './tempo.config.js'
export function SignOut() {
return <Button onPress={() => accounts.signOut()} title="Sign out" />
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [expo({ redirectUri: 'com.example.app:/callback' }), react()],
})The next sign-in always shows Tempo ID's sign-in page, so another user can sign in. Offline, signOut rejects, but the session stays empty, and the next signOut revokes the refresh token that the secure store keeps.
Use cases
Each use case sets one option of redirect.signIn, of the expo preset, or of the redirect plugin.
Bind a challenge
challenge has the user sign a server's message at the sign-in. The returned session's challengeSignature holds the signature, which the server checks, as Sign challenges shows.
import { Button } from 'react-native'
import { accounts } from './tempo.config.js'
export function SignIn() {
async function signIn() {
const response = await fetch('https://api.example.com/challenge', {
method: 'POST',
})
const session = await accounts.redirect.signIn({
challenge: await response.text(),
})
if (!session) return
await fetch('https://api.example.com/verify', {
body: JSON.stringify({
address: session.address,
signature: session.challengeSignature,
}),
headers: { 'content-type': 'application/json' },
method: 'POST',
})
}
return <Button onPress={signIn} title="Sign in with Tempo" />
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [expo({ redirectUri: 'com.example.app:/callback' }), 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 in the sheet after the approval.
import { Button } from 'react-native'
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<Button
onPress={() =>
accounts.redirect.signIn({
funding: { amount: '10', currency: 'USD' },
})
}
title="Sign in with Tempo"
/>
)
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [expo({ redirectUri: 'com.example.app:/callback' }), react()],
})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. A refused refresh ends the session, so the app then passes the email that it kept.
import { Button } from 'react-native'
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<Button
onPress={() =>
accounts.redirect.signIn({
loginHint: 'alice@example.com',
})
}
title="Sign in with Tempo"
/>
)
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [expo({ redirectUri: 'com.example.app:/callback' }), 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.
import { Button } from 'react-native'
import { accounts } from './tempo.config.js'
export function SignIn() {
return (
<Button
onPress={() =>
accounts.redirect.signIn({
resource: 'https://mcp.example.com/mcp',
})
}
title="Sign in with Tempo"
/>
)
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [expo({ redirectUri: 'com.example.app:/callback' }), react()],
})Share the browser's Tempo ID session
On iOS, the sheet shares no cookies with the system browser, so each sign-in shows Tempo ID's sign-in page. With ephemeral set to false, it shares them, and a user signed in to Tempo ID there only approves the request.
import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [
expo({ ephemeral: false, redirectUri: 'com.example.app:/callback' }),
react(),
],
})On Android, the sheet always shares them, so the option has no effect.
Sign in without Expo
An app without Expo passes redirect with its own open, and its own store. native.ts stands for the app's native modules.
import { Accounts, Store } from 'accounts'
import { redirect } from 'accounts/plugins'
import { keychain, openAuthSession } from './native.js'
export const accounts = Accounts.create({
chain: 'testnet',
clientId: 'example-mobile',
plugins: [
redirect({
open: (url, options) => openAuthSession(url, options.redirectUri),
}),
],
redirectUri: 'com.example.app:/callback',
store: Store.from(keychain),
})import type { Store } from 'accounts'
// Each declaration stands for a native module of the app.
export declare const keychain: Store.Store
export declare function openAuthSession(
url: string,
redirectUri: string,
): Promise<string | undefined>| Module | Contract |
|---|---|
keychain | Returns each value as it was set, a bigint included. A store of strings serializes with ox's Json. |
openAuthSession | Shows the URL, closes at the redirect URI, and resolves with the callback URL, or undefined for a closed sheet. |
The app also installs react-native-get-random-values, since React Native links only the native modules of the app's own package.json.