Skip to content

认证鉴权 Auth

Auth API 提供用户认证、登录、生物识别等安全相关能力。

在线试用

getUserProfilerequestSSOLogin
生物识别requestAuthorizationToken

结果将显示在下方

认证 API 试用

拉取用户资料、SSO、生物识别与授权 Token 的 mock 结果

导入

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

发起关联登录。

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

获取授权 Token。

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
}

API 速查表

方法说明签名
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 — 小程序开发利器