Family SDK

保護者の年齢確認

セットアップ中に保護者が年齢を確認する仕組みと、確認方法を絞り込む、並べ替える、または自分でフレームに表示する方法。

同意に年齢確認が必要な場合、保護者はSDKがフレーム内に表示するk-IDのページで年齢を確認します。確認方法の一覧はk-IDから提供され、プロダクトの設定、管轄区域、保護者のデバイスによって異なります。一覧を絞り込んだり並べ替えたりすることはできますが、追加することはできません。

import { KidVerifyAge } from '@k-id/family-react'
import { VerificationMethodTypes } from '@k-id/family-react/rules'

<KidVerifyAge methods={[VerificationMethodTypes.CreditCard, VerificationMethodTypes.AgeKey]} />

VerificationMethodTypesには、k-IDが提供するすべての方法(例: CreditCard、IDDocument、AgeEstimation、AgeKey、EmailConfirmation)が含まれます。各方法の内容については、確認方法を参照してください。

  • k-IDがその保護者に提供していない方法は、何を渡しても表示されません。指定した方法がどれも一致しない場合、ステップには方法が表示されません。
  • <KidChildSetup>にはmethods propがありません。一覧を絞り込むには、ステップを自分で配置し、useKidChildSetup().verification.methodsから選んでください。
  • KidVerifyAgeは、セットアップで年齢確認が不要な場合や、保護者が確認を完了した後は何もレンダリングしません。

年齢確認を自分でフレームに表示する

年齢確認を独自のフレームに表示するには、useKidChildSetup().verification.start(method)を呼び出してURLを取得し、allow={VERIFY_IFRAME_ALLOW}(@k-id/family-react/rulesから)を指定したiframeで表示し、そのiframeからの各messageイベントを、自分のiframeから来たものであることを確認したうえでverification.handleMessage(event)に渡します。

verification.methodsは、このデバイスでk-IDがこの保護者に提供する方法を、verification.methodsLoadedがtrueになった時点で順番に列挙します。verification.outcomeが'methods'でない場合は選択できる一覧がないため、方法を指定せずにstart()を呼び出してください。方法を読み取れなかった場合はmethodsErrorが設定されます。refreshMethods()を呼び出して再試行してください。方法が失敗した後にも呼び出すと、試行回数と代替の方法が更新されます。

import { useEffect, useRef, useState } from 'react'
import { useKidChildSetup, useKidStrings } from '@k-id/family-react'
import { VERIFY_IFRAME_ALLOW } from '@k-id/family-react/rules'

function VerifyFrame({ method }: { method?: string }) {
  const { status, verification } = useKidChildSetup()
  const frame = useRef<HTMLIFrameElement>(null)
  const strings = useKidStrings()
  const [url, setUrl] = useState<string | null>(null)
  const [message, setMessage] = useState<string | null>(null)
  const [attempt, setAttempt] = useState(0)
  const started = useRef<{ key: string; result: ReturnType<typeof verification.start> } | null>(null)

  useEffect(() => {
    if (status !== 'consent_required') return
    let current = true
    setUrl(null)
    setMessage(null)
    const key = `${method ?? ''}:${attempt}`
    if (started.current?.key !== key) {
      started.current = { key, result: verification.start(method) }
    }
    void started.current.result.then((result) => {
      if (!current) return
      if (result.ok) setUrl(result.url)
      else setMessage(strings.errors[result.error.code])
    })
    return () => {
      current = false
    }
  }, [status, method, attempt])

  useEffect(() => {
    const onMessage = (event: MessageEvent) => {
      if (event.source !== frame.current?.contentWindow) return
      verification.handleMessage(event)
      if (event.data?.eventType === 'Verification.Error') void verification.refreshMethods()
    }
    window.addEventListener('message', onMessage)
    return () => window.removeEventListener('message', onMessage)
  }, [verification])

  if (message) {
    return (
      <div role="alert">
        <p>{message}</p>
        <button onClick={() => setAttempt((count) => count + 1)}>Try again</button>
      </div>
    )
  }
  return url && <iframe ref={frame} src={url} allow={VERIFY_IFRAME_ALLOW} title="Verify your age" />
}

<KidChildProvider>はkuidやtokenが変わっても子要素をマウントしたままにするため、フレームにはそれらから作ったkeyを指定してください(例: <VerifyFrame key={kuid ?? token} method={method} />)。指定しないと、前の子どものURLが残ります。

このフレームには、Content-Security-Policyのframe-srcにbaseUrlのオリジンが必要です。appearanceの設定は、k-IDの年齢確認ページ自体には適用されません。

On this page