Skip to content

Commit

Permalink
add signInWithEmailAndPassword to Firebase Auth implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nbransby committed Jan 3, 2024
1 parent 230a15e commit 4db1e48
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
```

Expand Down Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.1.2
version=0.2.0
34 changes: 33 additions & 1 deletion src/main/java/com/google/firebase/auth/FirebaseAuth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,39 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
return source.task
}

fun signInWithEmailAndPassword(email: String, password: String): Task<AuthResult> {
val source = TaskCompletionSource<AuthResult>()
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()} -- " +
Expand Down Expand Up @@ -363,7 +396,6 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
idTokenListeners.remove(listener)
}

fun signInWithEmailAndPassword(email: String, password: String): Task<AuthResult> = TODO()
fun sendPasswordResetEmail(email: String, settings: ActionCodeSettings?): Task<Unit> = TODO()
fun createUserWithEmailAndPassword(email: String, password: String): Task<AuthResult> = TODO()
fun signInWithCredential(authCredential: AuthCredential): Task<AuthResult> = TODO()
Expand Down

0 comments on commit 4db1e48

Please sign in to comment.