Skip to content

Commit 4db1e48

Browse files
committed
add signInWithEmailAndPassword to Firebase Auth implementation
1 parent 230a15e commit 4db1e48

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ You can add the library via Gradle:
2525

2626
```kotlin
2727
dependencies {
28-
implementation("dev.gitlive:firebase-java-sdk:0.1.2")
28+
implementation("dev.gitlive:firebase-java-sdk:0.2.0")
2929
}
3030
```
3131

@@ -110,6 +110,7 @@ Currently, the following limitations are observed:
110110
* Firebase Auth implementation is minimal and only supports a small subset of the API:
111111
- `signInAnonymously`
112112
- `signInWithCustomToken`
113+
- `signInWithEmailAndPassword`
113114
- `currentUser`
114115
- `getAccessToken`
115116
- `getUid`

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=0.1.2
1+
version=0.2.0

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,39 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
228228
return source.task
229229
}
230230

231+
fun signInWithEmailAndPassword(email: String, password: String): Task<AuthResult> {
232+
val source = TaskCompletionSource<AuthResult>()
233+
val body = RequestBody.create(
234+
json,
235+
JsonObject(mapOf("email" to JsonPrimitive(email), "password" to JsonPrimitive(password), "returnSecureToken" to JsonPrimitive(true))).toString()
236+
)
237+
val request = Request.Builder()
238+
.url("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" + app.options.apiKey)
239+
.post(body)
240+
.build()
241+
client.newCall(request).enqueue(object : Callback {
242+
243+
override fun onFailure(request: Request, e: IOException) {
244+
source.setException(FirebaseException(e.toString(), e))
245+
}
246+
247+
@Throws(IOException::class)
248+
override fun onResponse(response: Response) {
249+
if (!response.isSuccessful) {
250+
source.setException(FirebaseAuthInvalidUserException(
251+
response.message(),
252+
formatErrorMessage("verifyPassword", request, response)
253+
))
254+
} else {
255+
val body = response.body().use { it.string() }
256+
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject)
257+
refreshToken(user, source) { AuthResult { it } }
258+
}
259+
}
260+
})
261+
return source.task
262+
}
263+
231264
internal fun formatErrorMessage(title: String, request: Request, response: Response): String {
232265
return "$title API returned an error, " +
233266
"with url [${request.method()}] ${request.urlString()} ${request.body()} -- " +
@@ -363,7 +396,6 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
363396
idTokenListeners.remove(listener)
364397
}
365398

366-
fun signInWithEmailAndPassword(email: String, password: String): Task<AuthResult> = TODO()
367399
fun sendPasswordResetEmail(email: String, settings: ActionCodeSettings?): Task<Unit> = TODO()
368400
fun createUserWithEmailAndPassword(email: String, password: String): Task<AuthResult> = TODO()
369401
fun signInWithCredential(authCredential: AuthCredential): Task<AuthResult> = TODO()

0 commit comments

Comments
 (0)