Skip to content

Commit 8961f46

Browse files
committed
Auth: Add support for emulator
1 parent eb534f3 commit 8961f46

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

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

+26-7
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
@@ -53,16 +64,19 @@ class FirebaseUserImpl private constructor(
5364
val refreshToken: String,
5465
val expiresIn: Int,
5566
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 {
@@ -183,11 +197,13 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
183197
}
184198
}
185199

200+
private var urlFactory = UrlFactory(app)
201+
186202
fun signInAnonymously(): Task<AuthResult> {
187203
val source = TaskCompletionSource<AuthResult>()
188204
val body = RequestBody.create(json, JsonObject(mapOf("returnSecureToken" to JsonPrimitive(true))).toString())
189205
val request = Request.Builder()
190-
.url("https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=" + app.options.apiKey)
206+
.url(urlFactory.buildUrl("v1/accounts:signUp"))
191207
.post(body)
192208
.build()
193209
client.newCall(request).enqueue(object : Callback {
@@ -222,7 +238,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
222238
JsonObject(mapOf("token" to JsonPrimitive(customToken), "returnSecureToken" to JsonPrimitive(true))).toString()
223239
)
224240
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"))
226242
.post(body)
227243
.build()
228244
client.newCall(request).enqueue(object : Callback {
@@ -257,7 +273,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
257273
JsonObject(mapOf("email" to JsonPrimitive(email), "password" to JsonPrimitive(password), "returnSecureToken" to JsonPrimitive(true))).toString()
258274
)
259275
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)
261277
.post(body)
262278
.build()
263279
client.newCall(request).enqueue(object : Callback {
@@ -435,5 +451,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
435451
fun signInWithEmailLink(email: String, link: String): Task<AuthResult> = TODO()
436452

437453
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+
}
439458
}

0 commit comments

Comments
 (0)