Firebase SDK: Authentication using the email link FIREBASE

Firebase SDK: Authentication using the email link  

Firebase SDK: Authentication using the email link

Firebase SDK: Authentication using the email link

Firebase SDK authentication using email link/no password is quite similar to Firebase UI authentication using an email link/password. We can use Firebase authentication to sign-up any user by sending them an email containing a link, and then they can click on that link to sign-in. The user email address is also verified in the process.

There are a lot of advantages to using email link authentication:

  • Low friction sign up and sign in
  • Low risk of password reuse across applications which can undermine the security of even well-selected passwords.
  • To authenticate a user and also verifying that the user is the legitimate owner of the email address. The user only needs to act an accessible email to sign-in, and the right kind of password list, no ownership of a phone number, no social media account is required. The user can sign-in securely without the need to provide a password. An existing user who previously signed in with an email identifier can also be upgraded to sign with just the email.
  • If the user forgets their password, they can still sign-in without any need to reset their password. So let's start our coding section and see how we can implement email link authentication using Firebase SDK.

Step 1:

In the first step, you have to perform the preliminary steps, which are as follows:

  1. Creating an Android project.
  2. Creating a Firebase project.
  3. Adding Firebase to the Android project or application, either manually or Firebase Assistance.
  4. Adding the required libraries and JSON files.

 

Step 2:

In the next step, you have to enable the email link password to sign in. You have to enable both email/password and email links.

 

Step 3:

You have to make sure that you have a dynamic link because when you select the link, your application needs to catch that link in order to login or register in the Firebase.

 

Step 4:

Now, you need to add the domain in the same way as you have done in Firebase UI email link authentication. Your domain name will be matched with the action code setting, which you will define next.

 

Step 5:

The last step which we have to do is to make sure that our project has SHA-1 and SHA-256 keys. We will set the SHA-1 and SHA-256 keys in the same way as we have done in our previous section Firebase UI: Authentication using the email link 

Step 6:

In this step you need to implement an activity layout which contains an edit text for email and two buttons for sign_in and email_click in the following way:

 

Step 7:

Now you need to modify your MainActivity.java to implement email link authentication using Firebase SDK in the following way:

  1. public class MainActivity extends AppCompatActivity {  
  2.       
  3.     //Declaration of EditText, Button, FirebaseAuth, String and SharedPreferences  
  4.     private EditText email;  
  5.     private Button signin;  
  6.     private String email_address, email_link,pending_email;  
  7.     String TAG="EMAIL_AUTH";  
  8.     FirebaseAuth auth;  
  9.     private SharedPreferences pref;  
  10.     private static final String KEY_PENDING_EMAIL="key_pending_email";  
  11.       
  12.    @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         //Fetching id?s of EditText and button  
  18.         email=(EditText)findViewById(R.id.editText);  
  19.         signin=(Button)findViewById(R.id.SignInButton);  
  20.           
  21.        //Making button disable   
  22.         signin.setEnabled(false);  
  23.   
  24.         //Getting instances of FirebaseAuth  
  25.         auth=FirebaseAuth.getInstance();  
  26.   
  27.         //Reading preferences if it?s already been set  
  28.         pref=getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE);  
  29.         pending_email=pref.getString(KEY_PENDING_EMAIL,null);  
  30.   
  31.         //Checking for pending email?.  
  32.         if(pending_email!=null){  
  33.             email.setText(pending_email);  
  34.             Log.d(TAG,"Getting Shared Preferences"+pending_email);  
  35.         }  
  36.   
  37.         //Creating intent for catching the link   
  38.         Intent intent = getIntent();  
  39.         if (intent != null && intent.getData() != null) {  
  40.             email_link = intent.getData().toString();  
  41.             Log.d(TAG, "got an intent: " + email_link);  
  42.   
  43.             // Confirm the link is a sign-in with email link.  
  44.             if (auth.isSignInWithEmailLink(email_link)) {  
  45.                 signin.setEnabled(true);  
  46.             }  
  47.         }  
  48.     }  
  49.   
  50.     //Implementing event handlers  
  51.     public void onSignInClickedButton(View view) {  
  52.         //Getting email address from the text field   
  53.         email_address = email.getText().toString();  
  54.            
  55.         //Calling signInWithEmailLink using the FirebaseAuth instances with OnCompleteListener and OnComplete callback.   
  56.         auth.signInWithEmailLink(email_address, email_link)  
  57.                 .addOnCompleteListener(new OnCompleteListener<AuthResult>() {  
  58.                     @Override  
  59.                     public void onComplete(@NonNull Task<AuthResult> task) {  
  60.   
  61.                         // remove shared preferences, set everything back to default  
  62.                         pending_email = null;  
  63.                         SharedPreferences.Editor editor = pref.edit();  
  64.                         editor.remove(KEY_PENDING_EMAIL);  
  65.                         editor.commit();  
  66.                         email.setText("");  
  67.                           
  68.                         //Checking for task   
  69.                         if (task.isSuccessful()) {  
  70.                             Toast.makeText(getApplicationContext(), "Successfully signed in with email link!", Toast.LENGTH_SHORT).show();  
  71.                             AuthResult result = task.getResult();  
  72.                         } else {  
  73.                             Toast.makeText(getApplicationContext(), "Error signing in with email link", Toast.LENGTH_SHORT).show();  
  74.                         }  
  75.                     }  
  76.                 });  
  77.     }  
  78.   
  79.     public void onEmailClick(View view) {  
  80.         email_address = email.getText().toString();  
  81.         //Validation check  
  82.         if (email_address.equals("")) {  
  83.             Toast.makeText(getApplicationContext(), "Enter Email Address!", Toast.LENGTH_SHORT).show();  
  84.             return;  
  85.         }  
  86.         //Save Email Address using SharedPreference Editor.  
  87.         SharedPreferences.Editor editor=pref.edit();  
  88.         editor.putString(KEY_PENDING_EMAIL,email_address);  
  89.         editor.commit();  
  90.            
  91.         //Set-up our ActionCodeSettings  
  92.         ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder()  
  93.                 .setAndroidPackageName(  
  94.                         getPackageName(),  
  95.                         false, /* install if not available? */  
  96.                         null   /* minimum app version */)  
  97.                 .setHandleCodeInApp(true)  
  98.                 .setUrl("https://auth.example.com/emailSignInLink")  
  99.                 .build();  
  100.   
  101.                  //Implement sendSignInLinkToEmail method to send the link to email address         
  102.                  auth.sendSignInLinkToEmail(email_address, actionCodeSettings)  
  103.                 .addOnCompleteListener(new OnCompleteListener<Void>() {  
  104.                     @Override  
  105.                     public void onComplete(@NonNull Task<Void> task) {  
  106.                         if (task.isSuccessful()) {  
  107.                             Toast.makeText(getApplicationContext(), "Email Sent", Toast.LENGTH_LONG).show();  
  108.                         }  
  109.                     }  
  110.                 });  
  111.     }  
  112. }   

Output:

 

#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries