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 属性。要缩小列表,请自行布置各个步骤,并从 useKidChildSetup().verification.methods 中选择。
  • 当设置流程不需要验证,或家长已通过验证时,KidVerifyAge 不渲染任何内容。

自行嵌入验证页面

如需在您自己的框架中显示验证页面,请调用 useKidChildSetup().verification.start(method) 获取其 URL,使用 allow={VERIFY_IFRAME_ALLOW}(来自 @k-id/family-react/rules)在 iframe 中显示它,并在确认 message 事件来自您的 iframe 之后,将每个事件交给 verification.handleMessage(event)。

verification.methodsLoaded 为 true 后,verification.methods 会按顺序列出 k-ID 在此设备上向该家长提供的方法。当 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 的来源。外观设置不适用于 k-ID 验证页面本身。

On this page