Firebase SDK: Authentication using the email link FIREBASE
- Firebase Tutorial
- Features of Firebase
- Android Studio
- Firebase - Environment Setup (with android studio)
- Firebase Assistance - Android Studio
- Firebase Authentication
- Firebase: Google Sign-In Authentication
- Firebase: Facebook Sign-In Authentication
- Firebase: Sign-In Authentication Using Phone Number
- Firebase UI: Authentication using email/password
- Firebase UI: Authentication using email link
- Firebase SDK: Authentication using email/password
- Firebase SDK: Authentication using the email link
- Firebase: Realtime Database
- Firebase: Real-time database setup and configuration
- Firebase: Data Organization in Real-time Database
- Firebase: Realtime Database Reading and Writing
- Firebase: Realtime Database Update and Delete
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:
- Creating an Android project.
- Creating a Firebase project.
- Adding Firebase to the Android project or application, either manually or Firebase Assistance.
- 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:
- public class MainActivity extends AppCompatActivity {
- //Declaration of EditText, Button, FirebaseAuth, String and SharedPreferences
- private EditText email;
- private Button signin;
- private String email_address, email_link,pending_email;
- String TAG="EMAIL_AUTH";
- FirebaseAuth auth;
- private SharedPreferences pref;
- private static final String KEY_PENDING_EMAIL="key_pending_email";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //Fetching id?s of EditText and button
- email=(EditText)findViewById(R.id.editText);
- signin=(Button)findViewById(R.id.SignInButton);
- //Making button disable
- signin.setEnabled(false);
- //Getting instances of FirebaseAuth
- auth=FirebaseAuth.getInstance();
- //Reading preferences if it?s already been set
- pref=getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE);
- pending_email=pref.getString(KEY_PENDING_EMAIL,null);
- //Checking for pending email?.
- if(pending_email!=null){
- email.setText(pending_email);
- Log.d(TAG,"Getting Shared Preferences"+pending_email);
- }
- //Creating intent for catching the link
- Intent intent = getIntent();
- if (intent != null && intent.getData() != null) {
- email_link = intent.getData().toString();
- Log.d(TAG, "got an intent: " + email_link);
- // Confirm the link is a sign-in with email link.
- if (auth.isSignInWithEmailLink(email_link)) {
- signin.setEnabled(true);
- }
- }
- }
- //Implementing event handlers
- public void onSignInClickedButton(View view) {
- //Getting email address from the text field
- email_address = email.getText().toString();
- //Calling signInWithEmailLink using the FirebaseAuth instances with OnCompleteListener and OnComplete callback.
- auth.signInWithEmailLink(email_address, email_link)
- .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- // remove shared preferences, set everything back to default
- pending_email = null;
- SharedPreferences.Editor editor = pref.edit();
- editor.remove(KEY_PENDING_EMAIL);
- editor.commit();
- email.setText("");
- //Checking for task
- if (task.isSuccessful()) {
- Toast.makeText(getApplicationContext(), "Successfully signed in with email link!", Toast.LENGTH_SHORT).show();
- AuthResult result = task.getResult();
- } else {
- Toast.makeText(getApplicationContext(), "Error signing in with email link", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- public void onEmailClick(View view) {
- email_address = email.getText().toString();
- //Validation check
- if (email_address.equals("")) {
- Toast.makeText(getApplicationContext(), "Enter Email Address!", Toast.LENGTH_SHORT).show();
- return;
- }
- //Save Email Address using SharedPreference Editor.
- SharedPreferences.Editor editor=pref.edit();
- editor.putString(KEY_PENDING_EMAIL,email_address);
- editor.commit();
- //Set-up our ActionCodeSettings
- ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder()
- .setAndroidPackageName(
- getPackageName(),
- false, /* install if not available? */
- null /* minimum app version */)
- .setHandleCodeInApp(true)
- .setUrl("https://auth.example.com/emailSignInLink")
- .build();
- //Implement sendSignInLinkToEmail method to send the link to email address
- auth.sendSignInLinkToEmail(email_address, actionCodeSettings)
- .addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(@NonNull Task<Void> task) {
- if (task.isSuccessful()) {
- Toast.makeText(getApplicationContext(), "Email Sent", Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- }
Output: