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



在SignUpActivity.java中,我从用户那里获取手机号码,并将其传递给另一个意图,用户需要在其中输入otp,否则它会自动填充。这在我的代码中运行得很好,但我想让这种类型的片段成为可能,如果用户在SignUpActivity.java中提交详细信息,那么就会出现一种片段类型的东西,如检测otp的屏幕截图所示。请有人说如何实现这一点。我想要的结果图像

您可以在此处查看相关信息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 */
}

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

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

现在,为fragment创建另一个布局文件和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"));
}

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

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

你也在一条评论中提到,你不知道碎片,所以我鼓励你在转到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。在实践中,您应该在某处使用它们之前检查所有这些方法的无效性。

祝你愉快Rohan

最新更新