@@ -10,13 +10,13 @@ import com.google.firebase.FirebasePlatform
10
10
import com.google.firebase.auth.internal.InternalAuthProvider
11
11
import com.google.firebase.internal.InternalTokenResult
12
12
import com.google.firebase.internal.api.FirebaseNoSignedInUserException
13
- import com.squareup.okhttp.*
14
13
import kotlinx.coroutines.Dispatchers
15
14
import kotlinx.coroutines.GlobalScope
16
15
import kotlinx.coroutines.launch
17
16
import kotlinx.serialization.Serializable
18
17
import kotlinx.serialization.Transient
19
18
import kotlinx.serialization.json.*
19
+ import okhttp3.*
20
20
import java.io.IOException
21
21
import java.util.*
22
22
import java.util.concurrent.CopyOnWriteArrayList
@@ -71,12 +71,12 @@ class FirebaseUserImpl private constructor(
71
71
.build()
72
72
FirebaseAuth .getInstance(app).client.newCall(request).enqueue(object : Callback {
73
73
74
- override fun onFailure (request : Request , e : IOException ) {
74
+ override fun onFailure (call : Call , e : IOException ) {
75
75
source.setException(FirebaseException (e.toString(), e))
76
76
}
77
77
78
78
@Throws(IOException ::class )
79
- override fun onResponse (response : Response ) {
79
+ override fun onResponse (call : Call , response : Response ) {
80
80
if (! response.isSuccessful) {
81
81
FirebaseAuth .getInstance(app).signOut()
82
82
source.setException(FirebaseAuthInvalidUserException (
@@ -104,7 +104,11 @@ class FirebaseUserImpl private constructor(
104
104
class FirebaseAuth constructor(val app : FirebaseApp ) : InternalAuthProvider {
105
105
106
106
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()
108
112
109
113
companion object {
110
114
@@ -159,12 +163,6 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
159
163
}
160
164
}
161
165
162
- init {
163
- client.setConnectTimeout(60 , TimeUnit .SECONDS )
164
- client.setReadTimeout(60 , TimeUnit .SECONDS )
165
- client.setWriteTimeout(60 , TimeUnit .SECONDS )
166
- }
167
-
168
166
fun signInAnonymously (): Task <AuthResult > {
169
167
val source = TaskCompletionSource <AuthResult >()
170
168
val body = RequestBody .create(json, JsonObject (mapOf (" returnSecureToken" to JsonPrimitive (true ))).toString())
@@ -174,19 +172,19 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
174
172
.build()
175
173
client.newCall(request).enqueue(object : Callback {
176
174
177
- override fun onFailure (request : Request , e : IOException ) {
175
+ override fun onFailure (call : Call , e : IOException ) {
178
176
source.setException(FirebaseException (e.toString(), e))
179
177
}
180
178
181
179
@Throws(IOException ::class )
182
- override fun onResponse (response : Response ) {
180
+ override fun onResponse (call : Call , response : Response ) {
183
181
if (! response.isSuccessful) {
184
182
source.setException(FirebaseAuthInvalidUserException (
185
183
response.message(),
186
184
formatErrorMessage(" accounts:signUp" , request, response)
187
185
))
188
186
} else {
189
- val body = response.body().use { it.string() }
187
+ val body = response.body()!! .use { it.string() }
190
188
user = FirebaseUserImpl (app, jsonParser.parseToJsonElement(body).jsonObject, true )
191
189
source.setResult(AuthResult { user })
192
190
}
@@ -207,19 +205,19 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
207
205
.build()
208
206
client.newCall(request).enqueue(object : Callback {
209
207
210
- override fun onFailure (request : Request , e : IOException ) {
208
+ override fun onFailure (call : Call , e : IOException ) {
211
209
source.setException(FirebaseException (e.toString(), e))
212
210
}
213
211
214
212
@Throws(IOException ::class )
215
- override fun onResponse (response : Response ) {
213
+ override fun onResponse (call : Call , response : Response ) {
216
214
if (! response.isSuccessful) {
217
215
source.setException(FirebaseAuthInvalidUserException (
218
216
response.message(),
219
217
formatErrorMessage(" verifyCustomToken" , request, response)
220
218
))
221
219
} else {
222
- val body = response.body().use { it.string() }
220
+ val body = response.body()!! .use { it.string() }
223
221
val user = FirebaseUserImpl (app, jsonParser.parseToJsonElement(body).jsonObject)
224
222
refreshToken(user, source) { AuthResult { it } }
225
223
}
@@ -240,19 +238,19 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
240
238
.build()
241
239
client.newCall(request).enqueue(object : Callback {
242
240
243
- override fun onFailure (request : Request , e : IOException ) {
241
+ override fun onFailure (call : Call , e : IOException ) {
244
242
source.setException(FirebaseException (e.toString(), e))
245
243
}
246
244
247
245
@Throws(IOException ::class )
248
- override fun onResponse (response : Response ) {
246
+ override fun onResponse (call : Call , response : Response ) {
249
247
if (! response.isSuccessful) {
250
248
source.setException(FirebaseAuthInvalidUserException (
251
249
response.message(),
252
250
formatErrorMessage(" verifyPassword" , request, response)
253
251
))
254
252
} else {
255
- val body = response.body().use { it.string() }
253
+ val body = response.body()!! .use { it.string() }
256
254
val user = FirebaseUserImpl (app, jsonParser.parseToJsonElement(body).jsonObject)
257
255
refreshToken(user, source) { AuthResult { it } }
258
256
}
@@ -263,8 +261,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
263
261
264
262
internal fun formatErrorMessage (title : String , request : Request , response : Response ): String {
265
263
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() }} "
268
266
}
269
267
270
268
fun signOut () {
@@ -313,17 +311,17 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
313
311
314
312
client.newCall(request).enqueue(object : Callback {
315
313
316
- override fun onFailure (request : Request , e : IOException ) {
314
+ override fun onFailure (call : Call , e : IOException ) {
317
315
source.setException(FirebaseException (e.toString(), e))
318
316
}
319
317
320
318
@Throws(IOException ::class )
321
- override fun onResponse (response : Response ) {
319
+ override fun onResponse (call : Call , response : Response ) {
322
320
response.body().use { body ->
323
321
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()} " )
325
323
} else {
326
- jsonParser.parseToJsonElement(body.string()).jsonObject.apply {
324
+ jsonParser.parseToJsonElement(body!! .string()).jsonObject.apply {
327
325
val user = FirebaseUserImpl (app, this , user.isAnonymous)
328
326
if (user.claims[" aud" ] != app.options.projectId) {
329
327
signOutAndThrowInvalidUserException(
0 commit comments