10.2 Authentication
Last updated
Was this helpful?
Last updated
Was this helpful?
Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more
Make sure your Firebase project is set up, the steps of which are covered in the first section 11.1!
Add these two dependencies to your module-level Gradle file:
These dependencies add the Firebase Authentication Android library and Google Play services SDK to your app.
When accessing most Authentication services, you need the appropriate FirebaseAuth object.
First, declare your auth variable:
In the onCreate/onCreateView method of your activity/fragment, initialize the auth variable as so:
When initializing your Activity, check to see if the user is currently signed in in your onStart method (which is called automatically after onCreate):
You can sign out a user using:
This will most likely just be a collection of the links below, some personal reflections also!
3. Add an ActivityResultLauncher for the Google Sign In Activity intent (the API launches a separate activity to sign the user in):
4. You can add a handy Google sign-in button via:
5. You can then register the OnClickListener for this button in your activity via:
7. Expand the intent launcher
8. After a user successfully signs in, get an ID token from the GoogleSignInAccount
object, exchange it for a Firebase credential, and authenticate with Firebase using the Firebase credential:
9. All done :)
2. When a new user signs up using your app's sign-up form, complete any new account validation steps that your app requires, such as verifying that the new account's password was correctly typed and meets your complexity requirements.
3. Create a new account by passing the new user's email address and password to createUserWithEmailAndPassword
:
If given some email and password, you can sign in a user with signInWithEmailAndPassword:
Check out to see operations one can do on a user object!
1. Enable Google Sign-In in the Firebase console: 1.1. In the , open the Auth section for your project 1.2. On the Sign in method tab, enable the Google sign-in method and click Save.
2. In your sign-in activity's onCreate
method, configure Google Sign-In to request the user data required by your app. For example, to configure Google Sign-In to request users' ID and basic profile information, create a object with the DEFAULT_SIGN_IN
parameter. To request users' email addresses as well, create the GoogleSignInOptions
object with the requestEmail
option.
If you need to request additional scopes to access Google APIs, specify them with . For the best user experience, on sign-in, only request the scopes that are required for your app to minimally function. Request any additional scopes only when you need them, so that your users see the consent screen in the context of an action they performed. See .
6. In the activity's onClick
method, handle sign-in button taps by creating a sign-in intent with the method, and starting the intent with the ActivityResultLauncher
.
Many of the other providers (i.e. Facebook and Twitter and GitHub) follow similar patterns. If you find yourself using multiple of these in your app in your authentication process, it may be worth it looking into for seamless integration.
.1. Enable Email/Password sign-in: 1.1. In the , open the Auth section. 1.2. On the Sign in method tab, enable the Email/password sign-in method and click Save.
If you are looking for a good password strength detector, take a look at , which is used by Dropbox.
Firebase Auth offers many more ways to authenticate users, I implore you to explore the and see what could possibly fit your needs!