Firebase Authentication – Login & Signup in Android 7


This tutorial describes the usage of the Firebase Authentication service in Android. If you don’t know what is Firebase and how to setup in android studio, then please go-through this previous blog.

Firebase’s built-in auth services that can handle user login entirely with client-side code, saving you time and the headache of operating your own back-end. Firebase makes it easy to support email and password authentication in your app. Firebase automatically stores your users’ credentials securely (using bcrypt) and redundantly (with replication and daily off-site backups).This separates sensitive user credentials from your application data, and lets you focus on the user interface and experience for your app.

 

So lets starts with Authentication service:

To write your Firebase Authentication Android client app, use the FirebaseAuth API. The instructions in this page assume that you have completed the steps for adding Firebase to your Android project.

Enable Auth service to your project

On your project dashboard. Find the Auth and click get started. Go to set up sign in method.

firebase_auth_signin_method

Choose Email & Password and change status from Disabled to Enable, and save it.

firebase_enable_auth

Note: This post will describe only on password-based account, for social media providers, we’ll discuss in another blog.

Set up Firebase and the Authentication SDK

In Android Studio, add the Authentication dependency to your app-level build.gradle file:

Permissions

For Firebase Auth to work, we need request for internet permission.

Authenticate User:

To authenticate user, create a shared instance of the FirebaseAuth object.

Set up an AuthStateListener that responds to changes in the user’s sign-in state.

Get Current Signed-In User:

To get the currently signed-in user by calling getCurrentUser. If a user isn’t signed in, getCurrentUser returns null.

Create an User:

Create a new account by passing the new user’s email address and password to createUserWithEmailAndPassword.

Sign-In an User:

If user signs in to your app, pass the user’s email address and password to signInWithEmailAndPassword.

Sign-Out an User:

To sign out an user, call signout method.

Update Profile Info:

An user’s basic profile information—the user’s display name and profile photo URL – can the updated with the updateProfile method.

Fetch User Info:

To get a user’s profile information, use the accessor methods of an instance of  FirebaseUser.

Update User Email Address:

An user’s email address can be updated with the updateEmail method.

Update User Password:

An user’s password can be updated with the updatePassword method.

Reset a Forgotten Password:

To reset a password, send a password reset email to a user with the sendPasswordResetEmail method.

Delete an User:

To delete an user account use the delete method.

ScreenShot:

 

vector_drawable_normal.pngvector_drawable_normal.pngvector_drawable_normal.png
vector_drawable_normal.pngvector_drawable_normal.png
vector_drawable_normal.png

Sample Class:

LoginSignUpFragment.java class is responsible for Login or Registering an User to our App.

HomeFragment.java class is responsible for updating user profile and to logout the user.

build.gradle (app-level)

build.gradle(project-level)

Conclusion

I hope you will like the article and it will definitely help you to make your apps more productive. In case of any confusion or query, feel free to ask. And if you found this tutorial to be useful, Share with others.


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.

  • Avista QuizBola

    Hello brother…
    Your post was awesome!!!

    But, may I to download your resource of this tutorial? Because better you share for more clear tutorial…
    Thanks bro, waiting your response in here to download the resource…

    • Please check the below link for text version and download complete course code.
      http://www.skholingua.com/android-basic/other-sdk-n-libs/firebase

      • Avista QuizBola

        okay thank you.
        I will download and learn your source.

        Bro, Can I request a tutorial ?
        I have a problem about creating an app.
        My app is about show daily content. About news in text. I hope I can update daily news in text to show in my app, but in firebase server database. So I hope I can update my news in firebase, and when user open my app, my app show the news that I write in firebase.

        Can you get it for me? I enjoy to see your blog, hope you can help me. Thanks.

        • Avista QuizBola

          Oh ya, also how to use firebase invites? I have read some info from firebase web about firebase invites. Yeah, this feature is about referral link.

          I want use this feature in my app. So when user 1 share the link and other user2 register on my app from that link, the user 1 and user 2 get a reward. Can you help me bro…? Hope this blog can solve it….

          This is a amazing blog!!! And everyday I always come here to see some tutorials…

        • Durga Chiranjeevi

          Thanks Avista, i’ll add it to my post list in future.

  • Phil

    Hello, please explain what is “import com.skholingua.android.firebasetuts.R”. I didn’t see this particular “R” in your provided source code. Thank you for your help in advance!

    • ‘R’ is an auto generated file in android and if you have different package in single project then we need to import this ‘R’ from the main package. Hope your package is different when compared to mine, thats why you not able to find. Simply delete this import line, everything will work has it. Thank you.