Firebase UI: Authentication using email link FIREBASE

Firebase UI: Authentication using email link  

Firebase UI: Authentication using email link

Firebase UI: Authentication using email link

10 steps to understand how we can perform authentication using the email link and no password.

In the previous topic(Firebase UI: Authentication using email/password), we learned how we could use Firebase UI libraries to provide email, phone, and Google authentication. Similarly, we will learn how we can use Firebase UI libraries to provide email link/no password authentication.

So let's see the step by step process.

Step1:

In the first step, let's create a new project and add Firebase to it using any of the methods either from manually or Google Assistance. For connecting Firebase to the project, please go through the following link: Firebase - Environment Setup (with android studio)

Step 2:

After connecting Firebase to our application and applying plugins, you have to enable email link authentication in Firebase console.

 

Step 3:

In the next step, you need to enable the dynamic links for the email link. For this, you need to add a link. So just click on get started.

After clicking on Get started button, a new pop up box will be shown. Add a URL prefix and ensure that the page link has not been used in the past.

 

 

After clicking on Finish, the dynamic link will be created successfully

 

Step 4:

The next thing you need to do is, we will add the domain to the authentication.

 

The domain will be matched with our code, which we will add in the firebase console.

 

Step 5:

The last thing we need to do for setting up the dynamic links in order to be able to catch the link is to set SHA-1 and SHA-256. To understand how these keys can be found, click here. We will go to the setting of our project and click on Add fingerprint. This will be used for adding both SHA-1 and SHA-256.

 

 

 

 

 

Step 6:

In the next step, Now, go to the Android Studio and create an activity for sign-in i.e., an activity with a single button.

Step 7:

Now, create a tag for logging. And inside the event handler, you need to set up our action code setting object. It is the main logic for a password list sign-in and email.

  1. ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder()  
  2.         .setAndroidPackageName(getPackageName(),  
  3.                 true, /* install if not available? */  
  4.                 null   /* minimum app version */)  
  5.         .setHandleCodeInApp(true)  
  6.         .setUrl("https://fire.example.com/emailSignInLink")  
  7.         .build();  

Step 8:

Now, you need to handle the callback. And use start activity to resolve it in the following way:

  1. startActivityForResult(AuthUI.getInstance()  
  2.                 .createSignInIntentBuilder()  
  3.                 .setAvailableProviders(Arrays.asList(new  AuthUI.IdpConfig.EmailBuilder().enableEmailLinkSignIn().  
  4. setActionCodeSettings(actionCodeSettings).build())).build(), 1234);  

Step 9:

Now let's create onActivityResult() just like we have used previously:

  1. @Override  
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.     super.onActivityResult(requestCode, resultCode, data);  
  4.   
  5.     if (requestCode == 12345) {  
  6.         if (resultCode == RESULT_OK) {  
  7.             // Successfully signed in  
  8.             FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();  
  9.             Toast.makeText(getApplicationContext(), "Successfully signed in!", Toast.LENGTH_SHORT).show();  
  10.   
  11.         } else {  
  12.             // Sign in failed. If response is null the user canceled the sign-in flow using the back button. Otherwise check  
  13.             // response.getError().getErrorCode() and handle the error.  
  14.             // ...  
  15.             Toast.makeText(getApplicationContext(), "Sign in FAILED", Toast.LENGTH_SHORT).show();  
  16.         }  
  17.     }  

 

Step 10:

The last thing which you have to do is catching the dynamic link. Now catch the dynamic link inside the onCreate() function.

  1. if (AuthUI.canHandleIntent(getIntent())) {  
  2.     String link = getIntent().getData().toString();  
  3.   
  4.     List<AuthUI.IdpConfig> providers = Arrays.asList(  
  5.             new AuthUI.IdpConfig.EmailBuilder().build());  
  6.   
  7.     Log.d(TAG, "got an email link: " + link);  
  8.   
  9.     if (link != null) {  
  10.         startActivityForResult(  
  11.                 AuthUI.getInstance()  
  12.                         .createSignInIntentBuilder()  
  13.                         .setEmailLink(link)  
  14.                         .setAvailableProviders(providers)  
  15.                         .build(),  
  16.                 12345);  
  17.     }  
  18. }  

Now it's time to run our application. When we run the application, it will show the following output:

 

#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries