简体   繁体   English

通过firebase认证通过otp用手机号登录

[英]login with mobile number via otp through firebase authentication

In SignUpActivity.java I am taking the mobile number from user and passing it to another intent where the user needs to enter the otp or it gets fill up automatically.This is working fine through my code but I want to make that type of fragment thing by which if user submits the details in SignUpActivity.java then a fragment kind of thing appears as shown in the screenshot for detecting the otp.在 SignUpActivity.java 中,我从用户那里获取手机号码并将其传递给用户需要输入 otp 或自动填充的另一个意图。这通过我的代码工作正常,但我想制作那种类型的片段如果用户在 SignUpActivity.java 中提交详细信息,则会出现一个片段类型的东西,如用于检测 otp 的屏幕截图所示。 Please someone say that how to achieve this.请有人说如何实现这一点。 image of the result which I want我想要的结果的图像

You can see info of that here https://firebase.google.com/docs/auth/android/phone-auth你可以在这里看到信息https://firebase.google.com/docs/auth/android/phone-auth

// The test phone number and code should be whitelisted in the console.
String phoneNumber = "some_number";
String smsCode = "some_codes";

FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseAuthSettings firebaseAuthSettings = firebaseAuth.getFirebaseAuthSettings();

// Configure faking the auto-retrieval with the whitelisted numbers.
firebaseAuthSettings.setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber, smsCode);

PhoneAuthProvider phoneAuthProvider = PhoneAuthProvider.getInstance();
phoneAuthProvider.verifyPhoneNumber(
  phoneNumber,
  60L,
  TimeUnit.SECONDS,
  this, /* activity */
  new OnVerificationStateChangedCallbacks() {
    @Override
    public void onVerificationCompleted(PhoneAuthCredential credential) {
      // Instant verification is applied and a credential is directly returned.
    }

    ... /* other callbacks */
  }

To add a fragment.添加一个片段。 First add a container in your layout file.首先在布局文件中添加一个容器。

<androidx.constraintlayout.widget.ConstraintLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

    < 
      ...........  
      />


    //  This is container for your fragment 
    <FrameLayout
       android:id="@+id/YOU_CONTAINER_ID"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Now, create another layout file and java class for fragment.现在,为片段创建另一个布局文件和 java class。 Then write below java code in your activity然后在您的活动中写下 java 代码

public static final int FRAGMENT_CODE = 1111;

.....

Bundle bundle = new Bundle();
bundle.putString("MobNo", YOUR_PHONE_NO_STRING);

MyFragment fragment = new MyFragment();
fragment.setArguments(bundle);
fragment.setTargetFragment(this,FRAGMENT_CODE);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.YOUR_CONTAINER_ID, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

Now, to get phone number in your fragment现在,在您的片段中获取电话号码

 if (getArguments() != null) {
     String phoneNo = getArguments().getString("MobNo"));
 }

AuthUI gives you that without any code from your side AuthUI 无需任何代码即可为您提供

do that implementation 'com.firebaseui:firebase-ui-auth:6.2.0' add this line to build.gradle and update to latest version implementation 'com.firebaseui:firebase-ui-auth:6.2.0'将此行添加到 build.gradle 并更新到最新版本

      // Choose authentication providers
        List<AuthUI.IdpConfig> providers = Arrays.asList(
      new AuthUI.IdpConfig.PhoneBuilder().build());  // can add more providers in future in that list without breaking existing code
      // Create and launch sign-in intent
      startActivityForResult(
      AuthUI.getInstance()
            .createSignInIntentBuilder()
            .setAvailableProviders(providers)
            .build(),
    REQUEST_CODE);

That is it here is the official documentation for that and don't forget to enable phone auth from the console就是这里的官方文档,不要忘记从控制台启用电话身份验证

also you mentioned in a comment that you don't know fragments so I will encourage you to learn the android basics first before moving to firebase你还在评论中提到你不知道片段,所以我鼓励你先学习 android 基础知识,然后再转到 firebase

EDIT编辑

Regarding collection of user info关于收集用户信息

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        String name = user.getDisplayName();
        String phone = user.getPhoneNumber();
        String email = user.getEmail();
        Uri photoUrl = user.getPhotoUrl();

note if you are authenticating via phone, only user.getPhoneNumer();请注意,如果您通过电话进行身份验证,则只有user.getPhoneNumer(); will return a phone number and other methods will return null, and in case of mail authentication user.getPhoneNumber() will return null, in practice you should check all of them for nullity before using them somewhere.将返回一个电话号码,其他方法将返回 null,并且在邮件身份验证的情况下user.getPhoneNumber()将返回 null,实际上您应该在某处使用它们之前检查所有这些是否为空。

Have a good day Rohan祝你有个美好的一天罗汉

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用手机号码和密码登录 Firebase(无 OTP) - Firebase Login using Mobile number and password (without OTP) Firebase 未向手机号码发送 OTP - Firebase is not sending OTP to the Mobile Number 如何使用Firebase Android验证收到的用于手机号码验证的OTP - How to validate the OTP received for mobile number verification using Firebase android Firebase 未将 OTP 发送到移动设备 — Android Studio firebase 电话身份验证错误 - Firebase didn't send OTP to mobile — Android Studio firebase phone authentication error 通过移动数据Firebase匿名身份验证失败 - Firebase anonymous authentication failure through Mobile Data 通过微软进行 Firebase 身份验证 - 登录选项卡未显示 - Firebase authentication via microsoft - Login tab not showing 在最新更新中通过 firebase 进行 OTP 身份验证时,它需要 reCaptcha 有没有办法在 android Studio 中绕过它? - In OTP authentication through firebase in latest update it requires reCaptcha is there any way to bypass it in android Studio? 无法同时使用电子邮件和电话号码验证在Firebase身份验证应用程序中发送OTP - OTP not sent in Firebase Authentication app using both email and contact number verification 通过android在firebase中进行电话号码身份验证的问题 - Issue in Phone number authentication in firebase through android Firebase 带有 OTP 的电话身份验证在发布版本中不起作用 - Firebase Phone authentication with OTP not working in release build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM