Add email sign-in
This page adds the email sign-in component to your app: a principal signs in with a 6-digit emailed code, and your app decides what to show after.
If your app has a server on the same origin as its pages, follow Keep the session on your server instead. Otherwise, keep the session in the browser with deviceSession({ storage: localStorage }), as the code below does. Session strategies compares the choices.
The SDK overview lists the packages and how to install them.
Before you start
- Your publishable key,
publishableKey, listing your app's origins inallowedOrigins, from Create a project and issue a key. - Your project's secret key,
key, to read sign-in codes from the test inbox in adevproject, from Create a project and issue a key.
This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.
1. Add the component
Use the React code with React 19, and the Web Components code otherwise.
// Sign principals in with IdentityHound's email-code component, in React.
//
// The provider holds the identity: the project's publishable key, and where
// the session is kept. Here the browser keeps it, with `deviceSession`; an
// app with its own server passes a session strategy whose server keeps it in
// an HTTP-only cookie instead. The component runs the whole sign-in: the
// email, the code, resend, change-email, and the errors.
import {
IdentityhoundProvider,
useSession,
} from '@agentpost-tools/identityhound-react';
import {
deviceSession,
type IdentityOptions,
} from '@agentpost-tools/identityhound-sdk/workflows';
import { EmailCodeSignIn } from '@agentpost-tools/identityhound-ui/react';
export interface SignInAppProps {
/** The project's publishable key, `pk_test_…` or `pk_live_…`. */
publishableKey: string;
/** IdentityHound's API root. Defaults to Agentpost's hosted API. */
baseUrl?: string;
fetch?: IdentityOptions['fetch'];
}
export function SignInApp({ publishableKey, baseUrl, fetch }: SignInAppProps) {
return (
<IdentityhoundProvider
publishableKey={publishableKey}
session={deviceSession({ storage: localStorage })}
baseUrl={baseUrl}
fetch={fetch}
>
<Account />
</IdentityhoundProvider>
);
}
function Account() {
const session = useSession();
if (session.status === 'loading') return <p>Loading…</p>;
if (session.status === 'signed_in')
return (
<p>
{session.principal.displayName
? `Signed in as ${session.principal.displayName}. `
: 'Signed in. '}
<button type="button" onClick={() => void session.signOut()}>
Sign out
</button>
</p>
);
return <EmailCodeSignIn />;
}
// Sign principals in with IdentityHound's email-code component, as Web
// Components, in a compact layout of the parts: one row for the address,
// one for the code. The same primitive and workflow as the React card.
import {
createIdentity,
deviceSession,
type Identity,
type IdentityOptions,
type SignedIn,
} from '@agentpost-tools/identityhound-sdk/workflows';
import '@agentpost-tools/identityhound-ui/web';
export interface SignInElementOptions {
/** The project's publishable key, `pk_test_…` or `pk_live_…`. */
publishableKey: string;
/** IdentityHound's API root. Defaults to Agentpost's hosted API. */
baseUrl?: string;
fetch?: IdentityOptions['fetch'];
/** Runs once the principal is signed in. */
onSignedIn?: (signedIn: SignedIn) => void;
}
const SLOTS = '<ui-otp-input-slot></ui-otp-input-slot>'.repeat(6);
export function mountSignIn(
container: HTMLElement,
{ onSignedIn, ...options }: SignInElementOptions,
): Identity {
const identity = createIdentity({
...options,
session: deviceSession({
storage: localStorage,
key: 'identityhound.web-session',
}),
});
container.innerHTML = `
<ih-email-code-sign-in data-layout="inline">
<ih-email-code-sign-in-title><h2>Use an email code</h2></ih-email-code-sign-in-title>
<ih-email-code-sign-in-email-step><form>
<ih-email-code-sign-in-field name="email"><ui-field>
<ui-field-label>Work email</ui-field-label>
<div data-inline>
<ui-input><input name="email" type="email" autocomplete="email" inputmode="email" autocapitalize="none" spellcheck="false" /></ui-input>
<ih-email-code-sign-in-send><button>Send code</button></ih-email-code-sign-in-send>
</div>
<ui-field-error match="true"><ih-email-code-sign-in-value name="message"></ih-email-code-sign-in-value></ui-field-error>
</ui-field></ih-email-code-sign-in-field>
<ih-email-code-sign-in-alert><ih-email-code-sign-in-value name="message"></ih-email-code-sign-in-value></ih-email-code-sign-in-alert>
</form></ih-email-code-sign-in-email-step>
<ih-email-code-sign-in-code-step><form>
<ih-email-code-sign-in-field name="code"><ui-field>
<ui-field-label>Code sent to <ih-email-code-sign-in-value name="email"></ih-email-code-sign-in-value></ui-field-label>
<div data-inline>
<ui-otp-input name="code" max-length="6"><ui-otp-input-group>${SLOTS}</ui-otp-input-group></ui-otp-input>
<ih-email-code-sign-in-verify><button>Verify</button></ih-email-code-sign-in-verify>
</div>
<ui-field-error match="true"><ih-email-code-sign-in-value name="message"></ih-email-code-sign-in-value></ui-field-error>
</ui-field></ih-email-code-sign-in-field>
<ih-email-code-sign-in-alert><ih-email-code-sign-in-value name="message"></ih-email-code-sign-in-value></ih-email-code-sign-in-alert>
<p data-links>
<ih-email-code-sign-in-resend><button><ih-email-code-sign-in-when resend="ready">Send again</ih-email-code-sign-in-when><ih-email-code-sign-in-when resend="waiting">Send again in <ih-email-code-sign-in-value name="resendIn"></ih-email-code-sign-in-value>s</ih-email-code-sign-in-when></button></ih-email-code-sign-in-resend>
<ih-email-code-sign-in-change-email><button>Change email</button></ih-email-code-sign-in-change-email>
</p>
</form></ih-email-code-sign-in-code-step>
<ih-email-code-sign-in-signed-in><p>Signed in.</p></ih-email-code-sign-in-signed-in>
</ih-email-code-sign-in>`;
const element = container.querySelector('ih-email-code-sign-in') as
(HTMLElement & { identity?: Identity }) | null;
element!.identity = identity;
if (onSignedIn)
element!.addEventListener('signedin', (event) =>
onSignedIn((event as CustomEvent<SignedIn>).detail),
);
return identity;
}
For Web Components, set the root element’s identity property, and listen for signedin to learn who signed in.
2. Load the styles
Import UI SDK’s stylesheet, then the component’s:
import '@ui-sdk/core/web.css';
import '@agentpost-tools/identityhound-ui/styles.css';
For a look of your own, leave the component’s stylesheet out and style the parts by their attributes.
3. Connect an agent
Skip this step unless an agent should run the sign-in through the page. Create UI SDK’s agent bridge before the page renders, and choose which actions it may take with allow:
import { createAgent, registerTools } from '@ui-sdk/react/agent';
// The app decides which steps an agent may run; signing in needs nobody to confirm.
const agent = createAgent({ allow: () => true });
registerTools(agent, { allow: () => true });
The component page’s agent actions list each action and its outcomes.
Check it worked
Sign in through the component. In a dev project, use an address at .test: its code waits in the test inbox, which you read with key.
With the Web Components layout, once the sign-in finishes, the root element’s data-step is signed_in, signedin has fired, identity.getSnapshot().session.status is signed_in, and localStorage holds the session under identityhound.web-session. The example app’s test checks exactly that:
await until(() => root.dataset.step === 'signed_in', 'signed in');
assert.equal(signedIn, true);
assert.equal(identity.getSnapshot().session.status, 'signed_in');
assert.ok(localStorage.getItem('identityhound.web-session'));
With the React card, the page shows Signed in. when the principal has no name, and the session is under identityhound.session in localStorage.