HarmonyOS Native SDK
User Account
Mobile and Email Account

Mobile and Email Account

Mobile captcha verification requires @gizwits/smart-sdk 1.0.4 or later.

Mobile verification-code login

await userManager.sendMobileLoginVerifyCode('+1', phone, 'en-US');
await userManager.loginWithMobile('+1', phone, verificationCode);

Email verification-code login

await userManager.sendEmailLoginVerifyCode(email, 'en-US');
await userManager.loginWithEmail(email, verificationCode);

Mobile registration

await userManager.sendMobileRegisterVerifyCode('+1', phone, 'en-US');
const mobileToken = await userManager.registerWithMobile(
  '+1',
  phone,
  verificationCode,
  password
);
userManager.updateAuthorizeData(mobileToken);

registerWithPhone(phoneCode, phone, password, verificationCode) is a compatibility API with a different parameter order.

Captcha verification for SMS codes

When an application requests an SMS code for login, registration, or password recovery, the service may require the user to complete a captcha first. Keep the initial request unchanged. When verification is required, the SDK throws GizCaptchaRequiredError.

import {
  GizCaptchaInfo,
  GizCaptchaInput,
  GizCaptchaRequiredError,
} from '@gizwits/smart-sdk';
 
let captchaInfo: GizCaptchaInfo | undefined = undefined;
 
try {
  await userManager.sendMobileLoginVerifyCode('+1', phone, 'en-US');
} catch (error) {
  if (error instanceof GizCaptchaRequiredError) {
    captchaInfo = error.captcha;
    // Display captchaInfo.captchaImage as a JPEG Base64 image.
  } else {
    throw error;
  }
}

After the user enters the characters from the image, retry with the same captcha ID. Pass the calling application's current versionName as appVersion:

if (captchaInfo !== undefined) {
  const captchaInput = new GizCaptchaInput(
    captchaInfo.captchaId,
    userEnteredCode,
    appVersion
  );
  await userManager.sendMobileLoginVerifyCode('+1', phone, 'en-US', captchaInput);
}

For registration and password recovery, pass GizCaptchaInput as the fourth argument of sendMobileRegisterVerifyCode() or sendMobileForgotPasswordVerifyCode(). Call getMobileCaptcha() to request another image. If a retry throws GizCaptchaRequiredError again, replace the displayed captcha with the new one from the error and continue the verification flow.

Email registration

await userManager.sendEmailRegisterVerifyCode(email, 'en-US');
const emailToken = await userManager.registerWithEmail(email, verificationCode, password);
userManager.updateAuthorizeData(emailToken);

Verification-code delivery and validation can be rate limited. Catch rejected Promises and map them to suitable ArkUI feedback.