@@ -43,6 +43,15 @@ import java.util.concurrent.TimeUnit
43
43
44
44
val jsonParser = Json { ignoreUnknownKeys = true }
45
45
46
+ class UrlFactory (
47
+ private val app : FirebaseApp ,
48
+ private val emulatorUrl : String? = null
49
+ ) {
50
+ fun buildUrl (uri : String ): String {
51
+ return " ${emulatorUrl ? : " https://" }$uri ?key=${app.options.apiKey} "
52
+ }
53
+ }
54
+
46
55
@Serializable
47
56
class FirebaseUserImpl private constructor(
48
57
@Transient
@@ -52,17 +61,20 @@ class FirebaseUserImpl private constructor(
52
61
val idToken : String ,
53
62
val refreshToken : String ,
54
63
val expiresIn : Int ,
55
- val createdAt : Long
64
+ val createdAt : Long ,
65
+ @Transient
66
+ private val urlFactory : UrlFactory = UrlFactory (app)
56
67
) : FirebaseUser() {
57
68
58
- constructor (app: FirebaseApp , data: JsonObject , isAnonymous: Boolean = data[" isAnonymous" ]?.jsonPrimitive?.booleanOrNull ? : false ) : this (
69
+ constructor (app: FirebaseApp , data: JsonObject , isAnonymous: Boolean = data[" isAnonymous" ]?.jsonPrimitive?.booleanOrNull ? : false , urlFactory : UrlFactory = UrlFactory (app) ) : this (
59
70
app,
60
71
isAnonymous,
61
72
data[" uid" ]?.jsonPrimitive?.contentOrNull ? : data[" user_id" ]?.jsonPrimitive?.contentOrNull ? : data[" localId" ]?.jsonPrimitive?.contentOrNull ? : " " ,
62
73
data[" idToken" ]?.jsonPrimitive?.contentOrNull ? : data.getValue(" id_token" ).jsonPrimitive.content,
63
74
data[" refreshToken" ]?.jsonPrimitive?.contentOrNull ? : data.getValue(" refresh_token" ).jsonPrimitive.content,
64
75
data[" expiresIn" ]?.jsonPrimitive?.intOrNull ? : data.getValue(" expires_in" ).jsonPrimitive.int,
65
- data[" createdAt" ]?.jsonPrimitive?.longOrNull ? : System .currentTimeMillis()
76
+ data[" createdAt" ]?.jsonPrimitive?.longOrNull ? : System .currentTimeMillis(),
77
+ urlFactory
66
78
)
67
79
68
80
val claims: Map <String , Any ?> by lazy {
@@ -85,7 +97,7 @@ class FirebaseUserImpl private constructor(
85
97
val source = TaskCompletionSource <Void >()
86
98
val body = RequestBody .create(FirebaseAuth .getInstance(app).json, JsonObject (mapOf (" idToken" to JsonPrimitive (idToken))).toString())
87
99
val request = Request .Builder ()
88
- .url(" https:// www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key= " + app.options.apiKey )
100
+ .url(urlFactory.buildUrl( " www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount" ) )
89
101
.post(body)
90
102
.build()
91
103
FirebaseAuth .getInstance(app).client.newCall(request).enqueue(object : Callback {
@@ -184,11 +196,13 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
184
196
}
185
197
}
186
198
199
+ private var urlFactory = UrlFactory (app)
200
+
187
201
fun signInAnonymously (): Task <AuthResult > {
188
202
val source = TaskCompletionSource <AuthResult >()
189
203
val body = RequestBody .create(json, JsonObject (mapOf (" returnSecureToken" to JsonPrimitive (true ))).toString())
190
204
val request = Request .Builder ()
191
- .url(" https:// identitytoolkit.googleapis.com/v1/accounts:signUp?key= " + app.options.apiKey )
205
+ .url(urlFactory.buildUrl( " identitytoolkit.googleapis.com/v1/accounts:signUp" ) )
192
206
.post(body)
193
207
.build()
194
208
client.newCall(request).enqueue(object : Callback {
@@ -220,7 +234,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
220
234
JsonObject (mapOf (" token" to JsonPrimitive (customToken), " returnSecureToken" to JsonPrimitive (true ))).toString()
221
235
)
222
236
val request = Request .Builder ()
223
- .url(" https:// www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key= " + app.options.apiKey )
237
+ .url(urlFactory.buildUrl( " www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken" ) )
224
238
.post(body)
225
239
.build()
226
240
client.newCall(request).enqueue(object : Callback {
@@ -252,7 +266,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
252
266
JsonObject (mapOf (" email" to JsonPrimitive (email), " password" to JsonPrimitive (password), " returnSecureToken" to JsonPrimitive (true ))).toString()
253
267
)
254
268
val request = Request .Builder ()
255
- .url(" https:// www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key= " + app.options.apiKey )
269
+ .url(urlFactory.buildUrl( " www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword" ) )
256
270
.post(body)
257
271
.build()
258
272
client.newCall(request).enqueue(object : Callback {
@@ -336,7 +350,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
336
350
).toString()
337
351
)
338
352
val request = Request .Builder ()
339
- .url(" https:// securetoken.googleapis.com/v1/token?key= " + app.options.apiKey )
353
+ .url(urlFactory.buildUrl( " securetoken.googleapis.com/v1/token" ) )
340
354
.post(body)
341
355
.build()
342
356
@@ -439,5 +453,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
439
453
fun signInWithEmailLink (email : String , link : String ): Task <AuthResult > = TODO ()
440
454
441
455
fun setLanguageCode (value : String ): Nothing = TODO ()
442
- fun useEmulator (host : String , port : Int ): Unit = TODO ()
456
+
457
+ fun useEmulator (host : String , port : Int ) {
458
+ urlFactory = UrlFactory (app, " http://$host :$port /" )
459
+ }
443
460
}
0 commit comments