Manage sessions
A React app signs a user in, renders the session, and signs the user out.
Overview
The react plugin of accounts/react adds accounts.useSession, a hook that returns the session and its status, and renders the component again at each change. This guide builds a component that signs a user in and out with it.
The demo shows each status of the session as it signs a user in and out.
Walkthrough
The web example reads the session with the same hook.
Create the instance
Create one instance for the page, outside any render, since the instance holds the session.
import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Read the session
The hook's first render restores the saved session.
import { accounts } from './tempo.config.js'
export function Account() {
const { status } = accounts.useSession()
return <p>status = {status}</p>
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Add the sign-in button
dialog.signIn opens Tempo ID's dialog, and the status changes to signedIn.
import { accounts } from './tempo.config.js'
export function Account() {
const { status } = accounts.useSession()
const { session, status } = accounts.useSession()
return <p>status = {status}</p>
return (
<main>
<p>status = {status}</p>
{!session && (
<button onClick={() => accounts.dialog.signIn()} type="button">
Sign in
</button>
)}
</main>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Add the sign-out button
accounts.signOut empties the session at once, and the status changes to signedOut. The user stays signed in to Tempo ID.
import { accounts } from './tempo.config.js'
export function Account() {
const { session, status } = accounts.useSession()
return (
<main>
<p>status = {status}</p>
{!session && (
<button onClick={() => accounts.dialog.signIn()} type="button">
Sign in
</button>
)}
{session && (
<>
<p>Signed in as {session.user.email}</p>
<button onClick={() => accounts.signOut()} type="button">
Sign out
</button>
</>
)}
</main>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Show the pending state
The status is pending until the instance reads the saved session. Disable the sign-in button meanwhile, so a signed-in user starts no second sign-in.
import { accounts } from './tempo.config.js'
export function Account() {
const { session, status } = accounts.useSession()
const { isPending, session, status } = accounts.useSession()
return (
<main>
<p>status = {status}</p>
{!session && (
<button onClick={() => accounts.dialog.signIn()} type="button">
<button
disabled={isPending}
onClick={() => accounts.dialog.signIn()}
type="button"
>
Sign in
</button>
)}
{session && (
<>
<p>Signed in as {session.user.email}</p>
<button onClick={() => accounts.signOut()} type="button">
Sign out
</button>
</>
)}
</main>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Use cases
Each use case sets an option of the react plugin, or reads a member that accounts.useSession returns.
Show a renewal
isReauthorizing is true while an authorization renews a session whose access key expired. The hook returns the expired session meanwhile, so the page keeps the user on screen.
import { accounts } from './tempo.config.js'
export function Account() {
const { isReauthorizing, session } = accounts.useSession()
if (!session) return null
return (
<p>
Signed in as {session.user.email}
{isReauthorizing ? ' (renewing the access key…)' : null}
</p>
)
}import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react()],
})Renew without an access key
On a page of a first-party app, the first render renews a missing or expired session from the browser's Tempo ID session. An object as reauthorizeOnMount passes the parameters of that authorization, such as no access key.
import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react({ reauthorizeOnMount: { accessKey: false } })],
})Turn off the renewal
reauthorizeOnMount: false restores only the saved session, so a first-party page without one shows its sign-in. Other apps renew nothing by default, since they hold no grant to read the Tempo ID session.
import { Accounts } from 'accounts'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
chain: 'testnet',
plugins: [react({ reauthorizeOnMount: false })],
})