Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing fields added to AuthResult #556

Merged
merged 27 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
377b887
Missing fields added to AuthResult
mr-kew Jun 27, 2024
a737063
Merge branch 'master' into auth-result
mr-kew Jun 27, 2024
4885a5d
Merge branch 'master' into auth-result
nbransby Jun 27, 2024
dcd28c7
Merge branch 'master' into auth-result
nbransby Jun 30, 2024
31d8466
Removed unused import
mr-kew Jun 30, 2024
37867cf
AuthResult test
mr-kew Jun 30, 2024
3a7f273
Merge branch 'master' into auth-result
nbransby Jul 12, 2024
93b9dda
Added messages to asserts in AuthResult test
mr-kew Jul 15, 2024
ef5e5d9
JVM lint issues fixed
mr-kew Jul 15, 2024
be998c3
JVM lint issues properly fixed
mr-kew Jul 15, 2024
f2c2c51
Public api updated
mr-kew Jul 15, 2024
ed24041
Public api fix
mr-kew Jul 15, 2024
434abaa
AuthResult structure test allows null values
mr-kew Jul 15, 2024
84cdf2c
AuthResult structure test unwrap fixed
mr-kew Jul 15, 2024
a18e3f2
AuthResult structure additionalUserInfo exists
mr-kew Jul 15, 2024
79a7516
AuthResult structure isNewUser default respected
mr-kew Jul 15, 2024
ac4cffa
AuthResult structure test made more forgiving
Jul 17, 2024
277e7f5
Merge branch 'master' into auth-result
mr-kew Aug 16, 2024
e5af67d
AuthResult map parsing fixed in JS
mr-kew Aug 28, 2024
9681cf4
Used Json? instead od dynamic for better clarity
mr-kew Aug 28, 2024
fad3664
AuthResult structure JS test fixed
mr-kew Aug 28, 2024
60e42c0
Merge branch 'master' into auth-result
mr-kew Aug 29, 2024
b566e31
Merge cleanup
mr-kew Aug 29, 2024
1a9a14a
API dump
mr-kew Aug 29, 2024
3eb1a0c
Native properties moved into extensions
mr-kew Aug 29, 2024
ecfa47d
Merge branch 'master' into auth-result
nbransby Aug 30, 2024
9e8ef04
Update auth.kt
nbransby Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions firebase-auth/api/android/firebase-auth.api
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public final class dev/gitlive/firebase/auth/ActionCodeSettings {
public fun toString ()Ljava/lang/String;
}

public final class dev/gitlive/firebase/auth/AdditionalUserInfo {
public fun <init> (Lcom/google/firebase/auth/AdditionalUserInfo;)V
public final fun getAndroid ()Lcom/google/firebase/auth/AdditionalUserInfo;
public final fun getProfile ()Ljava/util/Map;
public final fun getProviderId ()Ljava/lang/String;
public final fun getUsername ()Ljava/lang/String;
public final fun isNewUser ()Z
}

public final class dev/gitlive/firebase/auth/AndroidPackageName {
public fun <init> (Ljava/lang/String;ZLjava/lang/String;)V
public synthetic fun <init> (Ljava/lang/String;ZLjava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand All @@ -74,7 +83,10 @@ public class dev/gitlive/firebase/auth/AuthCredential {
}

public final class dev/gitlive/firebase/auth/AuthResult {
public fun <init> (Lcom/google/firebase/auth/AuthResult;)V
public final fun getAdditionalUserInfo ()Ldev/gitlive/firebase/auth/AdditionalUserInfo;
public final fun getAndroid ()Lcom/google/firebase/auth/AuthResult;
public final fun getCredential ()Ldev/gitlive/firebase/auth/AuthCredential;
public final fun getUser ()Ldev/gitlive/firebase/auth/FirebaseUser;
}

Expand Down
11 changes: 11 additions & 0 deletions firebase-auth/api/jvm/firebase-auth.api
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public final class dev/gitlive/firebase/auth/ActionCodeSettings {
public fun toString ()Ljava/lang/String;
}

public final class dev/gitlive/firebase/auth/AdditionalUserInfo {
public fun <init> ()V
public final fun getProfile ()Ljava/util/Map;
public final fun getProviderId ()Ljava/lang/String;
public final fun getUsername ()Ljava/lang/String;
public final fun isNewUser ()Z
}

public final class dev/gitlive/firebase/auth/AndroidPackageName {
public fun <init> (Ljava/lang/String;ZLjava/lang/String;)V
public synthetic fun <init> (Ljava/lang/String;ZLjava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand All @@ -74,7 +82,10 @@ public class dev/gitlive/firebase/auth/AuthCredential {
}

public final class dev/gitlive/firebase/auth/AuthResult {
public fun <init> (Lcom/google/firebase/auth/AuthResult;)V
public final fun getAdditionalUserInfo ()Ldev/gitlive/firebase/auth/AdditionalUserInfo;
public final fun getAndroid ()Lcom/google/firebase/auth/AuthResult;
public final fun getCredential ()Ldev/gitlive/firebase/auth/AuthCredential;
public final fun getUser ()Ldev/gitlive/firebase/auth/FirebaseUser;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,28 @@ public actual class FirebaseAuth internal constructor(public val android: com.go
public actual fun useEmulator(host: String, port: Int): Unit = android.useEmulator(host, port)
}

public actual class AuthResult internal constructor(public val android: com.google.firebase.auth.AuthResult) {
public actual class AuthResult(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why cant the constructor stay internal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just feel like there is no need for it to be internal. Having it public just allows more flexibility for the library. People can define their own extensions for stuff that is platform specific etc (like my example) and still keep the common types from the library.

I mean sure, in my example I could redefine the AuthResult in my own code and use that instead and this PR kinda removes the need for that code all together. But still the constructor being internal seems like an unnecessary obstacle forcing me to write something that is already there, while offering not much benefit elsewhere.

It's definitely up to discussion, depends if there are any more serious reasons for it to be internal in the first place.

public val android: com.google.firebase.auth.AuthResult,
) {
public actual val user: FirebaseUser?
get() = android.user?.let { FirebaseUser(it) }
public actual val credential: AuthCredential?
get() = android.credential?.let { AuthCredential(it) }
public actual val additionalUserInfo: AdditionalUserInfo?
get() = android.additionalUserInfo?.let { AdditionalUserInfo(it) }
}

public actual class AdditionalUserInfo(
public val android: com.google.firebase.auth.AdditionalUserInfo,
mr-kew marked this conversation as resolved.
Show resolved Hide resolved
) {
public actual val providerId: String?
get() = android.providerId
public actual val username: String?
get() = android.username
public actual val profile: Map<String, Any?>?
get() = android.profile
public actual val isNewUser: Boolean
get() = android.isNewUser
}

public actual class AuthTokenResult(public val android: com.google.firebase.auth.GetTokenResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public expect class FirebaseAuth {

public expect class AuthResult {
public val user: FirebaseUser?
public val credential: AuthCredential?
public val additionalUserInfo: AdditionalUserInfo?
}

public expect class AdditionalUserInfo {
public val providerId: String?
public val username: String?
public val profile: Map<String, Any?>?
public val isNewUser: Boolean
}

public expect class AuthTokenResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ class FirebaseAuthTest {
assertEquals("password", credential.providerId)
}

@Test
fun testAuthResultStructure() = runTest {
val uid = getTestUid("[email protected]", "test123")
val result = auth.signInWithEmailAndPassword("[email protected]", "test123")
assertNotNull(result.user, "User does not exist.")
assertEquals(uid, result.user!!.uid, "uid does not match.")
assertNull(result.credential, "Credential throws.")
assertNotNull(result.additionalUserInfo, "AdditionalUserInfo does not exist.")
// Just test if it does not throw
result.additionalUserInfo!!.providerId
result.additionalUserInfo!!.username
result.additionalUserInfo!!.profile
result.additionalUserInfo!!.isNewUser
}

private suspend fun getTestUid(email: String, password: String): String {
val uid = auth.let {
val user = try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.Flow
import platform.Foundation.NSError
import platform.Foundation.NSString
import platform.Foundation.NSURL

public actual val Firebase.auth: FirebaseAuth
Expand Down Expand Up @@ -102,9 +103,36 @@ public actual class FirebaseAuth internal constructor(public val ios: FIRAuth) {
public actual fun useEmulator(host: String, port: Int): Unit = ios.useEmulatorWithHost(host, port.toLong())
}

public actual class AuthResult internal constructor(public val ios: FIRAuthDataResult) {
public actual class AuthResult(
public val ios: FIRAuthDataResult,
) {
public actual val user: FirebaseUser?
get() = FirebaseUser(ios.user)
public actual val credential: AuthCredential?
get() = ios.credential?.let { AuthCredential(it) }
public actual val additionalUserInfo: AdditionalUserInfo?
get() = ios.additionalUserInfo?.let { AdditionalUserInfo(it) }
}

public actual class AdditionalUserInfo(
public val ios: FIRAdditionalUserInfo,
mr-kew marked this conversation as resolved.
Show resolved Hide resolved
) {
public actual val providerId: String?
get() = ios.providerID
public actual val username: String?
get() = ios.username
public actual val profile: Map<String, Any?>?
get() = ios.profile
?.mapNotNull { (key, value) ->
if (key is NSString && value != null) {
key.toString() to value
} else {
null
}
}
?.toMap()
public actual val isNewUser: Boolean
get() = ios.newUser
}

public actual class AuthTokenResult(public val ios: FIRAuthTokenResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlin.js.json
import dev.gitlive.firebase.auth.externals.AuthResult as JsAuthResult
import dev.gitlive.firebase.auth.externals.AdditionalUserInfo as JsAdditionalUserInfo

public actual val Firebase.auth: FirebaseAuth
get() = rethrow { FirebaseAuth(getAuth()) }
Expand Down Expand Up @@ -109,9 +110,28 @@ public actual class FirebaseAuth internal constructor(public val js: Auth) {
public actual fun useEmulator(host: String, port: Int): Unit = rethrow { connectAuthEmulator(js, "http://$host:$port") }
}

public actual class AuthResult internal constructor(public val js: JsAuthResult) {
public actual class AuthResult(
public val js: JsAuthResult,
) {
public actual val user: FirebaseUser?
get() = rethrow { js.user?.let { FirebaseUser(it) } }
public actual val credential: AuthCredential?
get() = rethrow { js.credential?.let { AuthCredential(it) } }
public actual val additionalUserInfo: AdditionalUserInfo?
get() = js.additionalUserInfo?.let { AdditionalUserInfo(it) }
}

public actual class AdditionalUserInfo(
public val js: JsAdditionalUserInfo,
mr-kew marked this conversation as resolved.
Show resolved Hide resolved
) {
public actual val providerId: String?
get() = js.providerId
public actual val username: String?
get() = js.username
public actual val profile: Map<String, Any?>?
get() = js.profile
public actual val isNewUser: Boolean
get() = js.newUser
}

public actual class AuthTokenResult(public val js: IdTokenResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ public external interface AuthResult {
public val credential: AuthCredential?
public val operationType: String?
public val user: User?
public val additionalUserInfo: AdditionalUserInfo?
}

public external interface AdditionalUserInfo {
public val providerId: String?
public val username: String?
public val profile: Map<String, Any?>?
mr-kew marked this conversation as resolved.
Show resolved Hide resolved
public val newUser: Boolean
}

public external interface AuthCredential {
Expand Down
23 changes: 20 additions & 3 deletions firebase-auth/src/jvmMain/kotlin/dev/gitlive/firebase/auth/auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.tasks.await

public actual val Firebase.auth
public actual val Firebase.auth: FirebaseAuth
get() = FirebaseAuth(com.google.firebase.auth.FirebaseAuth.getInstance())

public actual fun Firebase.auth(app: FirebaseApp) =
public actual fun Firebase.auth(app: FirebaseApp): FirebaseAuth =
FirebaseAuth(com.google.firebase.auth.FirebaseAuth.getInstance(app.android))

public actual class FirebaseAuth internal constructor(public val android: com.google.firebase.auth.FirebaseAuth) {
Expand Down Expand Up @@ -123,9 +123,26 @@ public actual class FirebaseAuth internal constructor(public val android: com.go
}
}

public actual class AuthResult internal constructor(public val android: com.google.firebase.auth.AuthResult) {
public actual class AuthResult(
public val android: com.google.firebase.auth.AuthResult,
) {
public actual val user: FirebaseUser?
get() = android.user?.let { FirebaseUser(it) }
public actual val credential: AuthCredential?
get() = throw NotImplementedError()
public actual val additionalUserInfo: AdditionalUserInfo?
get() = throw NotImplementedError()
}

public actual class AdditionalUserInfo {
public actual val providerId: String?
get() = throw NotImplementedError()
public actual val username: String?
get() = throw NotImplementedError()
public actual val profile: Map<String, Any?>?
get() = throw NotImplementedError()
public actual val isNewUser: Boolean
get() = throw NotImplementedError()
}

public actual class AuthTokenResult(public val android: com.google.firebase.auth.GetTokenResult) {
Expand Down
Loading