Skip to content

Commit f63e1b3

Browse files
committed
signupNewUser implementation
1 parent 482e35b commit f63e1b3

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,38 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
245245
return source.task
246246
}
247247

248+
fun createUserWithEmailAndPassword(email: String, password: String): Task<AuthResult> {
249+
val source = TaskCompletionSource<AuthResult>()
250+
val body = RequestBody.create(
251+
json,
252+
JsonObject(mapOf("email" to JsonPrimitive(email), "password" to JsonPrimitive(password), "returnSecureToken" to JsonPrimitive(true))).toString()
253+
)
254+
val request = Request.Builder()
255+
.url("https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=" + app.options.apiKey)
256+
.post(body)
257+
.build()
258+
client.newCall(request).enqueue(object : Callback {
259+
260+
override fun onFailure(call: Call, e: IOException) {
261+
source.setException(FirebaseException(e.toString(), e))
262+
}
263+
264+
@Throws(IOException::class)
265+
override fun onResponse(call: Call, response: Response) {
266+
if (!response.isSuccessful) {
267+
source.setException(
268+
createAuthInvalidUserException("signupNewUser", request, response)
269+
)
270+
} else {
271+
val responseBody = response.body()?.use { it.string() } ?: ""
272+
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(responseBody).jsonObject)
273+
refreshToken(user, source) { AuthResult { it } }
274+
}
275+
}
276+
})
277+
return source.task
278+
}
279+
248280
fun signInWithEmailAndPassword(email: String, password: String): Task<AuthResult> {
249281
val source = TaskCompletionSource<AuthResult>()
250282
val body = RequestBody.create(
@@ -425,7 +457,6 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
425457
}
426458

427459
fun sendPasswordResetEmail(email: String, settings: ActionCodeSettings?): Task<Unit> = TODO()
428-
fun createUserWithEmailAndPassword(email: String, password: String): Task<AuthResult> = TODO()
429460
fun signInWithCredential(authCredential: AuthCredential): Task<AuthResult> = TODO()
430461
fun checkActionCode(code: String): Task<ActionCodeResult> = TODO()
431462
fun confirmPasswordReset(code: String, newPassword: String): Task<Unit> = TODO()

0 commit comments

Comments
 (0)