Skip to content

Commit

Permalink
- Google Authorization repository added
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinJeyakumar committed Feb 25, 2022
1 parent e6bccf2 commit fc24c1f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions buildSrc/src/main/java/libraries/SupportLibraries.gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,11 @@ object ThirdPartyLibraries {
const val auto_starter = "com.github.judemanutd:autostarter:${Versions.auto_starter}"
const val subsampling_scale_image_view = "com.davemorrissey.labs:subsampling-scale-image-view-androidx:${Versions.subsampling_scale_image_view}"
const val otpview_pinview ="com.github.mukeshsolanki:android-otpview-pinview:2.1.2"
}

object GoogleMiscLibraries {
const val playservices_auth = "com.google.android.gms:play-services-auth:20.1.0" //https://developers.google.com/android/guides/setup
const val google_sheets = "com.google.apis:google-api-services-sheets:v4-rev612-1.25.0"//exclude: org.apache.httpcomponents //https://mvnrepository.com/artifact/com.google.apis/google-api-services-sheets/v4-rev612-1.25.0
const val google_oauth_jetty= "com.google.oauth-client:google-oauth-client-jetty:1.33.1" ////https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-jetty/1.33.1
const val google_api_client= "com.google.api-client:google-api-client-android:1.33.2" //exclude: org.apache.httpcomponents https://mvnrepository.com/artifact/com.google.api-client/google-api-client-android/1.33.2
}
8 changes: 8 additions & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ dependencies {
networkLibraries()
firebaseLibraries()
dataStoreLibraries()
implementation(GoogleMiscLibraries.playservices_auth)
implementation(GoogleMiscLibraries.google_sheets){
exclude("org.apache.httpcomponents")
}
implementation(GoogleMiscLibraries.google_oauth_jetty)
implementation(GoogleMiscLibraries.google_api_client) {
exclude("org.apache.httpcomponents")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.data.repositories.remote.google.authentication

import android.app.Activity
import android.app.Activity.RESULT_OK
import android.content.Context
import android.content.Intent
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.nativedevps.support.inline.orElse
import javax.inject.Inject


class GoogleAuthentication @Inject constructor(private val context: Context) {
companion object {
const val CONST_SIGNING_RESULT = 999
}

data class SignInModel(
var email: String,
var givenName: String,
)

/**
* invoke signIn option
**/
fun signIn(
activity: Activity,
signOut: Boolean,
callback: (exception: Exception) -> Unit,
) {
if (signOut) {
getSignInIntent().signOut()
}
val account = getLastSignedAccount()
account?.let {
activity.startActivityForResult(getSignInIntent().signInIntent, CONST_SIGNING_RESULT)
}.orElse {
callback(Exception("already signed in"))
}
}

fun signOut(callback: (exception: java.lang.Exception?) -> Unit) {
getSignInIntent().signOut().addOnCompleteListener {
if (it.isSuccessful) {
callback(null)
} else {
callback(it.exception!!)
}
}
}

/**
* support method to handle activity result to retrieve SignInModel attributes
**/
fun onActivityResult(
resultCode: Int,
data: Intent,
callback: (
signInModel: SignInModel?,
exception: Exception?,
) -> Unit,
) {
if (resultCode == RESULT_OK) {
GoogleSignIn.getSignedInAccountFromIntent(data).apply {
if (isSuccessful) {
getResult(ApiException::class.java).let {
SignInModel(it.email!!, it.givenName ?: it.displayName ?: it.familyName!!)
}.apply {
callback(this, null)
}
} else {
callback(null, Exception(this.exception))
}
}
} else {
callback(null, Exception("action cancelled"))
}
}

private fun getLastSignedAccount(): GoogleSignInAccount? {
return GoogleSignIn.getLastSignedInAccount(context)
}

private fun getSignInIntent(): GoogleSignInClient {
val gso = GoogleSignInOptions.Builder(
GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build()
return GoogleSignIn.getClient(context, gso)
}
}

0 comments on commit fc24c1f

Please sign in to comment.