--

Monday 28 November 2016

FIREBASE - Started with Authentication

           Firebase allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user's devices. Firebase Authentication provides secure backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to our app.

          The ready-made Authentication methods support developers to enhance the functionality and usability of the application. I have shared the work flow video of Firebase Authentication App for better understanding,




This video portrays the user registration, updating user information like display name, profile picture, reset password, and user login using Firebase Authentication backend services. I elucidate the complete Authentication implementation further.

Step 1: User registration
Get Firebase auth instance in OnCreate()
authRegister = FirebaseAuth.getInstance(), then
and call authRegister.createUserWithEmailAndPassword(email,password).



authRegister.createUserWithEmailAndPassword(email,password).
        addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                showProgress(false);
                if (!task.isSuccessful()) {
                    Log.e(TAG,"Register exception:"+task.getException());
                   } else {
                    finish();
                    startActivity(new Intent(RegisterActivity.this,HomeActivity.class));
                }
            }
        });

Step 2 : User signin
By calling signInWithEmailAndPassword(email,password) when user click Sign in button. 


 authLogin.signInWithEmailAndPassword(email,password).
        addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                showProgress(false);
                if (!task.isSuccessful()) {
                    mPasswordView.setError(getString(R.string.messgae_login_fail));
                    mPasswordView.requestFocus();
                } else {
                    finish();
                    startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                }
            }
        });

Step 3: Update user information
Creating UserProfileChangeRequest by adding display name and profile picture.

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName(etxtDisplayName.getText().toString())
        .setPhotoUri(photoUri)
        .build();



By calling updateProfile(UserProfileChangeRequest) method to update user information.
existingUser.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    showProgress(false);
                    Log.d(TAG, "User profile updated.");
                 }
            }
        });

Step 4: Reset Password
By calling sendPasswordResetEmail() to reset instructions sending to our given valid mail id.

  We have to click on the given link from received mail to proceed further.


Once user clicked, 'Reset your password' window opens to enter your new password.



Step 5: Update Email & password
By calling updateEmail(email) & updatePassword(password) methods to update credentials respectively. 

Step 6: Delete user account & Sign out
By calling auth.signOut() method to close existing user session.
Initialize FirebaseUser existingUserFirebaseAuth.getInstance().getCurrentUser(); in OnCreate()
By calling  existingUser.delete() to eradicate user details from cloud completely.

I have shared the complete source code in Github, 
https://github.com/JayaprakashR-Zealot/FirebaseAuthentication

Kindly share your queries in the comment section.

Happy Coding !!!
Thanks..

No comments:

Post a Comment