Skip to content

Commit c04945e

Browse files
authored
Merge pull request #20 from vanniktech/createUserWithEmailAndPassword
Firebase Auth: Add createUserWithEmailAndPassword.
2 parents 322bfd4 + e190136 commit c04945e

File tree

6 files changed

+40
-17
lines changed
  • firebase-auth/src
    • androidMain/kotlin/dev/gitlive/firebase/auth
    • commonMain/kotlin/dev/gitlive/firebase/auth
    • commonTest/kotlin/dev/gitlive/firebase/auth
    • iosMain/kotlin/dev/gitlive/firebase/auth
    • jsMain/kotlin/dev/gitlive/firebase/auth
  • firebase-common/src/jsMain/kotlin/dev/gitlive/firebase

6 files changed

+40
-17
lines changed

firebase-auth/src/androidMain/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ actual class FirebaseAuth internal constructor(val android: com.google.firebase.
2424
actual suspend fun signInWithEmailAndPassword(email: String, password: String) =
2525
AuthResult(android.signInWithEmailAndPassword(email, password).await())
2626

27+
actual suspend fun createUserWithEmailAndPassword(email: String, password: String) =
28+
AuthResult(android.createUserWithEmailAndPassword(email, password).await())
29+
2730
actual suspend fun signInWithCustomToken(token: String) =
2831
AuthResult(android.signInWithCustomToken(token).await())
2932

firebase-auth/src/commonMain/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ expect class FirebaseAuth {
1818
val currentUser: FirebaseUser?
1919
val authStateChanged: Flow<FirebaseUser?>
2020
suspend fun signInWithEmailAndPassword(email: String, password: String): AuthResult
21+
suspend fun createUserWithEmailAndPassword(email: String, password: String): AuthResult
2122
suspend fun signInWithCustomToken(token: String): AuthResult
2223
suspend fun signInAnonymously(): AuthResult
2324
suspend fun signOut()

firebase-auth/src/commonTest/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,48 @@
44

55
package dev.gitlive.firebase.auth
66

7-
import dev.gitlive.firebase.Firebase
8-
import dev.gitlive.firebase.FirebaseOptions
9-
import dev.gitlive.firebase.initialize
10-
import kotlinx.coroutines.GlobalScope
7+
import dev.gitlive.firebase.*
8+
import kotlin.random.Random
119
import kotlin.test.BeforeTest
1210
import kotlin.test.Test
1311
import kotlin.test.assertEquals
12+
import kotlin.test.assertNotEquals
1413

1514
expect val context: Any
1615
expect fun runTest(test: suspend () -> Unit)
1716

1817
class FirebaseAuthTest {
19-
20-
@BeforeTest
21-
fun initializeFirebase() {
22-
Firebase.initialize(
23-
context,
24-
FirebaseOptions(
25-
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
26-
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
27-
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
28-
storageBucket = "fir-kotlin-sdk.appspot.com",
29-
projectId = "fir-kotlin-sdk"
18+
companion object {
19+
init {
20+
// Firebase only wants to be initialized once.
21+
Firebase.initialize(
22+
context,
23+
FirebaseOptions(
24+
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
25+
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
26+
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
27+
storageBucket = "fir-kotlin-sdk.appspot.com",
28+
projectId = "fir-kotlin-sdk"
29+
)
3030
)
31-
)
31+
}
3232
}
3333

3434
@Test
3535
fun testSignInWithUsernameAndPassword() = runTest {
3636
val result = Firebase.auth.signInWithEmailAndPassword("[email protected]", "test123")
3737
assertEquals("mn8kgIFnxLO7il8GpTa5g0ObP6I2", result.user!!.uid)
3838
}
39-
}
39+
40+
@Test
41+
fun testCreateUserWithEmailAndPassword() = runTest {
42+
val email = "test+${Random.nextInt(100000)}@test.com"
43+
val createResult = Firebase.auth.createUserWithEmailAndPassword(email, "test123")
44+
assertNotEquals(null, createResult.user?.uid)
45+
46+
val signInResult = Firebase.auth.signInWithEmailAndPassword(email, "test123")
47+
assertEquals(createResult.user?.uid, signInResult.user?.uid)
48+
49+
signInResult.user!!.delete()
50+
}
51+
}

firebase-auth/src/iosMain/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ actual class FirebaseAuth internal constructor(val ios: FIRAuth) {
2929
actual suspend fun signInWithEmailAndPassword(email: String, password: String) =
3030
AuthResult(ios.awaitResult { signInWithEmail(email = email, password = password, completion = it) })
3131

32+
actual suspend fun createUserWithEmailAndPassword(email: String, password: String) =
33+
AuthResult(ios.awaitResult { createUserWithEmail(email = email, password = password, completion = it) })
34+
3235
actual suspend fun signInWithCustomToken(token: String) =
3336
AuthResult(ios.awaitResult { signInWithCustomToken(token, it) })
3437

firebase-auth/src/jsMain/kotlin/dev/gitlive/firebase/auth/auth.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ actual class FirebaseAuth internal constructor(val js: firebase.auth.Auth) {
2323
actual suspend fun signInWithEmailAndPassword(email: String, password: String) =
2424
rethrow { AuthResult(js.signInWithEmailAndPassword(email, password).await()) }
2525

26+
actual suspend fun createUserWithEmailAndPassword(email: String, password: String) =
27+
rethrow { AuthResult(js.createUserWithEmailAndPassword(email, password).await()) }
28+
2629
actual suspend fun signInWithCustomToken(token: String)
2730
= rethrow { AuthResult(js.signInWithCustomToken(token).await()) }
2831

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/externals.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ external object firebase {
5858
val currentUser: user.User?
5959

6060
fun signInWithEmailAndPassword(email: String, password: String): Promise<AuthResult>
61+
fun createUserWithEmailAndPassword(email: String, password: String): Promise<AuthResult>
6162
fun signInWithCustomToken(token: String): Promise<AuthResult>
6263
fun signInAnonymously(): Promise<AuthResult>
6364
fun signOut(): Promise<Unit>

0 commit comments

Comments
 (0)