通过Firebase身份验证通过otp使用手机号码登录

时间:2020-05-09 14:53:30

标签: android firebase firebase-authentication

在SignUpActivity.java中,我从用户那里获取手机号码,并将其传递给用户需要输入otp或自动填充的另一种意图。这在我的代码中可以正常工作,但我想输入该类型片段事物的片段,如果用户在SignUpActivity.java中提交详细信息,则会出现片段事物,如屏幕快照所示,用于检测otp。请有人说出如何实现这一目标。image of the result which I want

3 个答案:

答案 0 :(得分:0)

您可以在这里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 */
  }

答案 1 :(得分:0)

添加片段。首先在您的布局文件中添加一个容器。

<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>

现在,为片段创建另一个布局文件和java类。然后在您的活动中编写以下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();

现在,在片段中获取电话号码

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

答案 2 :(得分:0)

AuthUI为您提供了这样的功能,而无需您提供任何代码

为此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);

就是here的官方文档,不要忘记从控制台启用电话验证

您还在评论中提到您不了解片段,因此我鼓励您在转向Firebase之前先学习android基础知识

编辑

关于用户信息的收集

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

注意,如果您通过电话进行身份验证,则只有user.getPhoneNumer();将返回电话号码,其他方法将返回null,并且如果进行邮件身份验证,user.getPhoneNumber()将返回null ,实际上,在某个地方使用它们之前,应该检查所有它们是否为空。

罗汉过得愉快