Authenticate Using GitHub on Android
GitHub is where over 65 million developers shape the future of software, together. Contribute to the open-source community, manage your Git repositories, review code like a pro, track bugs and features, power your CI/CD and DevOps workflows, and secure code before you commit it. So in this article, we are going to discuss how to authenticate using GitHub in your Android App with the help of Firebase User Authentication.
Steps for Firebase user Authentication using GitHub
Create a new project in android studio or open any existing project through with you want to authenticate a user by GitHub and add the firebase to that android application. Steps to add firebase in your application.
Go to your firebase console, go to your application then navigate to project settings and add fingerprint-like SHA1 of your connected application. To find SHA1 go to Gradle (right side of android-studio window ) > your-application-name > Tasks > Android > signingReport (double click on it)
Note: Make sure to add google-services.json in your application, otherwise, download it from the project settings of the firebase console and add it to your application.
Go back to your application in the firebase console, navigate to Authentication (left panel of firebase) then go to Sign-in-method enable GitHub provider.
Now, we need Client ID and Client Secret. For this register your app as a developer application on GitHub and get your app’s OAuth 2.0 Client ID and Client Secret. To register your app, write the application name, enter the app’s website homepage URL (here I’m giving the same URL as in the Authorization callback URL), and provide the application description in brief.
Note: Make sure your Firebase OAuth redirect URI (e.g. my-app-12345.firebaseapp.com/__/auth/handler) is set as your Authorization callback URL in your app’s settings page on your GitHub app’s config.
Get your authorizable callback URL as shown in the image below.
Click Register application. You will get your Client ID and Client secret, copy and paste them in the firebase console (under sign-in-method) and click the save button to enable GitHub.
Come back to android studio. Add dependency for the Firebase Authentication Android library in your build.gradle (Module: your-application-name.app) by using the Firebase Android BoM .
// Import the BoM for the Firebase platform
// Declare the dependency for the Firebase Authentication library
// When using the BoM, you don’t specify versions in Firebase library dependencies
By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.
Step 5: Working with the MainActivity file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Step 6: Create a new Empty Activity
Refer to this article Create New Activity in Android Studio and create an empty activity. Name the activity as HomePageActivity. Navigate to the app > res > layout > activity_home_page.xml and add the below code to that file. Below is the code for the activity_home_page.xml file.
Go to the HomePageActivity.kt file and refer to the following code. Below is the code for the HomePageActivity.kt file.
Note: Don’t forget to Revoke all user tokens. This option will be shown when your application is ready and run once. Or may you see this option earlier?
Here is the final output.