Skip to content

Commit

Permalink
provide all GoogleSignInAccount details to login
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Marciset committed Dec 15, 2022
1 parent 81f129f commit 8e6e9fd
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions android/login/GoogleSignInLogin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import com.qmobile.qmobileui.ui.setOnVeryLongClickListener
import com.qmobile.qmobileui.utils.parcelable
import ___APP_PACKAGE___.R
import ___APP_PACKAGE___.databinding.GoogleSignInLoginBinding
import org.json.JSONArray
import org.json.JSONObject
import timber.log.Timber

@LoginForm
Expand Down Expand Up @@ -64,7 +66,7 @@ class GoogleSignInLogin(private val activity: LoginActivity) : LoginHandler {
signIn()
}
}

override fun validate(input: String): Boolean {
return true
}
Expand Down Expand Up @@ -131,20 +133,46 @@ class GoogleSignInLogin(private val activity: LoginActivity) : LoginHandler {

private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
try {
completedTask.getResult(ApiException::class.java).email?.let {
activity.login(it)
} ?: kotlin.run {
val googleSignInAccount: GoogleSignInAccount = completedTask.getResult(ApiException::class.java)
if (googleSignInAccount.email.isNullOrEmpty()) {
SnackbarHelper.show(
activity,
"No email attached to this account",
ToastMessage.Type.WARNING
)
return
}
activity.login(googleSignInAccount.buildJson().toString())
} catch (e: ApiException) {
handleError(e.statusCode, true)
}
}

private fun GoogleSignInAccount.buildJson(): JSONObject {
return JSONObject().apply {
put("email", email)
id?.let { put("id", it) }
displayName?.let { put("displayName", it) }
familyName?.let { put("familyName", it) }
givenName?.let { put("givenName", it) }
idToken?.let { put("idToken", it) }
photoUrl?.let { put("photoUrl", it) }
serverAuthCode?.let { put("serverAuthCode", it) }
put("isExpired", isExpired)
val accountJson = JSONObject().apply {
account?.let {
put("name", it.name)
put("type", it.type)
}
}
put("account", accountJson)
val grantedScopesArray = JSONArray(grantedScopes.map { it.scopeUri })
put("grantedScopes", grantedScopesArray)
val requestedScopesArray = JSONArray(requestedScopes.map { it.scopeUri })
put("requestedScopes", requestedScopesArray)
}
}

private fun handleError(statusCode: Int, showError: Boolean) {
Timber.e("status code: $statusCode")
val statusCodeString = GoogleSignInStatusCodes.getStatusCodeString(statusCode)
Expand Down

0 comments on commit 8e6e9fd

Please sign in to comment.