@@ -43,6 +43,15 @@ import java.util.concurrent.TimeUnit
4343
4444val jsonParser = Json { ignoreUnknownKeys = true }
4545
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+
4655@Serializable
4756class FirebaseUserImpl private constructor(
4857 @Transient
@@ -52,17 +61,20 @@ class FirebaseUserImpl private constructor(
5261 val idToken : String ,
5362 val refreshToken : String ,
5463 val expiresIn : Int ,
55- val createdAt : Long
64+ val createdAt : Long ,
65+ @Transient
66+ private val urlFactory : UrlFactory = UrlFactory (app)
5667) : FirebaseUser() {
5768
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 (
5970 app,
6071 isAnonymous,
6172 data[" uid" ]?.jsonPrimitive?.contentOrNull ? : data[" user_id" ]?.jsonPrimitive?.contentOrNull ? : data[" localId" ]?.jsonPrimitive?.contentOrNull ? : " " ,
6273 data[" idToken" ]?.jsonPrimitive?.contentOrNull ? : data.getValue(" id_token" ).jsonPrimitive.content,
6374 data[" refreshToken" ]?.jsonPrimitive?.contentOrNull ? : data.getValue(" refresh_token" ).jsonPrimitive.content,
6475 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
6678 )
6779
6880 val claims: Map <String , Any ?> by lazy {
@@ -85,7 +97,7 @@ class FirebaseUserImpl private constructor(
8597 val source = TaskCompletionSource <Void >()
8698 val body = RequestBody .create(FirebaseAuth .getInstance(app).json, JsonObject (mapOf (" idToken" to JsonPrimitive (idToken))).toString())
8799 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" ) )
89101 .post(body)
90102 .build()
91103 FirebaseAuth .getInstance(app).client.newCall(request).enqueue(object : Callback {
@@ -184,11 +196,13 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
184196 }
185197 }
186198
199+ private var urlFactory = UrlFactory (app)
200+
187201 fun signInAnonymously (): Task <AuthResult > {
188202 val source = TaskCompletionSource <AuthResult >()
189203 val body = RequestBody .create(json, JsonObject (mapOf (" returnSecureToken" to JsonPrimitive (true ))).toString())
190204 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" ) )
192206 .post(body)
193207 .build()
194208 client.newCall(request).enqueue(object : Callback {
@@ -220,7 +234,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
220234 JsonObject (mapOf (" token" to JsonPrimitive (customToken), " returnSecureToken" to JsonPrimitive (true ))).toString()
221235 )
222236 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" ) )
224238 .post(body)
225239 .build()
226240 client.newCall(request).enqueue(object : Callback {
@@ -252,7 +266,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
252266 JsonObject (mapOf (" email" to JsonPrimitive (email), " password" to JsonPrimitive (password), " returnSecureToken" to JsonPrimitive (true ))).toString()
253267 )
254268 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" ) )
256270 .post(body)
257271 .build()
258272 client.newCall(request).enqueue(object : Callback {
@@ -336,7 +350,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
336350 ).toString()
337351 )
338352 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" ) )
340354 .post(body)
341355 .build()
342356
@@ -439,5 +453,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
439453 fun signInWithEmailLink (email : String , link : String ): Task <AuthResult > = TODO ()
440454
441455 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+ }
443460}
0 commit comments