Skip to content

認証 Auth

Auth API は、ユーザー認証、ログイン、生体認証など、セキュリティに関する機能を提供します。

オンラインで試す

getUserProfilerequestSSOLogin
生体認証requestAuthorizationToken

結果は下に表示されます

Auth API デモ

ユーザープロフィール、SSO、生体認証、認可トークンのモック結果を取得します

インポート

ts
import woo from 'mini-sdk'
// 或
import { auth } from 'mini-sdk'

API リファレンス

requestSSOLogin

SSO(シングルサインオン)ログインを開始します。

ts
const result = await woo.requestSSOLogin()

戻り値 SSOLoginResult

フィールド説明
codestring認可コード
statestringステートトークン

ACK の仕組み

SSO ログインは対話的な操作です。Host がリクエストを受け取ると、まず ack を送信し、SDK が自動的にタイムアウトを 5 分に延長し、ユーザー操作の途中でタイムアウトしないようにします。


requestLinkedLogin

連携ログイン(IdP 連携)を開始します。

ts
const result = await woo.requestLinkedLogin({
  provider: 'wechat',
  scope: 'basic',
})

アカウントの連携登録を送信します。

ts
await woo.submitAccountLink({
  provider: 'phone',
  credential: '+86138xxxx1234',
  verificationCode: '123456',
})

unlinkAccount

アカウントの連携を解除します。

ts
await woo.unlinkAccount({ provider: 'wechat' })

authenticateWithBiometric

生体認証(指紋 / 顔)を開始します。

ts
try {
  const result = await woo.authenticateWithBiometric({
    reason: '请验证身份以完成支付',
  })
  console.log('认证成功', result)
} catch (err) {
  if (err.code === BridgeErrorCode.BiometricCancelled) {
    console.log('用户取消了认证')
  }
}

cancelBiometricAuthentication

進行中の生体認証をキャンセルします。

ts
await woo.cancelBiometricAuthentication()

requestAuthorizationToken

認可トークンを取得します。

ts
const result = await woo.requestAuthorizationToken({
  scope: 'user:read',
})
console.log(result.token)

getUserProfile

ユーザーのプロフィールを取得します。

ts
const profile = await woo.getUserProfile()
console.log(profile.nickname, profile.avatar)

完全な例:ログインフロー

vue
<script setup>
import woo from 'mini-sdk'
import { BridgeErrorCode } from 'mini-sdk'

async function login() {
  woo.showLoading('登录中...')

  try {
    // 1. SSO 登录获取授权码
    const { code } = await woo.requestSSOLogin()

    // 2. 用授权码换取 Token
    const res = await woo.request.post('/api/auth/login', { code })
    const { token } = res.data

    // 3. 保存 Token
    await woo.setStorage({ key: 'token', data: token })

    // 4. 获取用户信息
    const profile = await woo.getUserProfile()
    await woo.setStorage({ key: 'profile', data: profile })

    woo.showToast({ title: '登录成功', icon: 'success' })
    woo.reLaunch('/pages/index/index')
  } catch (err) {
    if (err.code === BridgeErrorCode.UserCancelled) {
      woo.showToast({ title: '已取消登录', icon: 'none' })
    } else {
      woo.showToast({ title: '登录失败', icon: 'none' })
    }
  } finally {
    woo.hideLoading()
  }
}
</script>
tsx
import { useCallback } from 'react'
import woo from 'mini-sdk'
import { BridgeErrorCode } from 'mini-sdk'

function LoginFlow() {
  const login = useCallback(async () => {
    woo.showLoading('登录中...')

    try {
      const { code } = await woo.requestSSOLogin()
      const res = await woo.request.post('/api/auth/login', { code })
      const { token } = res.data
      await woo.setStorage({ key: 'token', data: token })
      const profile = await woo.getUserProfile()
      await woo.setStorage({ key: 'profile', data: profile })
      woo.showToast({ title: '登录成功', icon: 'success' })
      woo.reLaunch('/pages/index/index')
    } catch (err) {
      if (err.code === BridgeErrorCode.UserCancelled) {
        woo.showToast({ title: '已取消登录', icon: 'none' })
      } else {
        woo.showToast({ title: '登录失败', icon: 'none' })
      }
    } finally {
      woo.hideLoading()
    }
  }, [])

  return <button onClick={login}>登录</button>
}

型定義

ts
interface SSOLoginResult {
  code: string
  state: string
}

interface LinkedLoginOptions {
  provider: string
  scope?: string
}

interface LinkedLoginResult {
  token: string
}

interface SubmitAccountLinkOptions {
  provider: string
  credential: string
  verificationCode?: string
}

interface BiometricOptions {
  reason?: string
}

interface BiometricResult {
  success: boolean
}

interface RequestAuthorizationTokenOptions {
  scope: string
}

interface AuthTokenResult {
  token: string
  expiresIn: number
}

interface UserProfile {
  nickname: string
  avatar: string
  [key: string]: unknown
}

早見表

メソッド説明シグネチャ
loginログインを開始する() → Promise<{ code }>
checkSessionセッションが有効かどうかを確認する() → Promise<{ valid }>
getUserInfoユーザー情報を取得する() → Promise<UserInfo>
getUserProfileユーザー情報を取得する(ポップアップの同意)({ desc }) → Promise<UserInfo>
authorize権限を明示的に要求する({ scope }) → Promise<void>
getSetting付与済みの権限一覧を取得する() → Promise<{ authSetting }>
openSetting権限設定ページを開く() → Promise<{ authSetting }>

MiniDev Studio — ミニアプリ開発ツールキット