RN手机号一键登录

356 阅读1分钟

本文已参与【新人创作礼】活动,一起开启掘金创作之路。

极光认证,手机一键登录

本文使用极光认证,详见官网,下面分享的是本人总结的,避免后人少踩坑

Android 集成

  1. 首先我们要在 you_project/android/app/build.gradle 中配置如下内容
    defaultConfig {
      // ... 省略非关键代码
      manifestPlaceholders = [
            JPUSH_APPKEY: "23232326cbcbf1aef4ae7c433",         //在此替换你的APPKey
            JPUSH_CHANNEL: "yl_app"        //在此替换你的channel
        ]
      // ... 省略非关键代码
    }

     // ... 省略非关键代码
     dependencies {
       // inport package
       implementation project(':jcore-react-native')          // 添加 jcore 依赖
       implementation project(':jverification-react-native')  // 添加 jverification 依赖
     }
  1. 然后在 you_project/android/settings.gradle 添加如下依赖
    include ':jverification-react-native'
    project(':jverification-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jverification-react-native/android')
    include ':jcore-react-native'
    project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')

  1. 最后在 AndroidManifest.xml中配置如下代码

    <meta-data
          android:name="JPUSH_CHANNEL"
          android:value="${JPUSH_CHANNEL}" />
    <meta-data
          android:name="JPUSH_APPKEY"
          android:value="${JPUSH_APPKEY}" />
  

至此, android 相关集成工作完毕。

Ios 集成 (待完善)

使用

  • 检查手机是否支持环境是否支持
    JVerification.checkLoginEnable(result => {
        console.log('checkLoginEnable:' + JSON.stringify(result))
    })
  • 初始化配置
   const params = {
      time: 5000,
      appKey: '87204wq06qwqcbcbf1aef4ae7c433', //仅iOS
      channel: 'channel', //仅iOS
      advertisingId: 'advertisingId', //仅iOS
      isProduction: false, //仅iOS
   }

   JVerification.init(Platform.OS == 'ios' ? parmas : { time: 50000 }, result => {
      console.log('init:', JSON.stringify(result))
   })
  • 获取token,用户和后端通讯
   JVerification.getToken(5000, (result: any) =>
      console.log('getToken:' + JSON.stringify(result))
   );
  • 唤起登录
   // 此时,如果环境支持不出意外,那么可以看见一键登录页面
   JVerification.login(true);
  • 监听登录回调
    useEffect(() => {
    // 一键监听登录
    JVerification.addLoginEventListener(LoginListener);
    }, []);

    const LoginListener=(result)=> {
        console.log('login result:', JSON.stringify(result))
    }