Skip to content

Commit c751172

Browse files
committed
Merge remote-tracking branch 'origin/master' into firestore-persistence
# Conflicts: # build.gradle.kts
2 parents 7506a3e + 5859a10 commit c751172

File tree

4 files changed

+34
-37
lines changed

4 files changed

+34
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Is the Firebase library or API you need missing? [Create an issue](https://githu
109109

110110
Currently, the following limitations are observed:
111111

112-
* Firebase Auth implementation is minimal and only supports a small subset of the API:
112+
#### Firebase Auth implementation is minimal and only supports a small subset of the API:
113113
- `signInAnonymously`
114114
- `signInWithCustomToken`
115115
- `signInWithEmailAndPassword`
@@ -121,14 +121,14 @@ Currently, the following limitations are observed:
121121
- `addAuthStateListener`
122122
- `removeAuthStateListener`
123123
- `signOut`
124-
* Cloud Firestore does not support [Offline Persistence](https://firebase.google.com/docs/firestore/manage-data/enable-offline#configure_offline_persistence) and should be setup as follows:
124+
#### Cloud Firestore does not support [Offline Persistence](https://firebase.google.com/docs/firestore/manage-data/enable-offline#configure_offline_persistence) and should be setup as follows:
125125
```java
126126
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder(db.getFirestoreSettings())
127127
.setPersistenceEnabled(false)
128128
.build();
129129
db.setFirestoreSettings(settings);
130130
```
131-
* Realtime Database does not support [Disk Persistence](https://firebase.google.com/docs/database/android/offline-capabilities), and should be setup as follows:
131+
#### Realtime Database does not support [Disk Persistence](https://firebase.google.com/docs/database/android/offline-capabilities), and should be setup as follows:
132132
```java
133133
FirebaseDatabase.getInstance().setPersistenceEnabled(false)
134134
```

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,10 @@ dependencies {
163163
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
164164
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.2")
165165
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
166-
implementation("com.squareup.okhttp:okhttp:2.7.5")
167166
implementation("org.xerial:sqlite-jdbc:3.44.1.0")
168167
// firebase dependencies
168+
implementation("javax.inject:javax.inject:1")
169+
implementation("com.squareup.okhttp3:okhttp:3.12.13")
169170
implementation("android.arch.lifecycle:common:1.1.1")
170171
implementation("io.grpc:grpc-protobuf-lite:1.52.1")
171172
implementation("io.grpc:grpc-stub:1.52.1")

src/main/java/android/content/Context.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@ open class Context {
5656
}
5757

5858
override fun getString(key: String, defaultValue: String?): String? {
59-
when (key) {
60-
"|T|app.teamhub.core|*" -> return null
61-
"|T|dev.teamhub.core|*" -> return null
62-
"last-used-date" -> return FirebasePlatform.firebasePlatform.retrieve(key) ?: defaultValue
63-
else -> if(key.startsWith("com.google.firebase.auth.FIREBASE_USER")) {
64-
return FirebasePlatform.firebasePlatform.retrieve(key) ?: defaultValue
65-
}
59+
return when {
60+
key == "last-used-date" -> FirebasePlatform.firebasePlatform.retrieve(key) ?: defaultValue
61+
key.contains("|T|") -> null
62+
key.startsWith("com.google.firebase.auth.FIREBASE_USER") ->
63+
FirebasePlatform.firebasePlatform.retrieve(key) ?: defaultValue
64+
else -> throw IllegalArgumentException(key)
6665
}
67-
throw IllegalArgumentException(key)
6866
}
6967

7068
override fun getLong(key: String?, defaultValue: Long): Long {

src/main/java/com/google/firebase/auth/FirebaseAuth.kt

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import com.google.firebase.FirebasePlatform
1010
import com.google.firebase.auth.internal.InternalAuthProvider
1111
import com.google.firebase.internal.InternalTokenResult
1212
import com.google.firebase.internal.api.FirebaseNoSignedInUserException
13-
import com.squareup.okhttp.*
1413
import kotlinx.coroutines.Dispatchers
1514
import kotlinx.coroutines.GlobalScope
1615
import kotlinx.coroutines.launch
1716
import kotlinx.serialization.Serializable
1817
import kotlinx.serialization.Transient
1918
import kotlinx.serialization.json.*
19+
import okhttp3.*
2020
import java.io.IOException
2121
import java.util.*
2222
import java.util.concurrent.CopyOnWriteArrayList
@@ -71,12 +71,12 @@ class FirebaseUserImpl private constructor(
7171
.build()
7272
FirebaseAuth.getInstance(app).client.newCall(request).enqueue(object : Callback {
7373

74-
override fun onFailure(request: Request, e: IOException) {
74+
override fun onFailure(call: Call, e: IOException) {
7575
source.setException(FirebaseException(e.toString(), e))
7676
}
7777

7878
@Throws(IOException::class)
79-
override fun onResponse(response: Response) {
79+
override fun onResponse(call: Call, response: Response) {
8080
if (!response.isSuccessful) {
8181
FirebaseAuth.getInstance(app).signOut()
8282
source.setException(FirebaseAuthInvalidUserException(
@@ -104,7 +104,11 @@ class FirebaseUserImpl private constructor(
104104
class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
105105

106106
val json = MediaType.parse("application/json; charset=utf-8")
107-
val client = OkHttpClient()
107+
val client: OkHttpClient = OkHttpClient.Builder()
108+
.connectTimeout(60, TimeUnit.SECONDS)
109+
.readTimeout(60, TimeUnit.SECONDS)
110+
.writeTimeout(60, TimeUnit.SECONDS)
111+
.build()
108112

109113
companion object {
110114

@@ -159,12 +163,6 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
159163
}
160164
}
161165

162-
init {
163-
client.setConnectTimeout(60, TimeUnit.SECONDS)
164-
client.setReadTimeout(60, TimeUnit.SECONDS)
165-
client.setWriteTimeout(60, TimeUnit.SECONDS)
166-
}
167-
168166
fun signInAnonymously(): Task<AuthResult> {
169167
val source = TaskCompletionSource<AuthResult>()
170168
val body = RequestBody.create(json, JsonObject(mapOf("returnSecureToken" to JsonPrimitive(true))).toString())
@@ -174,19 +172,19 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
174172
.build()
175173
client.newCall(request).enqueue(object : Callback {
176174

177-
override fun onFailure(request: Request, e: IOException) {
175+
override fun onFailure(call: Call, e: IOException) {
178176
source.setException(FirebaseException(e.toString(), e))
179177
}
180178

181179
@Throws(IOException::class)
182-
override fun onResponse(response: Response) {
180+
override fun onResponse(call: Call, response: Response) {
183181
if (!response.isSuccessful) {
184182
source.setException(FirebaseAuthInvalidUserException(
185183
response.message(),
186184
formatErrorMessage("accounts:signUp", request, response)
187185
))
188186
} else {
189-
val body = response.body().use { it.string() }
187+
val body = response.body()!!.use { it.string() }
190188
user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject, true)
191189
source.setResult(AuthResult { user })
192190
}
@@ -207,19 +205,19 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
207205
.build()
208206
client.newCall(request).enqueue(object : Callback {
209207

210-
override fun onFailure(request: Request, e: IOException) {
208+
override fun onFailure(call: Call, e: IOException) {
211209
source.setException(FirebaseException(e.toString(), e))
212210
}
213211

214212
@Throws(IOException::class)
215-
override fun onResponse(response: Response) {
213+
override fun onResponse(call: Call, response: Response) {
216214
if (!response.isSuccessful) {
217215
source.setException(FirebaseAuthInvalidUserException(
218216
response.message(),
219217
formatErrorMessage("verifyCustomToken", request, response)
220218
))
221219
} else {
222-
val body = response.body().use { it.string() }
220+
val body = response.body()!!.use { it.string() }
223221
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject)
224222
refreshToken(user, source) { AuthResult { it } }
225223
}
@@ -240,19 +238,19 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
240238
.build()
241239
client.newCall(request).enqueue(object : Callback {
242240

243-
override fun onFailure(request: Request, e: IOException) {
241+
override fun onFailure(call: Call, e: IOException) {
244242
source.setException(FirebaseException(e.toString(), e))
245243
}
246244

247245
@Throws(IOException::class)
248-
override fun onResponse(response: Response) {
246+
override fun onResponse(call: Call, response: Response) {
249247
if (!response.isSuccessful) {
250248
source.setException(FirebaseAuthInvalidUserException(
251249
response.message(),
252250
formatErrorMessage("verifyPassword", request, response)
253251
))
254252
} else {
255-
val body = response.body().use { it.string() }
253+
val body = response.body()!!.use { it.string() }
256254
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject)
257255
refreshToken(user, source) { AuthResult { it } }
258256
}
@@ -263,8 +261,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
263261

264262
internal fun formatErrorMessage(title: String, request: Request, response: Response): String {
265263
return "$title API returned an error, " +
266-
"with url [${request.method()}] ${request.urlString()} ${request.body()} -- " +
267-
"response [${response.code()}] ${response.message()} ${response.body().use { it.string() }}"
264+
"with url [${request.method()}] ${request.url()} ${request.body()} -- " +
265+
"response [${response.code()}] ${response.message()} ${response.body().use { it?.string() }}"
268266
}
269267

270268
fun signOut() {
@@ -313,17 +311,17 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
313311

314312
client.newCall(request).enqueue(object : Callback {
315313

316-
override fun onFailure(request: Request, e: IOException) {
314+
override fun onFailure(call: Call, e: IOException) {
317315
source.setException(FirebaseException(e.toString(), e))
318316
}
319317

320318
@Throws(IOException::class)
321-
override fun onResponse(response: Response) {
319+
override fun onResponse(call: Call, response: Response) {
322320
response.body().use { body ->
323321
if (!response.isSuccessful) {
324-
body.string().let { signOutAndThrowInvalidUserException(it, "token API returned an error: $it") }
322+
signOutAndThrowInvalidUserException(body?.string().orEmpty(), "token API returned an error: ${body?.string()}")
325323
} else {
326-
jsonParser.parseToJsonElement(body.string()).jsonObject.apply {
324+
jsonParser.parseToJsonElement(body!!.string()).jsonObject.apply {
327325
val user = FirebaseUserImpl(app, this, user.isAnonymous)
328326
if (user.claims["aud"] != app.options.projectId) {
329327
signOutAndThrowInvalidUserException(

0 commit comments

Comments
 (0)