- Mastering Firebase for Android Development
- Ashok Kumar S
- 474字
- 2021-06-18 18:49:15
Setting up Firebase Authentication
As we learned in Chapter 1, Keep it real – Firebase Realtime Database, we can make use of Android Studio to integrate Firebase, or we can follow the traditional way of integrating Firebase features through the console by adding configuration files and dependencies manually.
In this chapter, we will explore the complete features and potential of Firebase Authentication. We will later create a new Android project and add the Firebase SDK and authentication Gradle dependency.
Add the following dependency to an already configured project. At the time of writing, Firebase authentication had a version 16.0.1. It may vary in your builds, but the components class names will remain same:
implementation 'com.google.firebase:firebase-auth:16.0.1'
Firebase Authentication can follow two methods of implementation, one using Firebase Auth UI and the other using the Firebase SDK. The authentication process will make use of the FirebaseAuth shared instance. Reference to this object will allow developers to handle various tasks, such as creating the user account, signing users in and signing users out, and fetching information relating to the signed in user. Another essential role of a FirebaseAuth instance is as an authentication state listener; the AuthStateListener interface application will be able to receive notification of the user's authentication status.
An AuthUI instance helps when the application makes use of the FirebaseUI Auth library for the user interface. It will have all the classes that are necessary for creating the user account to sign in users and so on.
The FirebaseUser class is used to bind the user profile information for signed in users. The getCurrentUser() method from FirebaseAuth will return the FirebaseUser object, and data will be different depending on the auth provider.
The AuthCredential class will take care of data marshaling (the process of making the data compatible with Firebase) from all the Auth providers, and also it will bind the user account credentials to Firebase. Using this class, we can exchange tokens from third-party authentication providers for the credentials of the Firebase account. When the user signs in using a third-party authentication provider such as Facebook, Twitter, or GitHub, the application provides a user token for that provider. Using this token, Firebase creates an account for the user. The third-party provider token needs to be converted to the AuthCredential object by calling the getCredential() method. For popular authentication providers, there are AuthCredential subclasses listed here:
- EmailAuthCredential
- PhoneAuthCredential
- FacebookAuthCredential
- GithubAuthCredential
- GoogleAuthCredential
- TwitterAuthCredential
Every authentication provider class has its own provider class helping to manage most of the authentication tasks. Firebase includes these provider classes:
- EmailAuthProvider
- PhoneAuthProvider
- FacebookAuthProvider
- GithubAuthProvider
- GoogleAuthProvider
- TwitterAuthProvider
We will see all these classes in use in a later section of the chapter. Firebase authentication is one of the key components of the Firebase toolchain. It needs a practical approach to excel.