Skip to content

Authentication Auth

The Auth API provides user authentication, sign-in, biometrics, and other security-related capabilities.

Try it online

getUserProfilerequestSSOLogin
BiometricrequestAuthorizationToken

Results will appear below

Auth API demo

Mock results for user profile, SSO, biometric authentication, and authorization token

Import

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

API reference

requestSSOLogin

Start SSO (single sign-on) login.

ts
const result = await woo.requestSSOLogin()

Return value SSOLoginResult:

FieldTypeDescription
codestringAuthorization code
statestringState token

ACK mechanism

SSO login is interactive. When the host receives the request, it first sends ack, and the SDK automatically extends the timeout to 5 minutes so the flow does not time out while the user is acting.


requestLinkedLogin

Start linked (identity provider) login.

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

Submit account linking.

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

unlinkAccount

Unlink a linked account.

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

authenticateWithBiometric

Start biometric authentication (fingerprint / Face ID).

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

cancelBiometricAuthentication

Cancel biometric authentication in progress.

ts
await woo.cancelBiometricAuthentication()

requestAuthorizationToken

Request an authorization token.

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

getUserProfile

Get user profile data.

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

Full example: sign-in flow

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>
}

Type definitions

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
}

Quick reference

MethodDescriptionSignature
loginStart sign-in() → Promise<{ code }>
checkSessionCheck whether the session is still valid() → Promise<{ valid }>
getUserInfoGet user information() → Promise<UserInfo>
getUserProfileGet user information (with popup consent)({ desc }) → Promise<UserInfo>
authorizeRequest a permission explicitly({ scope }) → Promise<void>
getSettingGet the list of granted permissions() → Promise<{ authSetting }>
openSettingOpen the permission settings page() → Promise<{ authSetting }>

MiniDev Studio — Mini-app Development Toolkit