搜索插件、代码仓与 APP 分类

smart_auth 封面
插件详情v4.0.0

smart_auth

Android 短信用户同意 API、短信检索 API 的包装器,用于读取一次性短信验证码、获取用户手机号码、OTP、OTC、短信自动填充、Android 自动填充

Android
近 30 天下载
16.7万
喜欢数
211
Pub 分数
160

安装插件

flutter pub add smart_auth

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

SmartAuth 是一个 Flutter 包,通过利用 Android 短信用户同意 API 和短信检索 API,简化了 Android 上基于 OTP 和短信的认证。它能够自动检测并读取来自传入短信的一次性密码(OTP),支持在表单中自动填充 OTP,并安全地获取用户电话号码。该包专为与 PIN、OTP 和密码字段无缝集成而设计,通过减少手动输入来提升用户体验。该包为平台特定,仅适用于 Android。

适合场景

  • 从短信中自动检测OTP
  • 自动填充登录表单中的OTP
  • 从短信中读取用户电话号码
  • 通过短信自动填充增强登录流程
  • 简化双重身份验证
  • 减少OTP输入中的用户输入错误

主要特点

  • 从短信中自动提取OTP
  • 安全的电话号码检索
  • 仅限 Android 的短信自动填充支持
  • 与文本字段的简单集成
  • 实时短信监控
  • 符合 Android 的 SMS Retriever API

适合对象

  • 需要OTP自动填充的Android应用
  • 使用短信双因素认证的应用程序
  • 寻求原生短信集成的开发者
  • 用户流程中的短信验证
  • 旨在减少登录摩擦的应用

使用建议

  • 仅适用于 Android
  • 需要正确的短信格式以进行检测
  • 需要运行时权限以访问短信
  • 仅限具备 Google Play 服务的设备
  • 取决于短信检索 API 的可用性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Flutter Smart Auth

Need anything Flutter related? Reach out on LinkedIn

Pub package GitHub starts style: effective dart pub package

Flutter package for listening SMS code on Android, suggesting phone number, email, saving a credential.

If you need pin code input like shown below, take a look at the Pinput package.

Features:

  • Android SMS Autofill
    • SMS Retriever API
    • SMS User Consent API
  • Showing Phone number hints API

Support

Discord Channel

Example

Don't forget to give it a star ⭐

If you want to contribute to this project, please read the contribution guide.

Requirements

1. Set kotlin version to 1.8.0 or above and gradle plugin version to 8.3.2

If you are using legacy imperative apply

// android/build.gradle
buildscript {
    ext.kotlin_version = '1.8.0'
    ...others

    dependencies {
        classpath 'com.android.tools.build:gradle:8.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

If you are using new declarative plugin approach

// android/settings.gradle
plugins {
    id "org.jetbrains.kotlin.android" version "1.8.0" apply false
    id "com.android.application" version "8.3.2" apply false
    ...others
}
2. Set gradle version to 8.4.0 or above - more about gradle versions
// android/gradle/wrapper/gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip
3. Set Java version to 11
// android/app/build.gradle
compileOptions {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

kotlinOptions {
    jvmTarget = '11'
}

Getting Started

Create instance of SmartAuth

final smartAuth = SmartAuth.instance;
Request phone number hint - Docs

The Phone Number Hint API, a library powered by Google Play services, provides a frictionless way to show a user’s (SIM-based) phone numbers as a hint.

The benefits to using Phone Number Hint include the following:

  • No additional permission requests are needed
  • Eliminates the need for the user to manually type in the phone number
  • No Google account is needed
  • Not directly tied to sign in/up workflows
  • Wider support for Android versions compared to Autofill
void requestPhoneNumberHint() async {
  final res = await smartAuth.requestPhoneNumberHint();
  if (res.hasData) {
    // Use the phone number
  } else {
    // Handle error
  }
}
Get SMS with User Consent API

The SMS User Consent API complements the SMS Retriever API by allowing an app to prompt the user to grant access to the content of a single SMS message. When a user gives consent, the app will then have access to the entire message body to automatically complete SMS verification. The verification flow looks like this:

  1. A user initiates SMS verification in your app. Your app might prompt the user to provide a phone number manually or request the phone number hint by calling requestPhoneNumberHint method.
  2. Your app makes a request to your server to verify the user's phone number. Depending on what information is available in your user database, this request might include the user's ID, the user's phone number, or both.
  3. At the same time, your app calls the getSmsWithUserConsentApi to show the user a dialog to grant access to the SMS message.
  4. Your server sends an SMS message to the user that includes a one-time code to be sent back to your server.
  5. When the user's device receives the SMS message, the getSmsWithUserConsentApi will extract the one-time code from the message text and you have to send it back to your server.
  6. Your server receives the one-time code from your app, verifies the code, and finally records that the user has successfully verified their account.
void getSmsWithUserConsentApi() async {
  final res = await smartAuth.getSmsWithUserConsentApi();
  if (res.hasData) {
    final code = res.requireData.code;

    /// The code can be null if the SMS was received but the code was not extracted from it
    if (code == null) return;
    //  Use the code
  } else if (res.isCanceled) {
    // User canceled the dialog
  } else {
    // handle the error
  }
}
Get SMS with SMS Retriever API

With the SMS Retriever API, you can perform SMS-based user verification in your Android app automatically, without requiring the user to manually type verification codes, and without requiring any extra app permissions. When you implement automatic SMS verification in your app, the verification flow looks like this:

  1. A user initiates SMS verification in your app. Your app might prompt the user to provide a phone number manually or request the phone number hint by calling requestPhoneNumberHint method.
  2. Your app makes a request to your server to verify the user's phone number. Depending on what information is available in your user database, this request might include the user's ID, the user's phone number, or both.
  3. At the same time, your app calls the getSmsWithRetrieverApi to begin listening for an SMS response from your server.
  4. Your server sends an SMS message to the user that includes a one-time code to be sent back to your server, and a hash that identifies your app.
  5. When the user's device receives the SMS message, Google Play services uses the app hash to determine that the message is intended for your app, and makes the message text available to your app through the SMS Retriever API.
  6. The getSmsWithRetrieverApi will extract the one-time code from the message text and you have to send it back to your server.
  7. Your server receives the one-time code from your app, verifies the code, and finally records that the user has successfully verified their account.
void getSmsWithRetrieverApi() async {
  final res = await smartAuth.getSmsWithRetrieverApi();
  if (res.hasData) {
    final code = res.requireData.code;

    /// The code can be null if the SMS was received but the code was not extracted from it
    if (code == null) return;
    //  Use the code
  } else {
    // handle the error
  }
}
Dispose

The plugin automatically removes listeners after receiving the code, if not you can remove them by calling the removeUserConsentApiListener or removeSmsRetrieverApiListener method.

void removeSmsListener() {
  smartAuth.removeUserConsentApiListener();
  // or
  smartAuth.removeSmsRetrieverApiListener();
}

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目