Skip to content

Commit 5f7339a

Browse files
committed
fix: initialize serverClientId to default_web_client_id in AuthProvider.Google
- The `applicationId` parameter has been removed from the `AuthProvider.Facebook` constructor. Configuration values (`facebook_application_id`, `facebook_login_protocol_scheme`, and `facebook_client_token`) are now exclusively read from `strings.xml`. - The `serverClientId` for the Google provider is now automatically populated from `R.string.default_web_client_id` if it is not explicitly provided. - Added validation to ensure `facebook_client_token` and `facebook_login_protocol_scheme` are configured in `strings.xml`. - Updated tests to use resource files for provider configuration instead of constructor arguments. - Updated `README.md` to reflect the new configuration requirements, including adding `facebook_client_token`.
1 parent 9abf3d1 commit 5f7339a

File tree

6 files changed

+65
-53
lines changed

6 files changed

+65
-53
lines changed

auth/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ If using Facebook Login, add your Facebook App ID to `strings.xml`:
138138
<resources>
139139
<string name="facebook_application_id" translatable="false">YOUR_FACEBOOK_APP_ID</string>
140140
<string name="facebook_login_protocol_scheme" translatable="false">fbYOUR_FACEBOOK_APP_ID</string>
141+
<string name="facebook_client_token" translatable="false">CHANGE-ME</string>
141142
</resources>
142143
```
143144

@@ -489,9 +490,6 @@ Configure Facebook Login with optional permissions:
489490

490491
```kotlin
491492
val facebookProvider = AuthProvider.Facebook(
492-
// Optional: Facebook application ID (reads from strings.xml if not provided)
493-
applicationId = "YOUR_FACEBOOK_APP_ID",
494-
495493
// Optional: Permissions to request (default: ["email", "public_profile"])
496494
scopes = listOf("email", "public_profile", "user_friends"),
497495

auth/src/main/java/com/firebase/ui/auth/configuration/auth_provider/AuthProvider.kt

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ abstract class AuthProvider(open val providerId: String, open val providerName:
485485
/**
486486
* The OAuth 2.0 client ID for your server.
487487
*/
488-
val serverClientId: String?,
488+
var serverClientId: String?,
489489

490490
/**
491491
* A map of custom OAuth parameters.
@@ -505,8 +505,9 @@ abstract class AuthProvider(open val providerId: String, open val providerName:
505505
" default_web_client_id string wasn't populated.",
506506
R.string.default_web_client_id
507507
)
508+
serverClientId = context.getString(R.string.default_web_client_id)
508509
} else {
509-
require(serverClientId.isNotBlank()) {
510+
require(serverClientId!!.isNotBlank()) {
510511
"Server client ID cannot be blank."
511512
}
512513
}
@@ -529,7 +530,7 @@ abstract class AuthProvider(open val providerId: String, open val providerName:
529530
val credential: AuthCredential,
530531
val idToken: String,
531532
val displayName: String?,
532-
val photoUrl: Uri?
533+
val photoUrl: Uri?,
533534
)
534535

535536
/**
@@ -567,7 +568,7 @@ abstract class AuthProvider(open val providerId: String, open val providerName:
567568
credentialManager: CredentialManager,
568569
serverClientId: String,
569570
filterByAuthorizedAccounts: Boolean,
570-
autoSelectEnabled: Boolean
571+
autoSelectEnabled: Boolean,
571572
): GoogleSignInResult
572573

573574
suspend fun clearCredentialState(
@@ -600,8 +601,10 @@ abstract class AuthProvider(open val providerId: String, open val providerName:
600601
.build()
601602

602603
val result = credentialManager.getCredential(context, request)
603-
val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(result.credential.data)
604-
val credential = GoogleAuthProvider.getCredential(googleIdTokenCredential.idToken, null)
604+
val googleIdTokenCredential =
605+
GoogleIdTokenCredential.createFrom(result.credential.data)
606+
val credential =
607+
GoogleAuthProvider.getCredential(googleIdTokenCredential.idToken, null)
605608

606609
return GoogleSignInResult(
607610
credential = credential,
@@ -624,11 +627,6 @@ abstract class AuthProvider(open val providerId: String, open val providerName:
624627
* Facebook Login provider configuration.
625628
*/
626629
class Facebook(
627-
/**
628-
* The Facebook application ID.
629-
*/
630-
val applicationId: String? = null,
631-
632630
/**
633631
* The list of scopes (permissions) to request. Defaults to email and public_profile.
634632
*/
@@ -653,18 +651,26 @@ abstract class AuthProvider(open val providerId: String, open val providerName:
653651
)
654652
}
655653

656-
if (applicationId == null) {
657-
Preconditions.checkConfigured(
658-
context,
659-
"Facebook provider unconfigured. Make sure to " +
660-
"add a `facebook_application_id` string or provide applicationId parameter.",
661-
R.string.facebook_application_id
662-
)
663-
} else {
664-
require(applicationId.isNotBlank()) {
665-
"Facebook application ID cannot be blank"
666-
}
667-
}
654+
Preconditions.checkConfigured(
655+
context,
656+
"Facebook provider unconfigured. Make sure to " +
657+
"add a `facebook_application_id` string to your strings.xml",
658+
R.string.facebook_application_id
659+
)
660+
661+
Preconditions.checkConfigured(
662+
context,
663+
"Facebook provider unconfigured. Make sure to " +
664+
"add a `facebook_login_protocol_scheme` string to your strings.xml",
665+
R.string.facebook_login_protocol_scheme
666+
)
667+
668+
Preconditions.checkConfigured(
669+
context,
670+
"Facebook provider unconfigured. Make sure to " +
671+
"add a `facebook_client_token` string to your strings.xml",
672+
R.string.facebook_client_token
673+
)
668674
}
669675

670676
/**

auth/src/test/java/com/firebase/ui/auth/configuration/AuthUIConfigurationTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,13 @@ class AuthUIConfigurationTest {
294294
}
295295

296296
@Test
297+
@Config(manifest = Config.NONE, qualifiers = "night")
297298
fun `validation accepts all supported providers`() {
298299
val config = authUIConfiguration {
299300
context = applicationContext
300301
providers {
301302
provider(AuthProvider.Google(scopes = listOf(), serverClientId = "test_client_id"))
302-
provider(AuthProvider.Facebook(applicationId = "test_app_id"))
303+
provider(AuthProvider.Facebook())
303304
provider(AuthProvider.Twitter(customParameters = mapOf()))
304305
provider(AuthProvider.Github(customParameters = mapOf()))
305306
provider(AuthProvider.Microsoft(customParameters = mapOf(), tenant = null))

auth/src/test/java/com/firebase/ui/auth/configuration/auth_provider/AuthProviderTest.kt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.firebase.ui.auth.configuration.auth_provider
22

33
import android.content.Context
44
import androidx.test.core.app.ApplicationProvider
5+
import com.firebase.ui.auth.R
56
import com.google.common.truth.Truth.assertThat
67
import com.google.firebase.auth.actionCodeSettings
78
import org.junit.Before
@@ -264,32 +265,34 @@ class AuthProviderTest {
264265
}
265266
}
266267

268+
@Test
269+
@Config(manifest = Config.NONE, qualifiers = "night")
270+
fun `google provider assigns default_web_client_id to serverClientId when null`() {
271+
val provider = AuthProvider.Google(
272+
scopes = listOf("email"),
273+
serverClientId = null
274+
)
275+
276+
provider.validate(applicationContext)
277+
278+
assertThat(provider.serverClientId)
279+
.isEqualTo(applicationContext.getString(R.string.default_web_client_id))
280+
}
281+
267282
// =============================================================================================
268283
// Facebook Provider Tests
269284
// =============================================================================================
270285

271286
@Test
287+
@Config(manifest = Config.NONE, qualifiers = "night")
272288
fun `facebook provider with valid configuration should succeed`() {
273-
val provider = AuthProvider.Facebook(applicationId = "application_id")
289+
val provider = AuthProvider.Facebook()
274290

275291
provider.validate(applicationContext)
276292
}
277293

278294
@Test
279-
fun `facebook provider with empty application id throws`() {
280-
val provider = AuthProvider.Facebook(applicationId = "")
281-
282-
try {
283-
provider.validate(applicationContext)
284-
assertThat(false).isTrue() // Should not reach here
285-
} catch (e: Exception) {
286-
assertThat(e).isInstanceOf(IllegalArgumentException::class.java)
287-
assertThat(e.message).isEqualTo("Facebook application ID cannot be blank")
288-
}
289-
}
290-
291-
@Test
292-
fun `facebook provider validates facebook_application_id when applicationId is null`() {
295+
fun `facebook provider validates facebook_application_id`() {
293296
val provider = AuthProvider.Facebook()
294297

295298
try {
@@ -299,7 +302,7 @@ class AuthProviderTest {
299302
assertThat(e).isInstanceOf(IllegalStateException::class.java)
300303
assertThat(e.message).isEqualTo(
301304
"Facebook provider unconfigured. Make sure to " +
302-
"add a `facebook_application_id` string or provide applicationId parameter."
305+
"add a `facebook_application_id` string to your strings.xml"
303306
)
304307
}
305308
}
@@ -400,4 +403,4 @@ class AuthProviderTest {
400403
assertThat(e.message).isEqualTo("Button label cannot be null or empty")
401404
}
402405
}
403-
}
406+
}

auth/src/test/java/com/firebase/ui/auth/configuration/auth_provider/FacebookAuthProviderFirebaseAuthUI.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class FacebookAuthProviderFirebaseAuthUITest {
103103
}
104104

105105
@Test
106+
@Config(manifest = Config.NONE, qualifiers = "night")
106107
fun `signInWithFacebook - successful sign in signs user in and emits Success authState`() = runTest {
107108
val authStateListeners = mutableListOf<AuthStateListener>()
108109
doAnswer { invocation ->
@@ -118,9 +119,7 @@ class FacebookAuthProviderFirebaseAuthUITest {
118119
whenever(mockFirebaseAuth.currentUser).thenReturn(null)
119120

120121
val instance = FirebaseAuthUI.create(firebaseApp, mockFirebaseAuth)
121-
val provider = spy(AuthProvider.Facebook(
122-
applicationId = "000000000000"
123-
))
122+
val provider = spy(AuthProvider.Facebook())
124123
val config = authUIConfiguration {
125124
context = applicationContext
126125
providers {
@@ -175,6 +174,7 @@ class FacebookAuthProviderFirebaseAuthUITest {
175174
}
176175

177176
@Test
177+
@Config(manifest = Config.NONE, qualifiers = "night")
178178
fun `signInWithFacebook - handles account collision by saving credential and emitting error`() = runTest {
179179
EmailLinkPersistenceManager.default.clear(applicationContext)
180180
EmailLinkPersistenceManager.default.saveEmail(
@@ -185,9 +185,7 @@ class FacebookAuthProviderFirebaseAuthUITest {
185185
)
186186

187187
val instance = FirebaseAuthUI.create(firebaseApp, mockFirebaseAuth)
188-
val provider = spy(AuthProvider.Facebook(
189-
applicationId = "000000000000"
190-
))
188+
val provider = spy(AuthProvider.Facebook())
191189
val config = authUIConfiguration {
192190
context = applicationContext
193191
providers {
@@ -238,11 +236,10 @@ class FacebookAuthProviderFirebaseAuthUITest {
238236
}
239237

240238
@Test
239+
@Config(manifest = Config.NONE, qualifiers = "night")
241240
fun `signInWithFacebook - converts FacebookException into AuthException`() = runTest {
242241
val instance = FirebaseAuthUI.create(firebaseApp, mockFirebaseAuth)
243-
val provider = spy(AuthProvider.Facebook(
244-
applicationId = "000000000000"
245-
))
242+
val provider = spy(AuthProvider.Facebook())
246243
val config = authUIConfiguration {
247244
context = applicationContext
248245
providers {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="default_web_client_id" translatable="false">test_client_id</string>
4+
<string name="facebook_application_id" translatable="false">test_app_id</string>
5+
<string name="facebook_login_protocol_scheme" translatable="false">test_login_scheme</string>
6+
<string name="facebook_client_token" translatable="false">test_client_token</string>
7+
</resources>

0 commit comments

Comments
 (0)