Unidy Docs

JavaScript SDK & Web Components

Install @unidy.io/sdk and build framework-agnostic customer experiences.

Build customer sign-in and account screens with framework-agnostic web components. The components call the SDK HTTP API and manage browser authentication state. For headless React forms, use the React SDK; for JavaScript without a DOM, use the standalone client.

Prepare the application

Obtain your tenant or brand base URL and an SDK client API key. Ask the administrator to register your app's origin, including its scheme and development port, in Allowed Hosts. Obtain the base URL separately from the API key.

The API key identifies your application and can be used in browser code. Customer operations also require a customer token. Authentication discovery is intentionally available before sign-in, so treat the key as an application credential and avoid publishing it in support tickets.

These are web components and require a DOM. There is no native mobile adapter covered by this guide. A hybrid WebView is a web environment, but its storage, cookie and redirect behavior needs separate testing. Native applications can use Authorization Code with PKCE.

Install and configure

Install a compatible release and commit the resulting lockfile:

bash
npm install @unidy.io/sdk

Register the components once in your browser entry point:

javascript
import { defineCustomElements } from '@unidy.io/sdk/loader';
import '@unidy.io/sdk/sdk.css';

defineCustomElements();

Add one configuration element before the other SDK components:

html
<u-config
  base-url="https://login.example.com"
  api-key="YOUR_SDK_CLIENT_KEY"
  locale="en"
  check-signed-in="true">
</u-config>
AttributePurpose
base-urlRequired. Your tenant or brand's configured login origin.
api-keyRequired. The SDK client's application key.
locale / fallback-localeLanguage and fallback language; both default to en.
custom-translationsTranslation overrides keyed by locale.
check-signed-inAttempt restoration from an existing Unidy browser session. Defaults to false; success depends on whether cookies are available in this browser context.
modeproduction by default; development enables additional logging.

Missing configuration prevents the other components from initializing. Configuration also loads captcha settings from /api/sdk/v1/sdk_clients/:api_key/captcha_config; account for the configured provider in your site's privacy and loading behavior.

Add sign-in

This bounded starter covers email, password, magic code, brand connection and missing profile fields. Its remaining states deliberately link to your tenant's hosted login, so a customer always has a continuation. Replace every example URL with your configured login URL. Use the SDK's registration and invitation components when you need those experiences entirely inside your app.

html
<u-signin-root>
  <u-signin-step name="email">
    <u-email-field placeholder="Email"></u-email-field>
    <u-error-message for="email"></u-error-message>
    <u-submit-button for="email" text="Continue"></u-submit-button>
  </u-signin-step>

  <u-signin-step name="verification">
    <u-conditional-render when="auth.passwordEnabled">
      <u-password-field placeholder="Password"></u-password-field>
      <u-error-message for="password"></u-error-message>
      <u-submit-button for="password" text="Sign in"></u-submit-button>
    </u-conditional-render>
    <u-conditional-render when="auth.magicCodeEnabled">
      <u-send-magic-code-button>Send a code</u-send-magic-code-button>
    </u-conditional-render>
    <p><a href="https://login.example.com">Use passkey, social login or reset password</a></p>
    <u-back-button>Back</u-back-button>
  </u-signin-step>

  <u-signin-step name="magic-code">
    <u-magic-code-field></u-magic-code-field>
    <u-error-message for="magicCode"></u-error-message>
    <u-back-button>Choose another method</u-back-button>
  </u-signin-step>

  <u-signin-step name="connect-brand">
    <p>Connect your account to this brand to continue.</p>
    <u-brand-connect-button action="connect">Connect</u-brand-connect-button>
    <u-brand-connect-button action="cancel">Cancel</u-brand-connect-button>
  </u-signin-step>

  <u-signin-step name="missing-fields">
    <u-missing-field></u-missing-field>
    <u-missing-fields-submit-button>Continue</u-missing-fields-submit-button>
  </u-signin-step>

  <u-signin-step name="unconfirmed">
    <p>Confirm your email, then return to sign in.</p>
    <a href="https://login.example.com">Resend confirmation using hosted login</a>
  </u-signin-step>
  <u-signin-step name="invited">
    <a href="https://login.example.com">Accept your invitation</a>
  </u-signin-step>
  <u-signin-step name="registration">
    <a href="https://login.example.com">Create or resume an account</a>
  </u-signin-step>
  <u-signin-step name="reset-password">
    <a href="https://login.example.com">Reset your password</a>
  </u-signin-step>
</u-signin-root>

<u-signed-in>
  <p>You are signed in.</p>
  <u-logout-button>Sign out</u-logout-button>
</u-signed-in>

The enabled methods come from the brand's login configuration. Never assume password login is available. Social login and hosted continuations can leave your page; configure and test their return URL. This example starts with the email step. Adding a single-login step changes the root's initial mode, so only add it when implementing that alternative layout. See sign-in and registration operations for the transitions behind these components.

<u-signed-in> controls presentation. The API independently authorizes customer requests; hiding a component does not authorize access to its data.

Use the customer session

After configuration has completed, obtain the authentication singleton:

javascript
import { Auth } from '@unidy.io/sdk/auth';

const auth = await Auth.getInstance();
await auth.ready;

if (await auth.isAuthenticated()) {
  const customer = await auth.userTokenPayload();
  // Use customer claims to personalize this page; do not log the JWT.
  console.info('Customer session available:', Boolean(customer));
}

The ID token is kept in sessionStorage, while the refresh token persists in localStorage. Closing a tab removes its session storage but does not erase the persisted refresh token. Shared refresh handling coalesces concurrent requests; transient failures preserve recoverable credentials. See authentication and session management for restoration, refresh and logout.

Continue building

Validate each enabled authentication method, required-field state, confirmation, invitation and logout on your own brand before release. The starter is a composition example, not a preconfigured tenant application.

Was this page helpful?