@@ -43,6 +43,15 @@ import java.util.concurrent.TimeUnit
43
43
44
44
internal 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 internal constructor(
48
57
@Transient
@@ -53,14 +62,17 @@ class FirebaseUserImpl internal constructor(
53
62
val refreshToken : String ,
54
63
val expiresIn : Int ,
55
64
val createdAt : Long ,
56
- override val email : String?
65
+ override val email : String? ,
66
+ @Transient
67
+ private val urlFactory : UrlFactory = UrlFactory (app)
57
68
) : FirebaseUser() {
58
69
59
70
constructor (
60
71
app: FirebaseApp ,
61
72
data: JsonObject ,
62
73
isAnonymous: Boolean = data[" isAnonymous" ]?.jsonPrimitive?.booleanOrNull ? : false ,
63
- email: String? = data.getOrElse(" email" ) { null }?.jsonPrimitive?.contentOrNull
74
+ email: String? = data.getOrElse(" email" ) { null }?.jsonPrimitive?.contentOrNull,
75
+ urlFactory: UrlFactory = UrlFactory (app)
64
76
) : this (
65
77
app = app,
66
78
isAnonymous = isAnonymous,
@@ -69,7 +81,8 @@ class FirebaseUserImpl internal constructor(
69
81
refreshToken = data[" refreshToken" ]?.jsonPrimitive?.contentOrNull ? : data.getValue(" refresh_token" ).jsonPrimitive.content,
70
82
expiresIn = data[" expiresIn" ]?.jsonPrimitive?.intOrNull ? : data.getValue(" expires_in" ).jsonPrimitive.int,
71
83
createdAt = data[" createdAt" ]?.jsonPrimitive?.longOrNull ? : System .currentTimeMillis(),
72
- email = email
84
+ email = email,
85
+ urlFactory = urlFactory
73
86
)
74
87
75
88
val claims: Map <String , Any ?> by lazy {
@@ -92,7 +105,7 @@ class FirebaseUserImpl internal constructor(
92
105
val source = TaskCompletionSource <Void >()
93
106
val body = RequestBody .create(FirebaseAuth .getInstance(app).json, JsonObject (mapOf (" idToken" to JsonPrimitive (idToken))).toString())
94
107
val request = Request .Builder ()
95
- .url(" https:// www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key= " + app.options.apiKey )
108
+ .url(urlFactory.buildUrl( " www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount" ) )
96
109
.post(body)
97
110
.build()
98
111
FirebaseAuth .getInstance(app).client.newCall(request).enqueue(object : Callback {
@@ -194,7 +207,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
194
207
): TaskCompletionSource <AuthResult > {
195
208
val source = TaskCompletionSource <AuthResult >()
196
209
val request = Request .Builder ()
197
- .url(" $url ?key= " + app.options.apiKey )
210
+ .url(urlFactory.buildUrl(url) )
198
211
.post(body)
199
212
.build()
200
213
@@ -279,9 +292,11 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
279
292
}
280
293
}
281
294
295
+ private var urlFactory = UrlFactory (app)
296
+
282
297
fun signInAnonymously (): Task <AuthResult > {
283
298
val source = enqueueAuthPost(
284
- url = " https:// identitytoolkit.googleapis.com/v1/accounts:signUp" ,
299
+ url = " identitytoolkit.googleapis.com/v1/accounts:signUp" ,
285
300
body = RequestBody .create(json, JsonObject (mapOf (" returnSecureToken" to JsonPrimitive (true ))).toString()),
286
301
setResult = { responseBody ->
287
302
FirebaseUserImpl (app, jsonParser.parseToJsonElement(responseBody).jsonObject, isAnonymous = true )
@@ -292,7 +307,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
292
307
293
308
fun signInWithCustomToken (customToken : String ): Task <AuthResult > {
294
309
val source = enqueueAuthPost(
295
- url = " https:// www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken" ,
310
+ url = " www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken" ,
296
311
body = RequestBody .create(
297
312
json,
298
313
JsonObject (mapOf (" token" to JsonPrimitive (customToken), " returnSecureToken" to JsonPrimitive (true ))).toString()
@@ -306,7 +321,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
306
321
307
322
fun createUserWithEmailAndPassword (email : String , password : String ): Task <AuthResult > {
308
323
val source = enqueueAuthPost(
309
- url = " https:// www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser" ,
324
+ url = " www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser" ,
310
325
body = RequestBody .create(
311
326
json,
312
327
JsonObject (
@@ -324,9 +339,10 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
324
339
return source.task
325
340
}
326
341
342
+
327
343
fun signInWithEmailAndPassword (email : String , password : String ): Task <AuthResult > {
328
344
val source = enqueueAuthPost(
329
- url = " https:// www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword" ,
345
+ url = " www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword" ,
330
346
body = RequestBody .create(
331
347
json,
332
348
JsonObject (
@@ -406,7 +422,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
406
422
).toString()
407
423
)
408
424
val request = Request .Builder ()
409
- .url(" https:// securetoken.googleapis.com/v1/token?key= " + app.options.apiKey )
425
+ .url(urlFactory.buildUrl( " securetoken.googleapis.com/v1/token" ) )
410
426
.post(body)
411
427
.tag(REFRESH_TOKEN_TAG )
412
428
.build()
@@ -555,7 +571,12 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
555
571
idTokenListeners.remove(listener)
556
572
}
557
573
574
+ fun useEmulator (host : String , port : Int ) {
575
+ urlFactory = UrlFactory (app, " http://$host :$port /" )
576
+ }
577
+
558
578
fun sendPasswordResetEmail (email : String , settings : ActionCodeSettings ? ): Task <Unit > = TODO ()
579
+ fun signInWithCredential (authCredential : AuthCredential ): Task <AuthResult > = TODO ()
559
580
fun checkActionCode (code : String ): Task <ActionCodeResult > = TODO ()
560
581
fun confirmPasswordReset (code : String , newPassword : String ): Task <Unit > = TODO ()
561
582
fun fetchSignInMethodsForEmail (email : String ): Task <SignInMethodQueryResult > = TODO ()
@@ -568,5 +589,4 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
568
589
fun signInWithEmailLink (email : String , link : String ): Task <AuthResult > = TODO ()
569
590
570
591
fun setLanguageCode (value : String ): Nothing = TODO ()
571
- fun useEmulator (host : String , port : Int ): Unit = TODO ()
572
592
}
0 commit comments