在Android中使用电子邮件链接通过Firebase进行身份验证

时间:2019-06-26 11:04:18

标签: java android firebase

我正在尝试Firebase示例“使用Android中的电子邮件链接对Firebase进行身份验证”。该链接就是该示例。  https://firebase.google.com/docs/auth/android/email-link-auth

我已完成sendSignInLink到电子邮件的操作。

但是在“验证链接并登录”部分中,我不了解代码。 在这一部分

Intent intent = getIntent();
String emailLink = intent.getData().toString();

可理解的是,intent.getData()。toString();使错误nullpointexception ..... 在该Firebase示例中,没有详细的解释或示例代码。

ActionCodeSettings actionCodeSettings =
    ActionCodeSettings.newBuilder()
            // URL you want to redirect back to. The domain (www.example.com) for this
            // URL must be whitelisted in the Firebase Console.
            .setUrl("https://www.example.com/finishSignUp?cartId=1234")
            // This must be true
            .setHandleCodeInApp(true)
            .setIOSBundleId("com.example.ios")
            .setAndroidPackageName(
                    "com.example.android",
                    true, /* installIfNotAvailable */
                    "12"    /* minimumVersion */)
            .build();



FirebaseAuth auth = FirebaseAuth.getInstance();
auth.sendSignInLinkToEmail(email, actionCodeSettings)
    .addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                Log.d(TAG, "Email sent.");
            }
        }
    });

至此,一切都成功了。

Intent intent = getIntent();
String emailLink = intent.getData().toString();

// Confirm the link is a sign-in with email link.
if (auth.isSignInWithEmailLink(emailLink)) {
// Retrieve this from wherever you stored it
String email = "someemail@domain.com";

// The client SDK will parse the code from the link for you.
auth.signInWithEmailLink(email, emailLink)
        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "Successfully signed in with email link!");
                    AuthResult result = task.getResult();
                    // You can access the new user via result.getUser()
                    // Additional user info profile *not* available via:
                    // result.getAdditionalUserInfo().getProfile() == null
                    // You can check if the user is new or existing:
                    // result.getAdditionalUserInfo().isNewUser()
                } else {
                    Log.e(TAG, "Error signing in with email link", task.getException());
                }
            }
        });
}

0 个答案:

没有答案