@@ -7,6 +7,7 @@ package dev.gitlive.firebase.auth
77
88import com.google.firebase.auth.ActionCodeEmailInfo
99import com.google.firebase.auth.ActionCodeMultiFactorInfo
10+ import com.google.firebase.auth.ActionCodeResult.*
1011import com.google.firebase.auth.FirebaseAuth.AuthStateListener
1112import dev.gitlive.firebase.Firebase
1213import dev.gitlive.firebase.FirebaseApp
@@ -44,19 +45,18 @@ actual class FirebaseAuth internal constructor(val android: com.google.firebase.
4445 set(value) { android.setLanguageCode(value) }
4546
4647 actual suspend fun applyActionCode (code : String ) = android.applyActionCode(code).await().run { Unit }
47- actual suspend fun checkActionCode (code : String ): ActionCodeResult = ActionCodeResult (android.checkActionCode(code).await())
4848 actual suspend fun confirmPasswordReset (code : String , newPassword : String ) = android.confirmPasswordReset(code, newPassword).await().run { Unit }
4949
5050 actual suspend fun createUserWithEmailAndPassword (email : String , password : String ) =
5151 AuthResult (android.createUserWithEmailAndPassword(email, password).await())
5252
53- actual suspend fun fetchSignInMethodsForEmail (email : String ): SignInMethodQueryResult = SignInMethodQueryResult ( android.fetchSignInMethodsForEmail(email).await())
53+ actual suspend fun fetchSignInMethodsForEmail (email : String ): List < String > = android.fetchSignInMethodsForEmail(email).await().signInMethods.orEmpty( )
5454
5555 actual suspend fun sendPasswordResetEmail (email : String , actionCodeSettings : ActionCodeSettings ? ) {
56- android.sendPasswordResetEmail(email, actionCodeSettings?.android ).await()
56+ android.sendPasswordResetEmail(email, actionCodeSettings?.toAndroid() ).await()
5757 }
5858
59- actual suspend fun sendSignInLinkToEmail (email : String , actionCodeSettings : ActionCodeSettings ) = android.sendSignInLinkToEmail(email, actionCodeSettings.android ).await().run { Unit }
59+ actual suspend fun sendSignInLinkToEmail (email : String , actionCodeSettings : ActionCodeSettings ) = android.sendSignInLinkToEmail(email, actionCodeSettings.toAndroid() ).await().run { Unit }
6060
6161 actual suspend fun signInWithEmailAndPassword (email : String , password : String ) =
6262 AuthResult (android.signInWithEmailAndPassword(email, password).await())
@@ -73,75 +73,42 @@ actual class FirebaseAuth internal constructor(val android: com.google.firebase.
7373
7474 actual suspend fun updateCurrentUser (user : FirebaseUser ) = android.updateCurrentUser(user.android).await().run { Unit }
7575 actual suspend fun verifyPasswordResetCode (code : String ): String = android.verifyPasswordResetCode(code).await()
76+
77+ actual suspend fun <T : ActionCodeResult > checkActionCode (code : String ): T {
78+ val result = android.checkActionCode(code).await()
79+ @Suppress(" UNCHECKED_CAST" )
80+ return when (result.operation) {
81+ ERROR -> ActionCodeResult .Error
82+ SIGN_IN_WITH_EMAIL_LINK -> ActionCodeResult .SignInWithEmailLink
83+ VERIFY_EMAIL -> ActionCodeResult .VerifyEmail (result.info!! .email)
84+ PASSWORD_RESET -> ActionCodeResult .PasswordReset (result.info!! .email)
85+ RECOVER_EMAIL -> (result.info as ActionCodeEmailInfo ).run {
86+ ActionCodeResult .RecoverEmail (email, previousEmail)
87+ }
88+ VERIFY_BEFORE_CHANGE_EMAIL -> (result.info as ActionCodeEmailInfo ).run {
89+ ActionCodeResult .VerifyBeforeChangeEmail (email, previousEmail)
90+ }
91+ REVERT_SECOND_FACTOR_ADDITION -> (result.info as ActionCodeMultiFactorInfo ).run {
92+ ActionCodeResult .RevertSecondFactorAddition (email, MultiFactorInfo (multiFactorInfo))
93+ }
94+ else -> throw UnsupportedOperationException (result.operation.toString())
95+ } as T
96+ }
97+
7698}
7799
78100actual class AuthResult internal constructor(val android : com.google.firebase.auth.AuthResult ) {
79101 actual val user: FirebaseUser ?
80102 get() = android.user?.let { FirebaseUser (it) }
81103}
82104
83- actual class ActionCodeResult (val android : com.google.firebase.auth.ActionCodeResult ) {
84- actual val operation: Operation
85- get() = when (android.operation) {
86- com.google.firebase.auth.ActionCodeResult .PASSWORD_RESET -> Operation .PasswordReset (this )
87- com.google.firebase.auth.ActionCodeResult .VERIFY_EMAIL -> Operation .VerifyEmail (this )
88- com.google.firebase.auth.ActionCodeResult .RECOVER_EMAIL -> Operation .RecoverEmail (this )
89- com.google.firebase.auth.ActionCodeResult .ERROR -> Operation .Error
90- com.google.firebase.auth.ActionCodeResult .SIGN_IN_WITH_EMAIL_LINK -> Operation .SignInWithEmailLink
91- com.google.firebase.auth.ActionCodeResult .VERIFY_BEFORE_CHANGE_EMAIL -> Operation .VerifyBeforeChangeEmail (this )
92- com.google.firebase.auth.ActionCodeResult .REVERT_SECOND_FACTOR_ADDITION -> Operation .RevertSecondFactorAddition (this )
93- else -> Operation .Error
94- }
95- }
96-
97- internal actual sealed class ActionCodeDataType <out T > {
98-
99- actual abstract fun dataForResult (result : ActionCodeResult ): T
100-
101- actual object Email : ActionCodeDataType<String>() {
102- override fun dataForResult (result : ActionCodeResult ): String = result.android.info!! .email
103- }
104- actual object PreviousEmail : ActionCodeDataType<String>() {
105- override fun dataForResult (result : ActionCodeResult ): String = (result.android.info as ActionCodeEmailInfo ).previousEmail
106- }
107- actual object MultiFactor : ActionCodeDataType<MultiFactorInfo?>() {
108- override fun dataForResult (result : ActionCodeResult ): MultiFactorInfo ? = (result.android.info as ? ActionCodeMultiFactorInfo )?.multiFactorInfo?.let { MultiFactorInfo (it) }
109- }
110- }
111-
112- actual class SignInMethodQueryResult (val android : com.google.firebase.auth.SignInMethodQueryResult ) {
113- actual val signInMethods: List <String >
114- get() = android.signInMethods ? : emptyList()
115- }
116-
117- actual class ActionCodeSettings private constructor(val android : com.google.firebase.auth.ActionCodeSettings ) {
118-
119- actual constructor (url: String ,
120- androidPackageName: AndroidPackageName ? ,
121- dynamicLinkDomain: String? ,
122- canHandleCodeInApp: Boolean ,
123- iOSBundleId: String?
124- ) : this (com.google.firebase.auth.ActionCodeSettings .newBuilder().apply {
125- this .url = url
126- androidPackageName?.let {
127- this .setAndroidPackageName(it.androidPackageName, it.installIfNotAvailable, it.minimumVersion)
128- }
129- this .dynamicLinkDomain = dynamicLinkDomain
130- this .handleCodeInApp = canHandleCodeInApp
131- this .iosBundleId = iosBundleId
132- }.build())
133-
134- actual val canHandleCodeInApp: Boolean
135- get() = android.canHandleCodeInApp()
136- actual val androidPackageName: AndroidPackageName ?
137- get() = android.androidPackageName?.let {
138- AndroidPackageName (it, android.androidInstallApp, android.androidMinimumVersion)
139- }
140- actual val iOSBundle: String?
141- get() = android.iosBundle
142- actual val url: String
143- get() = android.url
144- }
105+ internal fun ActionCodeSettings.toAndroid () = com.google.firebase.auth.ActionCodeSettings .newBuilder()
106+ .setUrl(url)
107+ .also { androidPackageName?.run { it.setAndroidPackageName(packageName, installIfNotAvailable, minimumVersion) } }
108+ .also { dynamicLinkDomain?.run { it.setDynamicLinkDomain(this ) } }
109+ .setHandleCodeInApp(canHandleCodeInApp)
110+ .also { iOSBundleId?.run { it.setIOSBundleId(this ) } }
111+ .build()
145112
146113actual typealias FirebaseAuthException = com.google.firebase.auth.FirebaseAuthException
147114actual typealias FirebaseAuthActionCodeException = com.google.firebase.auth.FirebaseAuthActionCodeException
0 commit comments