@@ -43,6 +43,17 @@ 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
+ val baseUrl = " ${emulatorUrl ? : " https://" } www.googleapis.com/identitytoolkit"
52
+
53
+ return " $baseUrl /$uri ?key=${app.options.apiKey} "
54
+ }
55
+ }
56
+
46
57
@Serializable
47
58
class FirebaseUserImpl private constructor(
48
59
@Transient
@@ -53,16 +64,19 @@ class FirebaseUserImpl private constructor(
53
64
val refreshToken : String ,
54
65
val expiresIn : Int ,
55
66
val createdAt : Long ,
67
+ @Transient
68
+ private val urlFactory : UrlFactory = UrlFactory (app),
56
69
) : FirebaseUser() {
57
70
58
- constructor (app: FirebaseApp , data: JsonObject , isAnonymous: Boolean = data[" isAnonymous" ]?.jsonPrimitive?.booleanOrNull ? : false ) : this (
71
+ constructor (app: FirebaseApp , data: JsonObject , isAnonymous: Boolean = data[" isAnonymous" ]?.jsonPrimitive?.booleanOrNull ? : false , urlFactory : UrlFactory = UrlFactory (app) ) : this (
59
72
app,
60
73
isAnonymous,
61
74
data[" uid" ]?.jsonPrimitive?.contentOrNull ? : data[" user_id" ]?.jsonPrimitive?.contentOrNull ? : data[" localId" ]?.jsonPrimitive?.contentOrNull ? : " " ,
62
75
data[" idToken" ]?.jsonPrimitive?.contentOrNull ? : data.getValue(" id_token" ).jsonPrimitive.content,
63
76
data[" refreshToken" ]?.jsonPrimitive?.contentOrNull ? : data.getValue(" refresh_token" ).jsonPrimitive.content,
64
77
data[" expiresIn" ]?.jsonPrimitive?.intOrNull ? : data.getValue(" expires_in" ).jsonPrimitive.int,
65
- data[" createdAt" ]?.jsonPrimitive?.longOrNull ? : System .currentTimeMillis()
78
+ data[" createdAt" ]?.jsonPrimitive?.longOrNull ? : System .currentTimeMillis(),
79
+ urlFactory
66
80
)
67
81
68
82
val claims: Map <String , Any ?> by lazy {
@@ -85,7 +99,7 @@ class FirebaseUserImpl private constructor(
85
99
val source = TaskCompletionSource <Void >()
86
100
val body = RequestBody .create(FirebaseAuth .getInstance(app).json, JsonObject (mapOf (" idToken" to JsonPrimitive (idToken))).toString())
87
101
val request = Request .Builder ()
88
- .url(" https://www.googleapis.com/identitytoolkit/ v3/relyingparty/deleteAccount?key= " + app.options.apiKey )
102
+ .url(urlFactory.buildUrl( " v3/relyingparty/deleteAccount" ) )
89
103
.post(body)
90
104
.build()
91
105
FirebaseAuth .getInstance(app).client.newCall(request).enqueue(object : Callback {
@@ -183,11 +197,13 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
183
197
}
184
198
}
185
199
200
+ private var urlFactory = UrlFactory (app)
201
+
186
202
fun signInAnonymously (): Task <AuthResult > {
187
203
val source = TaskCompletionSource <AuthResult >()
188
204
val body = RequestBody .create(json, JsonObject (mapOf (" returnSecureToken" to JsonPrimitive (true ))).toString())
189
205
val request = Request .Builder ()
190
- .url(" https://identitytoolkit.googleapis.com/ v1/accounts:signUp?key= " + app.options.apiKey )
206
+ .url(urlFactory.buildUrl( " v1/accounts:signUp" ) )
191
207
.post(body)
192
208
.build()
193
209
client.newCall(request).enqueue(object : Callback {
@@ -222,7 +238,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
222
238
JsonObject (mapOf (" token" to JsonPrimitive (customToken), " returnSecureToken" to JsonPrimitive (true ))).toString()
223
239
)
224
240
val request = Request .Builder ()
225
- .url(" https://www.googleapis.com/identitytoolkit/ v3/relyingparty/verifyCustomToken?key= " + app.options.apiKey )
241
+ .url(urlFactory.buildUrl( " v3/relyingparty/verifyCustomToken" ) )
226
242
.post(body)
227
243
.build()
228
244
client.newCall(request).enqueue(object : Callback {
@@ -257,7 +273,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
257
273
JsonObject (mapOf (" email" to JsonPrimitive (email), " password" to JsonPrimitive (password), " returnSecureToken" to JsonPrimitive (true ))).toString()
258
274
)
259
275
val request = Request .Builder ()
260
- .url(" https://www.googleapis.com/identitytoolkit/ v3/relyingparty/verifyPassword?key=" + app.options.apiKey)
276
+ .url(" v3/relyingparty/verifyPassword?key=" + app.options.apiKey)
261
277
.post(body)
262
278
.build()
263
279
client.newCall(request).enqueue(object : Callback {
@@ -435,5 +451,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
435
451
fun signInWithEmailLink (email : String , link : String ): Task <AuthResult > = TODO ()
436
452
437
453
fun setLanguageCode (value : String ): Nothing = TODO ()
438
- fun useEmulator (host : String , port : Int ): Unit = TODO ()
454
+
455
+ fun useEmulator (host : String , port : Int ) {
456
+ urlFactory = UrlFactory (app, " http://$host :$port /" )
457
+ }
439
458
}
0 commit comments