Skip to content

Commit 265c924

Browse files
committed
Auth: Add support for emulator
1 parent 94f0d1d commit 265c924

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/main/java/com/google/firebase/auth/FirebaseAuth.kt

+27-8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ import java.util.concurrent.TimeUnit
4343

4444
val 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+
val baseUrl = "${emulatorUrl ?: "https://"}www.googleapis.com/identitytoolkit"
52+
53+
return "$baseUrl/$uri?key=${app.options.apiKey}"
54+
}
55+
}
56+
4657
@Serializable
4758
class FirebaseUserImpl private constructor(
4859
@Transient
@@ -52,17 +63,20 @@ class FirebaseUserImpl private constructor(
5263
val idToken: String,
5364
val refreshToken: String,
5465
val expiresIn: Int,
55-
val createdAt: Long
66+
val createdAt: Long,
67+
@Transient
68+
private val urlFactory: UrlFactory = UrlFactory(app),
5669
) : FirebaseUser() {
5770

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(
5972
app,
6073
isAnonymous,
6174
data["uid"]?.jsonPrimitive?.contentOrNull ?: data["user_id"]?.jsonPrimitive?.contentOrNull ?: data["localId"]?.jsonPrimitive?.contentOrNull ?: "",
6275
data["idToken"]?.jsonPrimitive?.contentOrNull ?: data.getValue("id_token").jsonPrimitive.content,
6376
data["refreshToken"]?.jsonPrimitive?.contentOrNull ?: data.getValue("refresh_token").jsonPrimitive.content,
6477
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
6680
)
6781

6882
val claims: Map<String, Any?> by lazy {
@@ -85,7 +99,7 @@ class FirebaseUserImpl private constructor(
8599
val source = TaskCompletionSource<Void>()
86100
val body = RequestBody.create(FirebaseAuth.getInstance(app).json, JsonObject(mapOf("idToken" to JsonPrimitive(idToken))).toString())
87101
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"))
89103
.post(body)
90104
.build()
91105
FirebaseAuth.getInstance(app).client.newCall(request).enqueue(object : Callback {
@@ -184,11 +198,13 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
184198
}
185199
}
186200

201+
private var urlFactory = UrlFactory(app)
202+
187203
fun signInAnonymously(): Task<AuthResult> {
188204
val source = TaskCompletionSource<AuthResult>()
189205
val body = RequestBody.create(json, JsonObject(mapOf("returnSecureToken" to JsonPrimitive(true))).toString())
190206
val request = Request.Builder()
191-
.url("https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=" + app.options.apiKey)
207+
.url(urlFactory.buildUrl("v1/accounts:signUp"))
192208
.post(body)
193209
.build()
194210
client.newCall(request).enqueue(object : Callback {
@@ -220,7 +236,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
220236
JsonObject(mapOf("token" to JsonPrimitive(customToken), "returnSecureToken" to JsonPrimitive(true))).toString()
221237
)
222238
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"))
224240
.post(body)
225241
.build()
226242
client.newCall(request).enqueue(object : Callback {
@@ -252,7 +268,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
252268
JsonObject(mapOf("email" to JsonPrimitive(email), "password" to JsonPrimitive(password), "returnSecureToken" to JsonPrimitive(true))).toString()
253269
)
254270
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)
256272
.post(body)
257273
.build()
258274
client.newCall(request).enqueue(object : Callback {
@@ -439,5 +455,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
439455
fun signInWithEmailLink(email: String, link: String): Task<AuthResult> = TODO()
440456

441457
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+
}
443462
}

0 commit comments

Comments
 (0)