Before you begin
- Add Firebase to your Android project.
- Add the dependency for Firebase Authentication to your app-level
build.gradle
file:compile 'com.google.firebase:firebase-auth:9.6.1'
- Get your project's server keys:
- Go to the Credentials page of the Google API Console and select your project.
- Click Create credentials > Service account key. Select New service account, type any name, select JSON as the key type, and click Create.
- The new service account's public/private key pair is automatically saved on your computer. Copy this file to your authentication server.
Authenticate with Firebase
- In your sign-in activity's
onCreate
method, get the shared instance of theFirebaseAuth
object:private FirebaseAuth mAuth; // ... mAuth = FirebaseAuth.getInstance();
- Set up an
AuthStateListener
that responds to changes in the user's sign-in state:private FirebaseAuth.AuthStateListener mAuthListener; // ... @Override protected void onCreate(Bundle savedInstanceState) { // ... mAuthListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { // User is signed in Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); } else { // User is signed out Log.d(TAG, "onAuthStateChanged:signed_out"); } // ... } }; // ... } @Override public void onStart() { super.onStart(); mAuth.addAuthStateListener(mAuthListener); // ... } @Override public void onStop() { super.onStop(); if (mAuthListener != null) { mAuth.removeAuthStateListener(mAuthListener); } // ... }
- When users sign in to your app, send their sign-in credentials (for example, their username and password) to your authentication server. Your server checks the credentials and returns a custom token if they are valid.
- After you receive the custom token from your authentication server, pass
it to
signInWithCustomToken
to sign in the user:mAuth.signInWithCustomToken(mCustomToken) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "signInWithCustomToken:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Log.w(TAG, "signInWithCustomToken", task.getException()); Toast.makeText(CustomAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } } });
If sign-in succeeds, theAuthStateListener
runs theonAuthStateChanged
callback, in which you can use thegetCurrentUser
method to get the user's account data.
Next steps
After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.
-
In your apps, you can get the user's basic profile information from the
FirebaseUser
object. See Manage Users. In your Firebase Realtime Database and Firebase Storage Security Rules, you can get the signed-in user's unique user ID from the
auth
variable, and use it to control what data a user can access.
You can allow users to sign in to your app using multiple authentication providers by linking auth provider credentials to an existing user account.
To sign out a user, call
signOut
:
FirebaseAuth.getInstance().signOut();