Authenticate with Firebase Using Email Link in Android

Viewed 1256

I am trying to Firebase example 'Authenticate with Firebase Using Email Link in Android'. This link is that example. https://firebase.google.com/docs/auth/android/email-link-auth

I have done sendSignInLink to email.

But in 'Verify link and sign in' part, I don't understand code. In this part

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

Understandably, intent.getData().toString(); make error nullpointexception..... In that firebase example, there is no detail explain or sample code about this.

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.");
            }
        }
    });

To this part, everything successful.

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