@@ -15,10 +15,29 @@ import kotlinx.coroutines.GlobalScope
1515import kotlinx.coroutines.launch
1616import kotlinx.serialization.Serializable
1717import kotlinx.serialization.Transient
18- import kotlinx.serialization.json.*
19- import okhttp3.*
18+ import kotlinx.serialization.json.Json
19+ import kotlinx.serialization.json.JsonArray
20+ import kotlinx.serialization.json.JsonElement
21+ import kotlinx.serialization.json.JsonNull
22+ import kotlinx.serialization.json.JsonObject
23+ import kotlinx.serialization.json.JsonPrimitive
24+ import kotlinx.serialization.json.booleanOrNull
25+ import kotlinx.serialization.json.contentOrNull
26+ import kotlinx.serialization.json.doubleOrNull
27+ import kotlinx.serialization.json.int
28+ import kotlinx.serialization.json.intOrNull
29+ import kotlinx.serialization.json.jsonObject
30+ import kotlinx.serialization.json.jsonPrimitive
31+ import kotlinx.serialization.json.longOrNull
32+ import okhttp3.Call
33+ import okhttp3.Callback
34+ import okhttp3.MediaType
35+ import okhttp3.OkHttpClient
36+ import okhttp3.Request
37+ import okhttp3.RequestBody
38+ import okhttp3.Response
2039import java.io.IOException
21- import java.util.*
40+ import java.util.Base64
2241import java.util.concurrent.CopyOnWriteArrayList
2342import java.util.concurrent.TimeUnit
2443
@@ -54,7 +73,7 @@ class FirebaseUserImpl private constructor(
5473 .orEmpty()
5574 }
5675
57- val JsonElement .value get(): Any? = when (this ) {
76+ val JsonElement .value get(): Any? = when (this ) {
5877 is JsonNull -> null
5978 is JsonArray -> map { it.value }
6079 is JsonObject -> jsonObject.mapValues { (_, it) -> it.value }
@@ -79,10 +98,12 @@ class FirebaseUserImpl private constructor(
7998 override fun onResponse (call : Call , response : Response ) {
8099 if (! response.isSuccessful) {
81100 FirebaseAuth .getInstance(app).signOut()
82- source.setException(FirebaseAuthInvalidUserException (
83- response.message(),
84- FirebaseAuth .getInstance(app).formatErrorMessage(" deleteAccount" , request, response)
85- ))
101+ source.setException(
102+ FirebaseAuthInvalidUserException (
103+ response.message(),
104+ FirebaseAuth .getInstance(app).formatErrorMessage(" deleteAccount" , request, response)
105+ )
106+ )
86107 } else {
87108 source.setResult(null )
88109 }
@@ -98,7 +119,6 @@ class FirebaseUserImpl private constructor(
98119 }
99120
100121 override fun getIdToken (forceRefresh : Boolean ) = FirebaseAuth .getInstance(app).getAccessToken(forceRefresh)
101-
102122}
103123
104124class FirebaseAuth constructor(val app : FirebaseApp ) : InternalAuthProvider {
@@ -126,7 +146,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
126146 val currentUser: FirebaseUser ?
127147 get() = user
128148
129- val FirebaseApp .key get() = " com.google.firebase.auth.FIREBASE_USER${" [${ name} ]" .takeUnless { isDefaultApp }.orEmpty()} "
149+ val FirebaseApp .key get() = " com.google.firebase.auth.FIREBASE_USER${" [$name ]" .takeUnless { isDefaultApp }.orEmpty()} "
130150
131151 private var user: FirebaseUserImpl ? = FirebasePlatform .firebasePlatform
132152 .runCatching { retrieve(app.key)?.let { FirebaseUserImpl (app, jsonParser.parseToJsonElement(it).jsonObject) } }
@@ -155,7 +175,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
155175 Log .i(" FirebaseAuth" , " Calling onIdTokenChanged for ${value?.uid} on listener $listener " )
156176 listener.onIdTokenChanged(result)
157177 }
158- for (listener in idTokenListeners) {
178+ for (listener in idTokenListeners) {
159179 listener.onIdTokenChanged(this @FirebaseAuth)
160180 }
161181 }
@@ -179,10 +199,12 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
179199 @Throws(IOException ::class )
180200 override fun onResponse (call : Call , response : Response ) {
181201 if (! response.isSuccessful) {
182- source.setException(FirebaseAuthInvalidUserException (
183- response.message(),
184- formatErrorMessage(" accounts:signUp" , request, response)
185- ))
202+ source.setException(
203+ FirebaseAuthInvalidUserException (
204+ response.message(),
205+ formatErrorMessage(" accounts:signUp" , request, response)
206+ )
207+ )
186208 } else {
187209 val body = response.body()!! .use { it.string() }
188210 user = FirebaseUserImpl (app, jsonParser.parseToJsonElement(body).jsonObject, true )
@@ -212,10 +234,12 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
212234 @Throws(IOException ::class )
213235 override fun onResponse (call : Call , response : Response ) {
214236 if (! response.isSuccessful) {
215- source.setException(FirebaseAuthInvalidUserException (
216- response.message(),
217- formatErrorMessage(" verifyCustomToken" , request, response)
218- ))
237+ source.setException(
238+ FirebaseAuthInvalidUserException (
239+ response.message(),
240+ formatErrorMessage(" verifyCustomToken" , request, response)
241+ )
242+ )
219243 } else {
220244 val body = response.body()!! .use { it.string() }
221245 val user = FirebaseUserImpl (app, jsonParser.parseToJsonElement(body).jsonObject)
@@ -245,10 +269,12 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
245269 @Throws(IOException ::class )
246270 override fun onResponse (call : Call , response : Response ) {
247271 if (! response.isSuccessful) {
248- source.setException(FirebaseAuthInvalidUserException (
249- response.message(),
250- formatErrorMessage(" verifyPassword" , request, response)
251- ))
272+ source.setException(
273+ FirebaseAuthInvalidUserException (
274+ response.message(),
275+ formatErrorMessage(" verifyPassword" , request, response)
276+ )
277+ )
252278 } else {
253279 val body = response.body()!! .use { it.string() }
254280 val user = FirebaseUserImpl (app, jsonParser.parseToJsonElement(body).jsonObject)
@@ -266,14 +292,14 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
266292 }
267293
268294 fun signOut () {
269- // todo cancel token refresher
295+ // todo cancel token refresher
270296 user = null
271297 }
272298
273299 override fun getAccessToken (forceRefresh : Boolean ): Task <GetTokenResult > {
274300 val user = user ? : return Tasks .forException(FirebaseNoSignedInUserException (" Please sign in before trying to get a token." ))
275301
276- if (! forceRefresh && user.createdAt + user.expiresIn* 1000 - 5 * 60 * 1000 > System .currentTimeMillis() ) {
302+ if (! forceRefresh && user.createdAt + user.expiresIn * 1000 - 5 * 60 * 1000 > System .currentTimeMillis()) {
277303// Log.i("FirebaseAuth", "returning existing token for user ${user.uid} from getAccessToken")
278304 return Tasks .forResult(GetTokenResult (user.idToken, user.claims))
279305 }
0 commit comments