Authenticate Using GitHub on Android

You can let your users authenticate with Firebase using their GitHub accountsby integrating web-based generic OAuth Login into your app using the FirebaseSDK to carry out the end to end sign-in flow.

Before you begin

To sign in users using GitHub accounts, you must first enable GitHub as asign-in provider for your Firebase project:

  1. If you haven't already,add Firebase to your Android project.

  2. In theFirebase console, open theAuth section.
  3. On theSign in method tab, enable theGitHub provider.
  4. Add theClient ID andClient Secret from that provider's developer console to the provider configuration:
    1. Register your app as a developer application on GitHub and get your app's OAuth 2.0Client ID andClient Secret.
    2. Make sure your FirebaseOAuth redirect URI (e.g.my-app-12345.firebaseapp.com/__/auth/handler) is set as yourAuthorization callback URL in your app's settings page on yourGitHub app's config.
  5. ClickSave.
  6. In yourmodule (app-level) Gradle file(usually<project>/<app-module>/build.gradle.kts or<project>/<app-module>/build.gradle),add the dependency for theFirebase Authentication library for Android. We recommend using theFirebase Android BoMto control library versioning.

    dependencies{// Import theBoM for the Firebase platformimplementation(platform("com.google.firebase:firebase-bom:34.7.0"))// Add the dependency for theFirebase Authentication library// When using theBoM, you don't specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-auth")}

    By using theFirebase Android BoM, your app will always use compatible versions of Firebase Android libraries.

    (Alternative)  Add Firebase library dependencies without using theBoM

    If you choose not to use theFirebase BoM, you must specify each Firebase library version in its dependency line.

    Note that if you usemultiple Firebase libraries in your app, we strongly recommend using theBoM to manage library versions, which ensures that all versions are compatible.

    dependencies{// Add the dependency for theFirebase Authentication library// When NOT using theBoM, you must specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-auth:24.0.1")}

  7. If you haven't yet specified your app's SHA-1 fingerprint, do so from theSettings pageof theFirebase console. Refer toAuthenticating Your Clientfor details on how to get your app's SHA-1 fingerprint.

Handle the sign-in flow with the Firebase SDK

If you are building an Android app, the easiest way to authenticate your userswith Firebase using their GitHub accounts is to handle the entire sign-inflow with the Firebase Android SDK.

To handle the sign-in flow with the Firebase Android SDK, follow these steps:

  1. Construct an instance of anOAuthProvider using itsBuilder with the provider IDgithub.com

    Kotlin

    valprovider=OAuthProvider.newBuilder("github.com")

    Java

    OAuthProvider.Builderprovider=OAuthProvider.newBuilder("github.com");

  2. Optional: Specify additional custom OAuth parameters that you want tosend with the OAuth request.

    Kotlin

    // Target specific email with login hint.provider.addCustomParameter("login","your-email@gmail.com")

    Java

    // Target specific email with login hint.provider.addCustomParameter("login","your-email@gmail.com");

    For the parameters GitHub supports, see theGitHub OAuth documentation.Note that you can't pass Firebase-required parameters withsetCustomParameters(). These parameters areclient_id,response_type,redirect_uri,state,scope andresponse_mode.

  3. Optional: Specify additional OAuth 2.0 scopes beyond basic profile thatyou want to request from the authentication provider. If your applicationrequires access to private user data from GitHub APIs, you'll need torequest permissions to access GitHub APIs underAPI Permissions in theGitHub developer console. Requested OAuth scopes must be exact matches tothe preconfigured ones in the app's API permissions.

    Kotlin

    // Request read access to a user's email addresses.// This must be preconfigured in the app's API permissions.provider.scopes=listOf("user:email")

    Java

    // Request read access to a user's email addresses.// This must be preconfigured in the app's API permissions.List<String>scopes=newArrayList<String>(){{add("user:email");}};provider.setScopes(scopes);

  4. Authenticate with Firebase using the OAuth provider object. Note that unlikeother FirebaseAuthoperations, this will take control of your UI by popping up aCustom Chrome Tab.As a result, do not reference your Activity in theOnSuccessListenerandOnFailureListener that you attach as they will immediately detach whenthe operation starts the UI.

    You should first check if you've already received a response. Signing in viathis method puts your Activity in the background, which means that it can bereclaimed by the system during the sign in flow. In order to make sure thatyou don't make the user try again if this happens, you should check if aresult is already present.

    To check if there is a pending result, callgetPendingAuthResult:

    Kotlin

    valpendingResultTask=firebaseAuth.pendingAuthResultif(pendingResultTask!=null){// There's something already here! Finish the sign-in for your user.pendingResultTask.addOnSuccessListener{// User is signed in.// IdP data available in// authResult.getAdditionalUserInfo().getProfile().// The OAuth access token can also be retrieved:// ((OAuthCredential)authResult.getCredential()).getAccessToken().// The OAuth secret can be retrieved by calling:// ((OAuthCredential)authResult.getCredential()).getSecret().}.addOnFailureListener{// Handle failure.}}else{// There's no pending result so you need to start the sign-in flow.// See below.}

    Java

    Task<AuthResult>pendingResultTask=firebaseAuth.getPendingAuthResult();if(pendingResultTask!=null){// There's something already here! Finish the sign-in for your user.pendingResultTask.addOnSuccessListener(newOnSuccessListener<AuthResult>(){@OverridepublicvoidonSuccess(AuthResultauthResult){// User is signed in.// IdP data available in// authResult.getAdditionalUserInfo().getProfile().// The OAuth access token can also be retrieved:// ((OAuthCredential)authResult.getCredential()).getAccessToken().// The OAuth secret can be retrieved by calling:// ((OAuthCredential)authResult.getCredential()).getSecret().}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){// Handle failure.}});}else{// There's no pending result so you need to start the sign-in flow.// See below.}

    To start the sign in flow, callstartActivityForSignInWithProvider:

    Kotlin

    firebaseAuth.startActivityForSignInWithProvider(activity,provider.build()).addOnSuccessListener{// User is signed in.// IdP data available in// authResult.getAdditionalUserInfo().getProfile().// The OAuth access token can also be retrieved:// ((OAuthCredential)authResult.getCredential()).getAccessToken().// The OAuth secret can be retrieved by calling:// ((OAuthCredential)authResult.getCredential()).getSecret().}.addOnFailureListener{// Handle failure.}

    Java

    firebaseAuth.startActivityForSignInWithProvider(/* activity= */this,provider.build()).addOnSuccessListener(newOnSuccessListener<AuthResult>(){@OverridepublicvoidonSuccess(AuthResultauthResult){// User is signed in.// IdP data available in// authResult.getAdditionalUserInfo().getProfile().// The OAuth access token can also be retrieved:// ((OAuthCredential)authResult.getCredential()).getAccessToken().// The OAuth secret can be retrieved by calling:// ((OAuthCredential)authResult.getCredential()).getSecret().}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){// Handle failure.}});

    On successful completion, the OAuth access token associated with theprovider can be retrieved from theOAuthCredential object returned.

    Using the OAuth access token, you can call theGitHub API.

    For example, to get basic profile information, you can call the REST API,passing the access token in theAuthorization header:

  5. While the above examples focus on sign-in flows, you also have theability to link a GitHub provider to an existing user usingstartActivityForLinkWithProvider. For example, you can link multipleproviders to the same user allowing them to sign in with either.

    Kotlin

    // The user is already signed-in.valfirebaseUser=firebaseAuth.currentUser!!firebaseUser.startActivityForLinkWithProvider(activity,provider.build()).addOnSuccessListener{// Provider credential is linked to the current user.// IdP data available in// authResult.getAdditionalUserInfo().getProfile().// The OAuth access token can also be retrieved:// authResult.getCredential().getAccessToken().// The OAuth secret can be retrieved by calling:// authResult.getCredential().getSecret().}.addOnFailureListener{// Handle failure.}

    Java

    // The user is already signed-in.FirebaseUserfirebaseUser=firebaseAuth.getCurrentUser();firebaseUser.startActivityForLinkWithProvider(/* activity= */this,provider.build()).addOnSuccessListener(newOnSuccessListener<AuthResult>(){@OverridepublicvoidonSuccess(AuthResultauthResult){// Provider credential is linked to the current user.// IdP data available in// authResult.getAdditionalUserInfo().getProfile().// The OAuth access token can also be retrieved:// authResult.getCredential().getAccessToken().// The OAuth secret can be retrieved by calling:// authResult.getCredential().getSecret().}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){// Handle failure.}});

  6. The same pattern can be used withstartActivityForReauthenticateWithProvider which can be used to retrievefresh credentials for sensitive operations that require recent login.

    Kotlin

    // The user is already signed-in.valfirebaseUser=firebaseAuth.currentUser!!firebaseUser.startActivityForReauthenticateWithProvider(activity,provider.build()).addOnSuccessListener{// User is re-authenticated with fresh tokens and// should be able to perform sensitive operations// like account deletion and email or password// update.}.addOnFailureListener{// Handle failure.}

    Java

    // The user is already signed-in.FirebaseUserfirebaseUser=firebaseAuth.getCurrentUser();firebaseUser.startActivityForReauthenticateWithProvider(/* activity= */this,provider.build()).addOnSuccessListener(newOnSuccessListener<AuthResult>(){@OverridepublicvoidonSuccess(AuthResultauthResult){// User is re-authenticated with fresh tokens and// should be able to perform sensitive operations// like account deletion and email or password// update.}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){// Handle failure.}});

Next steps

After a user signs in for the first time, a new user account is created andlinked to the credentials—that is, the user name and password, phonenumber, or auth provider information—the user signed in with. This newaccount is stored as part of your Firebase project, and can be used to identifya 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 theFirebaseUser object. SeeManage Users.

  • In yourFirebase Realtime Database andCloud StorageSecurity Rules, you can get the signed-in user's unique user ID from theauth variable, and use it to control what data a user can access.

You can allow users to sign in to your app using multiple authenticationproviders bylinking auth provider credentials to anexisting user account.

To sign out a user, callsignOut:

Kotlin

Firebase.auth.signOut()

Java

FirebaseAuth.getInstance().signOut();

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.