From 4db1e48c05acaee3b2e6f82be84c74157701cf78 Mon Sep 17 00:00:00 2001 From: nbransby Date: Thu, 4 Jan 2024 12:23:04 +1300 Subject: [PATCH] add signInWithEmailAndPassword to Firebase Auth implementation --- README.md | 3 +- gradle.properties | 2 +- .../com/google/firebase/auth/FirebaseAuth.kt | 34 ++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c6fe969..be10611 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ You can add the library via Gradle: ```kotlin dependencies { - implementation("dev.gitlive:firebase-java-sdk:0.1.2") + implementation("dev.gitlive:firebase-java-sdk:0.2.0") } ``` @@ -110,6 +110,7 @@ Currently, the following limitations are observed: * Firebase Auth implementation is minimal and only supports a small subset of the API: - `signInAnonymously` - `signInWithCustomToken` + - `signInWithEmailAndPassword` - `currentUser` - `getAccessToken` - `getUid` diff --git a/gradle.properties b/gradle.properties index dab6df0..04d070a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=0.1.2 \ No newline at end of file +version=0.2.0 \ No newline at end of file diff --git a/src/main/java/com/google/firebase/auth/FirebaseAuth.kt b/src/main/java/com/google/firebase/auth/FirebaseAuth.kt index 0d8c694..f6df082 100644 --- a/src/main/java/com/google/firebase/auth/FirebaseAuth.kt +++ b/src/main/java/com/google/firebase/auth/FirebaseAuth.kt @@ -228,6 +228,39 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider { return source.task } + fun signInWithEmailAndPassword(email: String, password: String): Task { + val source = TaskCompletionSource() + val body = RequestBody.create( + json, + JsonObject(mapOf("email" to JsonPrimitive(email), "password" to JsonPrimitive(password), "returnSecureToken" to JsonPrimitive(true))).toString() + ) + val request = Request.Builder() + .url("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" + app.options.apiKey) + .post(body) + .build() + client.newCall(request).enqueue(object : Callback { + + override fun onFailure(request: Request, e: IOException) { + source.setException(FirebaseException(e.toString(), e)) + } + + @Throws(IOException::class) + override fun onResponse(response: Response) { + if (!response.isSuccessful) { + source.setException(FirebaseAuthInvalidUserException( + response.message(), + formatErrorMessage("verifyPassword", request, response) + )) + } else { + val body = response.body().use { it.string() } + val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject) + refreshToken(user, source) { AuthResult { it } } + } + } + }) + return source.task + } + internal fun formatErrorMessage(title: String, request: Request, response: Response): String { return "$title API returned an error, " + "with url [${request.method()}] ${request.urlString()} ${request.body()} -- " + @@ -363,7 +396,6 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider { idTokenListeners.remove(listener) } - fun signInWithEmailAndPassword(email: String, password: String): Task = TODO() fun sendPasswordResetEmail(email: String, settings: ActionCodeSettings?): Task = TODO() fun createUserWithEmailAndPassword(email: String, password: String): Task = TODO() fun signInWithCredential(authCredential: AuthCredential): Task = TODO()