@@ -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
@@ -52,17 +63,20 @@ class FirebaseUserImpl private constructor(
52
63
val idToken : String ,
53
64
val refreshToken : String ,
54
65
val expiresIn : Int ,
55
- val createdAt : Long
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 {
@@ -184,11 +198,13 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
184
198
}
185
199
}
186
200
201
+ private var urlFactory = UrlFactory (app)
202
+
187
203
fun signInAnonymously (): Task <AuthResult > {
188
204
val source = TaskCompletionSource <AuthResult >()
189
205
val body = RequestBody .create(json, JsonObject (mapOf (" returnSecureToken" to JsonPrimitive (true ))).toString())
190
206
val request = Request .Builder ()
191
- .url(" https://identitytoolkit.googleapis.com/ v1/accounts:signUp?key= " + app.options.apiKey )
207
+ .url(urlFactory.buildUrl( " v1/accounts:signUp" ) )
192
208
.post(body)
193
209
.build()
194
210
client.newCall(request).enqueue(object : Callback {
@@ -220,7 +236,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
220
236
JsonObject (mapOf (" token" to JsonPrimitive (customToken), " returnSecureToken" to JsonPrimitive (true ))).toString()
221
237
)
222
238
val request = Request .Builder ()
223
- .url(" https://www.googleapis.com/identitytoolkit/ v3/relyingparty/verifyCustomToken?key= " + app.options.apiKey )
239
+ .url(urlFactory.buildUrl( " v3/relyingparty/verifyCustomToken" ) )
224
240
.post(body)
225
241
.build()
226
242
client.newCall(request).enqueue(object : Callback {
@@ -252,7 +268,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
252
268
JsonObject (mapOf (" email" to JsonPrimitive (email), " password" to JsonPrimitive (password), " returnSecureToken" to JsonPrimitive (true ))).toString()
253
269
)
254
270
val request = Request .Builder ()
255
- .url(" https://www.googleapis.com/identitytoolkit/ v3/relyingparty/verifyPassword?key=" + app.options.apiKey)
271
+ .url(" v3/relyingparty/verifyPassword?key=" + app.options.apiKey)
256
272
.post(body)
257
273
.build()
258
274
client.newCall(request).enqueue(object : Callback {
@@ -439,5 +455,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
439
455
fun signInWithEmailLink (email : String , link : String ): Task <AuthResult > = TODO ()
440
456
441
457
fun setLanguageCode (value : String ): Nothing = TODO ()
442
- fun useEmulator (host : String , port : Int ): Unit = TODO ()
458
+
459
+ fun useEmulator (host : String , port : Int ) {
460
+ urlFactory = UrlFactory (app, " http://$host :$port /" )
461
+ }
443
462
}
0 commit comments