Skip to content

Commit

Permalink
Adding scopes into GoogleButtonUiContainer and GoogleButtonUiCOntaine…
Browse files Browse the repository at this point in the history
…rFirebase
  • Loading branch information
mirzemehdi committed Jan 18, 2025
1 parent 4575dea commit 65ab3e5
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ kotlin.mpp.enableCInteropCommonization=true
#Development
development=true

kmpAuthVersion=2.3.0
kmpAuthVersion=2.3.1
2 changes: 1 addition & 1 deletion kmpauth-firebase/api/android/kmpauth-firebase.api
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class com/mmk/kmpauth/firebase/github/GithubButtonUiContainerKt {

public final class com/mmk/kmpauth/firebase/google/GoogleButtonUiContainerFirebaseKt {
public static final fun GoogleButtonUiContainerFirebase (Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
public static final fun GoogleButtonUiContainerFirebase (Landroidx/compose/ui/Modifier;ZZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
public static final fun GoogleButtonUiContainerFirebase (Landroidx/compose/ui/Modifier;ZZLjava/util/List;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
}

public final class com/mmk/kmpauth/firebase/oauth/OAuthContainer_androidKt {
Expand Down
2 changes: 1 addition & 1 deletion kmpauth-firebase/api/jvm/kmpauth-firebase.api
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class com/mmk/kmpauth/firebase/github/GithubButtonUiContainerKt {

public final class com/mmk/kmpauth/firebase/google/GoogleButtonUiContainerFirebaseKt {
public static final fun GoogleButtonUiContainerFirebase (Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
public static final fun GoogleButtonUiContainerFirebase (Landroidx/compose/ui/Modifier;ZZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
public static final fun GoogleButtonUiContainerFirebase (Landroidx/compose/ui/Modifier;ZZLjava/util/List;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
}

public final class com/mmk/kmpauth/firebase/oauth/OAuthContainer_jvmKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import kotlinx.coroutines.launch
* Child of this Composable can be any view or Composable function.
* You need to call [UiContainerScope.onClick] function on your child view's click function.
*
* @param linkAccount Default value is false
* @param filterByAuthorizedAccounts set to true so users can choose between available accounts to sign in.
* @param scopes Custom scopes to retrieve more information. Default value listOf("email", "profile")
* [onResult] callback will return [Result] with [FirebaseUser] type.
*
* Example Usage:
Expand All @@ -36,6 +39,7 @@ public fun GoogleButtonUiContainerFirebase(
modifier: Modifier = Modifier,
linkAccount: Boolean = false,
filterByAuthorizedAccounts: Boolean = false,
scopes: List<String> = listOf("email", "profile"),
onResult: (Result<FirebaseUser?>) -> Unit,
content: @Composable UiContainerScope.() -> Unit,
) {
Expand All @@ -45,6 +49,7 @@ public fun GoogleButtonUiContainerFirebase(
GoogleButtonUiContainer(
modifier = modifier,
filterByAuthorizedAccounts = filterByAuthorizedAccounts,
scopes = scopes,
onGoogleSignInResult = { googleUser ->
val idToken = googleUser?.idToken
val accessToken = googleUser?.accessToken
Expand Down
2 changes: 1 addition & 1 deletion kmpauth-google/api/android/kmpauth-google.api
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class com/mmk/kmpauth/google/GoogleAuthUiProvider$DefaultImpls {
}

public final class com/mmk/kmpauth/google/GoogleButtonUiContainerKt {
public static final fun GoogleButtonUiContainer (Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
public static final fun GoogleButtonUiContainer (Landroidx/compose/ui/Modifier;ZLjava/util/List;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
}

public final class com/mmk/kmpauth/google/GoogleUser {
Expand Down
2 changes: 1 addition & 1 deletion kmpauth-google/api/jvm/kmpauth-google.api
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class com/mmk/kmpauth/google/GoogleAuthUiProvider$DefaultImpls {
}

public final class com/mmk/kmpauth/google/GoogleButtonUiContainerKt {
public static final fun GoogleButtonUiContainer (Landroidx/compose/ui/Modifier;ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
public static final fun GoogleButtonUiContainer (Landroidx/compose/ui/Modifier;ZLjava/util/List;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
}

public final class com/mmk/kmpauth/google/GoogleUser {
Expand Down
2 changes: 1 addition & 1 deletion kmpauth-google/kmpauth_google.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'kmpauth_google'
spec.version = '2.3.0'
spec.version = '2.3.1'
spec.homepage = ''
spec.source = { :http=> ''}
spec.authors = ''
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mmk.kmpauth.google

import android.content.Context
import androidx.credentials.Credential
import androidx.credentials.CredentialManager
import androidx.credentials.CustomCredential
import androidx.credentials.GetCredentialRequest
Expand Down Expand Up @@ -32,23 +31,40 @@ internal class GoogleAuthUiProviderImpl(

getGoogleUserFromCredential(filterByAuthorizedAccounts = filterByAuthorizedAccounts)
} catch (e: NoCredentialException) {
if (!filterByAuthorizedAccounts) return handleCredentialException(e)
if (!filterByAuthorizedAccounts)
return handleCredentialException(
e = e,
filterByAuthorizedAccounts = filterByAuthorizedAccounts,
scopes = scopes
)
try {
getGoogleUserFromCredential(filterByAuthorizedAccounts = false)
} catch (e: GetCredentialException) {
handleCredentialException(e)
handleCredentialException(
e = e,
filterByAuthorizedAccounts = filterByAuthorizedAccounts,
scopes = scopes
)
} catch (e: NullPointerException) {
null
}
} catch (e: GetCredentialException) {
handleCredentialException(e)
handleCredentialException(
e = e,
filterByAuthorizedAccounts = filterByAuthorizedAccounts,
scopes = scopes
)
} catch (e: NullPointerException) {
null
}
return googleUser
}

private suspend fun handleCredentialException(e: GetCredentialException): GoogleUser? {
private suspend fun handleCredentialException(
e: GetCredentialException,
filterByAuthorizedAccounts: Boolean,
scopes: List<String>
): GoogleUser? {
println("GoogleAuthUiProvider error: ${e.message}")
val shouldCheckLegacyAuthServices = when (e) {
is GetCredentialProviderConfigurationException -> true
Expand All @@ -57,15 +73,21 @@ internal class GoogleAuthUiProviderImpl(
else -> false
}
return if (shouldCheckLegacyAuthServices) {
checkLegacyGoogleSignIn()
checkLegacyGoogleSignIn(filterByAuthorizedAccounts, scopes)
} else {
null
}
}

private suspend fun checkLegacyGoogleSignIn(): GoogleUser? {
private suspend fun checkLegacyGoogleSignIn(
filterByAuthorizedAccounts: Boolean,
scopes: List<String>
): GoogleUser? {
println("GoogleAuthUiProvider: Checking Outdated Google Sign In...")
return googleLegacyAuthentication.signIn()
return googleLegacyAuthentication.signIn(
filterByAuthorizedAccounts = filterByAuthorizedAccounts,
scopes = scopes
)
}

private suspend fun getGoogleUserFromCredential(filterByAuthorizedAccounts: Boolean): GoogleUser? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import com.mmk.kmpauth.core.UiContainerScope
import com.mmk.kmpauth.google.GoogleAuthUiProvider.Companion.BASIC_AUTH_SCOPE
import kotlinx.coroutines.launch

/**
Expand All @@ -32,12 +33,14 @@ import kotlinx.coroutines.launch
* ```
*
* @param filterByAuthorizedAccounts set to true so users can choose between available accounts to sign in.
* @param scopes Custom scopes to retrieve more information. Default value listOf("email", "profile")
* setting to false list any accounts that have previously been used to sign in to your app.
*/
@Composable
public fun GoogleButtonUiContainer(
modifier: Modifier = Modifier,
filterByAuthorizedAccounts: Boolean = false,
scopes: List<String> = BASIC_AUTH_SCOPE,
onGoogleSignInResult: (GoogleUser?) -> Unit,
content: @Composable UiContainerScope.() -> Unit,
) {
Expand All @@ -51,7 +54,10 @@ public fun GoogleButtonUiContainer(
override fun onClick() {
println("GoogleUiButtonContainer is clicked")
coroutineScope.launch {
val googleUser = googleAuthUiProvider.signIn(filterByAuthorizedAccounts)
val googleUser = googleAuthUiProvider.signIn(
filterByAuthorizedAccounts = filterByAuthorizedAccounts,
scopes = scopes
)
updatedOnResultFunc(googleUser)
}
}
Expand Down

0 comments on commit 65ab3e5

Please sign in to comment.