封装华为登录模块获取授权和取消授权

import { authentication } from '@kit.AccountKit'

PersistentStorage.persistProp<string>('openId', '')

//发送华为登录授权请求

class HuaweiAuthPlugin {

  async requestAuth() {

    // 1. 创建一个Account Kit授权请求对象,可通过返回值设置请求参数。

    const huaweiIdProvider = new authentication.HuaweiIDProvider()

    const authCreateRequest = huaweiIdProvider.createAuthorizationWithHuaweiIDRequest()

    // 2. 添加请求参数

    authCreateRequest.scopes = ['phone', 'openid']

    authCreateRequest.permissions = ['serviceauthcode']

    authCreateRequest.forceAuthorization = true

    // 3. 执行授权请求,获取认证码

    const authController = new authentication.AuthenticationController(getContext())

    const authResponse: authentication.AuthorizationWithHuaweiIDResponse =

      await authController.executeRequest(authCreateRequest)

    const serviceauthcode = authResponse.data?.authorizationCode

    AppStorage.setOrCreate<string>('openId', authResponse.data?.openID)

    return serviceauthcode

  }

  // getHuaweiIDState  api12 支持

//取消授权

  async cancelAuth() {

    try {

      // 1. 创建一个Account Kit授权请求对象,可通过返回值设置请求参数。

      const huaweiIdProvider = new authentication.HuaweiIDProvider()

      const authCancelRequest = huaweiIdProvider.createCancelAuthorizationRequest()

      // 2. 取消授权

      const authController = new authentication.AuthenticationController(getContext())

      await authController.executeRequest(authCancelRequest)

      return true

    } catch (e) {

      console.log('mk-logger', JSON.stringify(e))

      return false

    }

  }

}

export const huaweiAuthPlugin = new HuaweiAuthPlugin()

华为登录icon-default.png?t=O83Ahttps://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/account-get-phonenumber-V5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值