Skip to content

Commit bf3f897

Browse files
authored
Merge pull request #33 from JacobCube/signup_new_user
signupNewUser implementation
2 parents 2f5387f + f0217a5 commit bf3f897

13 files changed

+864
-309
lines changed

src/main/java/android/net/ConnectivityManager.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package android.net
22

3+
import android.app.Application
4+
import com.google.firebase.FirebaseApp
35
import com.google.firebase.database.DataSnapshot
46
import com.google.firebase.database.DatabaseError
57
import com.google.firebase.database.FirebaseDatabase
@@ -11,7 +13,9 @@ class ConnectivityManager private constructor() {
1113
val instance = ConnectivityManager()
1214
}
1315

14-
private val connected = FirebaseDatabase.getInstance().getReference(".info/connected")
16+
private val connected by lazy {
17+
FirebaseDatabase.getInstance(FirebaseApp.getApps(Application()).first()).getReference(".info/connected")
18+
}
1519

1620
fun registerDefaultNetworkCallback(networkCallback: NetworkCallback) {
1721
connected.addValueEventListener(networkCallback)

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

Lines changed: 579 additions & 185 deletions
Large diffs are not rendered by default.

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,46 @@ import com.google.android.gms.tasks.Task
44

55
abstract class FirebaseUser {
66
abstract val uid: String
7+
abstract val email: String?
8+
abstract val photoUrl: String?
9+
abstract val displayName: String?
710
abstract val isAnonymous: Boolean
11+
812
abstract fun delete(): Task<Void>
13+
914
abstract fun reload(): Task<Void>
1015

11-
val email: String get() = TODO()
12-
val displayName: String get() = TODO()
16+
abstract fun verifyBeforeUpdateEmail(
17+
newEmail: String,
18+
actionCodeSettings: ActionCodeSettings?
19+
): Task<Unit>
20+
21+
abstract fun updateEmail(email: String): Task<Unit>
22+
23+
abstract fun getIdToken(forceRefresh: Boolean): Task<GetTokenResult>
24+
25+
abstract fun updateProfile(request: UserProfileChangeRequest): Task<Unit>
26+
1327
val phoneNumber: String get() = TODO()
14-
val photoUrl: String? get() = TODO()
1528
val isEmailVerified: Boolean get() = TODO()
1629
val metadata: FirebaseUserMetadata get() = TODO()
1730
val multiFactor: MultiFactor get() = TODO()
1831
val providerData: List<UserInfo> get() = TODO()
1932
val providerId: String get() = TODO()
20-
abstract fun getIdToken(forceRefresh: Boolean): Task<GetTokenResult>
33+
2134
fun linkWithCredential(credential: AuthCredential): Task<AuthResult> = TODO()
35+
2236
fun sendEmailVerification(): Task<Unit> = TODO()
37+
2338
fun sendEmailVerification(actionCodeSettings: ActionCodeSettings): Task<Unit> = TODO()
39+
2440
fun unlink(provider: String): Task<AuthResult> = TODO()
25-
fun updateEmail(email: String): Task<Unit> = TODO()
41+
2642
fun updatePassword(password: String): Task<Unit> = TODO()
43+
2744
fun updatePhoneNumber(credential: AuthCredential): Task<Unit> = TODO()
28-
fun updateProfile(request: UserProfileChangeRequest): Task<Unit> = TODO()
29-
fun verifyBeforeUpdateEmail(newEmail: String, actionCodeSettings: ActionCodeSettings?): Task<Unit> = TODO()
45+
3046
fun reauthenticate(credential: AuthCredential): Task<Unit> = TODO()
47+
3148
fun reauthenticateAndRetrieveData(credential: AuthCredential): Task<AuthResult> = TODO()
3249
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.google.firebase.auth
2+
3+
internal enum class OobRequestType {
4+
PASSWORD_RESET,
5+
EMAIL_SIGNIN,
6+
VERIFY_EMAIL,
7+
VERIFY_AND_CHANGE_EMAIL
8+
}

src/main/java/com/google/firebase/auth/UserProfileChangeRequest.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.google.firebase.auth
2+
3+
import android.net.Uri
4+
import android.os.Parcel
5+
import android.os.Parcelable
6+
7+
class UserProfileChangeRequest private constructor(
8+
internal val displayName: String?,
9+
internal val photoUrl: String?
10+
) : Parcelable {
11+
override fun describeContents(): Int = displayName.hashCode() + photoUrl.hashCode()
12+
13+
override fun writeToParcel(
14+
dest: Parcel,
15+
flags: Int
16+
) {
17+
dest.writeString(displayName)
18+
dest.writeString(photoUrl)
19+
}
20+
21+
internal companion object CREATOR : Parcelable.Creator<UserProfileChangeRequest> {
22+
override fun createFromParcel(parcel: Parcel): UserProfileChangeRequest {
23+
val displayName = parcel.readString()
24+
val photoUri = parcel.readString()
25+
return UserProfileChangeRequest(displayName, photoUri)
26+
}
27+
28+
override fun newArray(size: Int): Array<UserProfileChangeRequest?> = arrayOfNulls(size)
29+
}
30+
31+
class Builder {
32+
private var displayName: String? = null
33+
private var photoUri: Uri? = null
34+
35+
fun setDisplayName(name: String?): Builder {
36+
this.displayName = name
37+
return this
38+
}
39+
40+
fun setPhotoUri(uri: Uri?): Builder {
41+
this.photoUri = uri
42+
return this
43+
}
44+
45+
fun build(): UserProfileChangeRequest = UserProfileChangeRequest(displayName, photoUri?.toString())
46+
}
47+
}

src/test/kotlin/AppTest.kt

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/test/kotlin/AuthTest.kt

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/test/kotlin/FirebaseAppTest.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import android.app.Application
2+
import com.google.firebase.Firebase
3+
import com.google.firebase.FirebaseOptions
4+
import com.google.firebase.FirebasePlatform
5+
import com.google.firebase.initialize
6+
import org.junit.Test
7+
8+
class FirebaseAppTest : FirebaseTest() {
9+
@Test
10+
fun `initialize firebase`() {
11+
FirebasePlatform.initializeFirebasePlatform(
12+
object : FirebasePlatform() {
13+
val storage = mutableMapOf<String, String>()
14+
15+
override fun store(
16+
key: String,
17+
value: String
18+
) = storage.set(key, value)
19+
20+
override fun retrieve(key: String) = storage[key]
21+
22+
override fun clear(key: String) {
23+
storage.remove(key)
24+
}
25+
26+
override fun log(msg: String) = println(msg)
27+
}
28+
)
29+
val options =
30+
FirebaseOptions
31+
.Builder()
32+
.setProjectId("fir-java-sdk")
33+
.setApplicationId("1:341458593155:web:bf8e1aa37efe01f32d42b6")
34+
.setApiKey("AIzaSyCvVHjTJHyeStnzIE7J9LLtHqWk6reGM08")
35+
.setDatabaseUrl("https://fir-java-sdk-default-rtdb.firebaseio.com")
36+
.setStorageBucket("fir-java-sdk.appspot.com")
37+
.setGcmSenderId("341458593155")
38+
.build()
39+
Firebase.initialize(Application(), options)
40+
}
41+
}

src/test/kotlin/FirebaseAuthTest.kt

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import android.net.Uri
2+
import com.google.firebase.auth.FirebaseAuth
3+
import com.google.firebase.auth.FirebaseAuthInvalidUserException
4+
import kotlinx.coroutines.runBlocking
5+
import kotlinx.coroutines.tasks.await
6+
import kotlinx.coroutines.test.runTest
7+
import org.junit.Assert.assertEquals
8+
import org.junit.Assert.assertNotEquals
9+
import org.junit.Assert.assertThrows
10+
import org.junit.Before
11+
import org.junit.Test
12+
import java.util.UUID
13+
14+
class FirebaseAuthTest : FirebaseTest() {
15+
private val email = "email${UUID.randomUUID()}@example.com"
16+
private val auth by lazy { FirebaseAuth.getInstance(app) }
17+
18+
@Before
19+
fun initialize() {
20+
auth.apply {
21+
useEmulator("localhost", 9099)
22+
}
23+
}
24+
25+
@Test
26+
fun `should authenticate via anonymous auth`() =
27+
runTest {
28+
auth.signInAnonymously().await()
29+
assertEquals(null, auth.currentUser!!.email)
30+
assertEquals(true, auth.currentUser!!.isAnonymous)
31+
}
32+
33+
@Test
34+
fun `should create user via email and password`() =
35+
runTest {
36+
val createResult = auth.createUserWithEmailAndPassword(email, "test123").await()
37+
assertNotEquals(null, createResult.user?.uid)
38+
assertEquals(null, createResult.user?.displayName)
39+
// assertEquals(null, createResult.user?.phoneNumber)
40+
assertEquals(false, createResult.user?.isAnonymous)
41+
assertEquals(email, createResult.user?.email)
42+
assertNotEquals("", createResult.user!!.email)
43+
44+
val signInResult = auth.signInWithEmailAndPassword(email, "test123").await()
45+
assertEquals(createResult.user?.uid, signInResult.user?.uid)
46+
}
47+
48+
@Test
49+
fun `should authenticate via email and password`() =
50+
runTest {
51+
auth.createUserWithEmailAndPassword(email, "test123").await()
52+
53+
auth.signInWithEmailAndPassword(email, "test123").await()
54+
55+
assertEquals(false, auth.currentUser?.isAnonymous)
56+
}
57+
58+
/*@Test
59+
fun `should authenticate via custom token`() =
60+
runTest {
61+
val user = auth.createUserWithEmailAndPassword(email, "test123").await()
62+
auth
63+
.signInWithCustomToken(
64+
user.user
65+
.getIdToken(false)
66+
.await()
67+
.token ?: "",
68+
).await()
69+
70+
assertEquals(false, auth.currentUser?.isAnonymous)
71+
}*/
72+
73+
@Test
74+
fun `should update displayName and photoUrl`() =
75+
runTest {
76+
auth
77+
.createUserWithEmailAndPassword(email, "test123")
78+
.await()
79+
.user
80+
auth.currentUser
81+
?.updateProfile(
82+
com.google.firebase.auth.UserProfileChangeRequest
83+
.Builder()
84+
.setDisplayName("testDisplayName")
85+
.setPhotoUri(Uri.parse("https://picsum.photos/100"))
86+
.build()
87+
)?.await()
88+
assertEquals("testDisplayName", auth.currentUser?.displayName)
89+
assertEquals("https://picsum.photos/100", auth.currentUser?.photoUrl)
90+
}
91+
92+
@Test
93+
fun `should sign in anonymously`() =
94+
runTest {
95+
val signInResult = auth.signInAnonymously().await()
96+
assertNotEquals("", signInResult.user!!.email)
97+
assertEquals(true, signInResult.user?.isAnonymous)
98+
}
99+
100+
@Test
101+
fun `should throw exception on invalid password`() =
102+
runTest {
103+
auth.createUserWithEmailAndPassword(email, "test123").await()
104+
105+
val exception =
106+
assertThrows(FirebaseAuthInvalidUserException::class.java) {
107+
runBlocking {
108+
auth.signInWithEmailAndPassword(email, "wrongpassword").await()
109+
}
110+
}
111+
112+
assertEquals("INVALID_PASSWORD", exception.errorCode)
113+
}
114+
}

0 commit comments

Comments
 (0)