Skip to content

Commit

Permalink
Merge pull request #556 from mr-kew/auth-result
Browse files Browse the repository at this point in the history
Missing fields added to AuthResult
  • Loading branch information
nbransby authored Aug 30, 2024
2 parents ba73861 + 9e8ef04 commit fc7cb62
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 5 deletions.
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,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> (Lcom/google/firebase/auth/AdditionalUserInfo;)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,6 +82,9 @@ 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 getCredential ()Ldev/gitlive/firebase/auth/AuthCredential;
public final fun getUser ()Ldev/gitlive/firebase/auth/FirebaseUser;
}

Expand Down Expand Up @@ -278,6 +289,7 @@ public final class dev/gitlive/firebase/auth/UserMetaData {

public final class dev/gitlive/firebase/auth/android {
public static final fun auth (Ldev/gitlive/firebase/Firebase;Ldev/gitlive/firebase/FirebaseApp;)Ldev/gitlive/firebase/auth/FirebaseAuth;
public static final fun getAndroid (Ldev/gitlive/firebase/auth/AdditionalUserInfo;)Lcom/google/firebase/auth/AdditionalUserInfo;
public static final fun getAndroid (Ldev/gitlive/firebase/auth/AuthResult;)Lcom/google/firebase/auth/AuthResult;
public static final fun getAndroid (Ldev/gitlive/firebase/auth/AuthTokenResult;)Lcom/google/firebase/auth/GetTokenResult;
public static final fun getAndroid (Ldev/gitlive/firebase/auth/FirebaseAuth;)Lcom/google/firebase/auth/FirebaseAuth;
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,6 +82,9 @@ 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 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 @@ -118,9 +118,29 @@ public actual class FirebaseAuth internal constructor(internal val android: com.

public val AuthResult.android: com.google.firebase.auth.AuthResult get() = android

public actual class AuthResult internal constructor(internal val android: com.google.firebase.auth.AuthResult) {
public actual class AuthResult(internal 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 val AdditionalUserInfo.android: com.google.firebase.auth.AdditionalUserInfo
get() = android

public actual class AdditionalUserInfo(
internal val android: com.google.firebase.auth.AdditionalUserInfo,
) {
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 val AuthTokenResult.android: com.google.firebase.auth.GetTokenResult get() = android
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 @@ -112,6 +112,22 @@ class FirebaseAuthTest {
assertEquals(uid, result.user!!.uid)
}

@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.")
// Just test if it does not throw
result.additionalUserInfo?.let { additionalUserInfo ->
additionalUserInfo.providerId
additionalUserInfo.username
additionalUserInfo.profile
additionalUserInfo.isNewUser
}
}

@Test
fun testIsSignInWithEmailLink() {
val validLink = "http://localhost:9099/emulator/action?mode=signIn&lang=en&oobCode=_vr0QcFcxcVeLZbrcU-GpTaZiuxlHquqdC8MSy0YM_vzWCTAQgV9Jq&apiKey=fake-api-key&continueUrl=https%3A%2F%2Fexample.com%2Fsignin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 val FirebaseAuth.ios: FIRAuth get() = FIRAuth.auth()
Expand Down Expand Up @@ -104,11 +105,39 @@ public actual class FirebaseAuth internal constructor(internal val ios: FIRAuth)

public actual fun useEmulator(host: String, port: Int): Unit = ios.useEmulatorWithHost(host, port.toLong())
}

public val AuthResult.ios: FIRAuthDataResult get() = ios

public actual class AuthResult internal constructor(internal val ios: FIRAuthDataResult) {
public actual class AuthResult(internal 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 val AdditionalUserInfo.ios: FIRAdditionalUserInfo get() = ios

public actual class AdditionalUserInfo(
internal val ios: FIRAdditionalUserInfo,
) {
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 val AuthTokenResult.ios: FIRAuthTokenResult get() = ios
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ import kotlinx.coroutines.await
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlin.js.Json
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 @@ -133,9 +135,34 @@ public actual class FirebaseAuth internal constructor(internal val js: Auth) {

public val AuthResult.js: JsAuthResult get() = js

public actual class AuthResult internal constructor(internal val js: JsAuthResult) {
public actual class AuthResult(internal 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() = rethrow { js.additionalUserInfo?.let { AdditionalUserInfo(it) } }
}

public val AdditionalUserInfo.js: JsAdditionalUserInfo get() = js

public actual class AdditionalUserInfo(
internal val js: JsAdditionalUserInfo,
) {
public actual val providerId: String?
get() = js.providerId
public actual val username: String?
get() = js.username
public actual val profile: Map<String, Any?>?
get() = rethrow {
val profile = js.profile ?: return@rethrow null
val entries = js("Object.entries") as (Json) -> Array<Array<Any?>>
return@rethrow entries
.invoke(profile)
.associate { entry -> entry[0] as String to entry[1] }
}
public actual val isNewUser: Boolean
get() = js.newUser
}

public val AuthTokenResult.js: IdTokenResult get() = js
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: Json?
public val newUser: Boolean
}

public external interface AuthCredential {
Expand Down
19 changes: 17 additions & 2 deletions firebase-auth/src/jvmMain/kotlin/dev/gitlive/firebase/auth/auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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) =
Expand Down Expand Up @@ -127,9 +127,24 @@ public actual class FirebaseAuth internal constructor(internal val android: com.

public val AuthResult.android: com.google.firebase.auth.AuthResult get() = android

public actual class AuthResult internal constructor(internal val android: com.google.firebase.auth.AuthResult) {
public actual class AuthResult(internal 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 val AuthTokenResult.android: com.google.firebase.auth.GetTokenResult get() = android
Expand Down

0 comments on commit fc7cb62

Please sign in to comment.