src/examples/accounts/Headless.tsx

Every source

src/examples/accounts/Headless.tsx
'use client'

import { type SignInMethod, useAuth, useLinkableMethods, useLinkedAccounts } from '@synqauth/react'

// Connected accounts headless: useLinkedAccounts lists the ways the person
// signs in, useLinkableMethods the ways the brand lets them add, every
// provider included, and useAuth().link adds one (Synq asks them to sign in
// again, then for the new method, and comes back here). A passkey is added
// on Synq's page with the add-passkey intent through link('passkey'). accounts.unlink stops
// one; Synq refuses to unlink the last way in, so nobody locks themselves out.
export function AccountsHeadless() {
  const { data: accounts, error } = useLinkedAccounts()
  const { data: linkable } = useLinkableMethods()
  const { link, accounts: actions } = useAuth()
  if (error) return <p role="alert">{error.message}</p>
  if (!accounts) return <p>Loading…</p>
  return (
    <>
      <ul aria-label="Ways you sign in">
        {accounts.map((account) => (
          <li key={account.id}>
            {account.provider}: <code>{account.subject}</code>
            {account.handle ? ` @${account.handle}` : ''}{' '}
            <button type="button" onClick={() => void actions.unlink(account.id)}>
              Unlink {account.provider}
            </button>
          </li>
        ))}
      </ul>
      {linkable?.map(({ method, name }) => (
        <button key={method} type="button" onClick={() => void link(method as SignInMethod)}>
          Add {name}
        </button>
      ))}
    </>
  )
}