Skip to content

debojyoti452/GithubLoginSDK

Repository files navigation

Github Login SDK for Android (LightWeight)

Motivation BTS: :octocat:

Reason Behind Building SDK :- As all we know that Github SDK is already available in Firebase Auth SDK but it has limitation like using custom redirect url can't be used or setting scopes is hard. Firebase Auth comes with a lot of extras like analytics, etc.. So I need a solution which simply just do the auth and return me token. So as usual was looking for an efficient SDK that can help me with this simple Auth, but TBH not a single solution I able to find, all were complex and head scratching. Finally I created my own and made it open source.

  • 🔥 Light Weight SDK (No Extra Libs used)
  • 😎 Works with Firebase Authentication Redirect Url also. (No Extra Firebase SDK Required)

Installation

Project Root Level Gradle (Below 7.0)

allprojects {
	repositories {
		maven { url 'https://jitpack.io' }
	}
}

OR

settings.gradle Level Gradle (Below 7.0)

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}

App Level Gradle

dependencies {
	 implementation 'com.github.debojyoti452:GithubLoginSDK:${version}'
}

AuthResult Model

  • Response will return this Params as ==AuthResult== model
       val scope: String? = null,
       val accessToken: String? = null,
       val tokenType: String? = null,
       val errorDescription: String? = null,
       val error: String? = null

Integrations

  • Initiate Github Auth SDK in Kotlin
       private val githubAuth: GithubAuth by lazy {
           GithubAuth.Builder(GIT_CLIENT_ID, GIT_CLIENT_SECRET)
               .setActivity(this)
               .setOnSuccess {
                   // Success Response Fetched.
                   Toast.makeText(this, it.toString(), Toast.LENGTH_SHORT).show()
               }
               .setOnFailed {
                   // Exceptions Catched.
                   Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
               }
               .setScopes(listOf(Scopes.PUBLIC_REPO, Scopes.USER_EMAIL))
               .build()
       }
  • Initiate Github Auth SDK in JAVA
       private GithubAuth githubAuth = new GithubAuth.Builder(GIT_CLIENT_ID, GIT_CLIENT_SECRET)
               .setActivity(this)
               .setOnSuccess(response -> {
                   Toast.makeText(JavaActivity.this, "Success", Toast.LENGTH_SHORT).show();
                   return null;
               })
               .setOnFailed(error -> {
                   Toast.makeText(JavaActivity.this, "Failed", Toast.LENGTH_SHORT).show();
                   return null;
               })
               .setScopes(Arrays.asList(Scopes.PUBLIC_REPO, Scopes.USER_EMAIL))
               .build();
  • Scopes Available (Constants)

       - Scopes.PUBLIC_REPO
       - Scopes.USER_EMAIL
       - Scopes.REPO
       - Scopes.GIST
  • Response Listener

      setOnSuccess {
           // Success Response Fetched.
           Toast.makeText(this, it.toString(), Toast.LENGTH_SHORT).show()
       }
       setOnFailed {
           // Exceptions Catched.
           Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
       }
  • Setting Scopes - [Accepts Arrays of Strings]

-- Kotlin Code

       setScopes(listOf(Scopes.PUBLIC_REPO, Scopes.USER_EMAIL)) 

-- JAVA Code

       setScopes(Arrays.asList(Scopes.PUBLIC_REPO, Scopes.USER_EMAIL))
  • Setting Custom Redirect URL (Optional Builder Method)

       setRedirectUrl("WILL ACCEPT ONLY THE URL USED IN YOUR GITHUB OAUTH APP CREATION")
  • Authorization Call (Mandatory)

    It will open the external browser for github oauth authorization.
       override fun onClick(id: Int?) {
           githubAuth.auth()
       }
  • Deep Link Observer (Mandatory)

    It will check when we will return the ==Auth Code== after ==Github Web OAuth== Successfully done.
       override fun onResume() {
           super.onResume()
           githubAuth.onDeepLinkInitializer()
       }
  • Deep Link Manifest (Mandatory)

   <activity
       android:name="Activity Name"
       android:exported="true">
       <intent-filter>
           <action android:name="android.intent.action.VIEW" />
           <category android:name="android.intent.category.DEFAULT" />
           <category android:name="android.intent.category.BROWSABLE" />
           
           <data android:scheme="http" />
           <data android:scheme="https" />
           
           // example: <data android:host="***.firebaseapp.com" />
           <data android:host="Redirect URL" />
       </intent-filter>
   </activity>

Demo Links

IMPORTANT NOTES

==(Feel free to raise a PR)==

Open Source Plugins used

Library README
HttpUrlConnection [https://developer.android.com/reference/java/net/HttpURLConnection]
GSON Serializer [https://github.com/google/gson]
Android STD Libs [https://developer.android.com/kotlin/first]