2
2
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3
3
*/
4
4
5
+ @file:JvmName(" android" )
5
6
package dev.gitlive.firebase.auth
6
7
7
- import com.google.firebase.auth.EmailAuthProvider
8
+ import com.google.firebase.auth.ActionCodeEmailInfo
9
+ import com.google.firebase.auth.ActionCodeMultiFactorInfo
8
10
import com.google.firebase.auth.FirebaseAuth.AuthStateListener
9
11
import dev.gitlive.firebase.Firebase
10
12
import dev.gitlive.firebase.FirebaseApp
11
13
import kotlinx.coroutines.channels.awaitClose
14
+ import kotlinx.coroutines.flow.Flow
12
15
import kotlinx.coroutines.flow.callbackFlow
13
16
import kotlinx.coroutines.tasks.await
14
17
@@ -22,71 +25,129 @@ actual class FirebaseAuth internal constructor(val android: com.google.firebase.
22
25
actual val currentUser: FirebaseUser ?
23
26
get() = android.currentUser?.let { FirebaseUser (it) }
24
27
25
- actual suspend fun sendPasswordResetEmail (email : String ) {
26
- android.sendPasswordResetEmail(email).await()
28
+ actual val authStateChanged get() = callbackFlow {
29
+ val listener = AuthStateListener { auth -> offer(auth.currentUser?.let { FirebaseUser (it) }) }
30
+ android.addAuthStateListener(listener)
31
+ awaitClose { android.removeAuthStateListener(listener) }
27
32
}
28
33
29
- actual suspend fun signInWithEmailAndPassword (email : String , password : String ) =
30
- AuthResult (android.signInWithEmailAndPassword(email, password).await())
34
+ actual val idTokenChanged: Flow <FirebaseUser ?>
35
+ get() = callbackFlow {
36
+ val listener = com.google.firebase.auth.FirebaseAuth .IdTokenListener { auth -> offer(auth.currentUser?.let { FirebaseUser (it) })}
37
+ android.addIdTokenListener(listener)
38
+ awaitClose { android.removeIdTokenListener(listener) }
39
+ }
40
+
41
+ actual var languageCode: String
42
+ get() = android.languageCode ? : " "
43
+ set(value) { android.setLanguageCode(value) }
44
+
45
+ actual suspend fun applyActionCode (code : String ) = android.applyActionCode(code).await().run { Unit }
46
+ actual suspend fun checkActionCode (code : String ): ActionCodeResult = ActionCodeResult (android.checkActionCode(code).await())
47
+ actual suspend fun confirmPasswordReset (code : String , newPassword : String ) = android.confirmPasswordReset(code, newPassword).await().run { Unit }
31
48
32
49
actual suspend fun createUserWithEmailAndPassword (email : String , password : String ) =
33
50
AuthResult (android.createUserWithEmailAndPassword(email, password).await())
34
51
52
+ actual suspend fun fetchSignInMethodsForEmail (email : String ): SignInMethodQueryResult = SignInMethodQueryResult (android.fetchSignInMethodsForEmail(email).await())
53
+
54
+ actual suspend fun sendPasswordResetEmail (email : String , actionCodeSettings : ActionCodeSettings ? ) {
55
+ android.sendPasswordResetEmail(email, actionCodeSettings?.android).await()
56
+ }
57
+
58
+ actual suspend fun sendSignInLinkToEmail (email : String , actionCodeSettings : ActionCodeSettings ) = android.sendSignInLinkToEmail(email, actionCodeSettings.android).await().run { Unit }
59
+
60
+ actual suspend fun signInWithEmailAndPassword (email : String , password : String ) =
61
+ AuthResult (android.signInWithEmailAndPassword(email, password).await())
62
+
35
63
actual suspend fun signInWithCustomToken (token : String ) =
36
64
AuthResult (android.signInWithCustomToken(token).await())
37
65
38
66
actual suspend fun signInAnonymously () = AuthResult (android.signInAnonymously().await())
39
67
40
- actual val authStateChanged get() = callbackFlow {
41
- val listener = object : AuthStateListener {
42
- override fun onAuthStateChanged (auth : com.google.firebase.auth.FirebaseAuth ) {
43
- offer(auth.currentUser?.let { FirebaseUser (it) })
44
- }
45
- }
46
- android.addAuthStateListener(listener)
47
- awaitClose { android.removeAuthStateListener(listener) }
48
- }
49
68
actual suspend fun signInWithCredential (authCredential : AuthCredential ) =
50
69
AuthResult (android.signInWithCredential(authCredential.android).await())
51
70
52
71
actual suspend fun signOut () = android.signOut()
53
- }
54
72
55
- actual class AuthCredential (val android : com.google.firebase.auth.AuthCredential )
73
+ actual suspend fun updateCurrentUser (user : FirebaseUser ) = android.updateCurrentUser(user.android).await().run { Unit }
74
+ actual suspend fun verifyPasswordResetCode (code : String ): String = android.verifyPasswordResetCode(code).await()
75
+ }
56
76
57
77
actual class AuthResult internal constructor(val android : com.google.firebase.auth.AuthResult ) {
58
78
actual val user: FirebaseUser ?
59
79
get() = android.user?.let { FirebaseUser (it) }
60
80
}
61
81
62
- actual class FirebaseUser internal constructor(val android : com.google.firebase.auth.FirebaseUser ) {
63
- actual val uid: String
64
- get() = android.uid
65
- actual val displayName: String?
66
- get() = android.displayName
67
- actual val email: String?
68
- get() = android.email
69
- actual val phoneNumber: String?
70
- get() = android.phoneNumber
71
- actual val isAnonymous: Boolean
72
- get() = android.isAnonymous
73
- actual suspend fun delete () = android.delete().await().run { Unit }
74
- actual suspend fun reload () = android.reload().await().run { Unit }
75
- actual suspend fun sendEmailVerification () = android.sendEmailVerification().await().run { Unit }
82
+ actual class ActionCodeResult (val android : com.google.firebase.auth.ActionCodeResult ) {
83
+ actual val operation: Operation
84
+ get() = when (android.operation) {
85
+ com.google.firebase.auth.ActionCodeResult .PASSWORD_RESET -> Operation .PasswordReset (this )
86
+ com.google.firebase.auth.ActionCodeResult .VERIFY_EMAIL -> Operation .VerifyEmail (this )
87
+ com.google.firebase.auth.ActionCodeResult .RECOVER_EMAIL -> Operation .RecoverEmail (this )
88
+ com.google.firebase.auth.ActionCodeResult .ERROR -> Operation .Error
89
+ com.google.firebase.auth.ActionCodeResult .SIGN_IN_WITH_EMAIL_LINK -> Operation .SignInWithEmailLink
90
+ com.google.firebase.auth.ActionCodeResult .VERIFY_BEFORE_CHANGE_EMAIL -> Operation .VerifyBeforeChangeEmail (this )
91
+ com.google.firebase.auth.ActionCodeResult .REVERT_SECOND_FACTOR_ADDITION -> Operation .RevertSecondFactorAddition (this )
92
+ else -> Operation .Error
93
+ }
94
+ }
95
+
96
+ internal actual sealed class ActionCodeDataType <out T > {
97
+
98
+ actual abstract fun dataForResult (result : ActionCodeResult ): T
99
+
100
+ actual object Email : ActionCodeDataType<String>() {
101
+ override fun dataForResult (result : ActionCodeResult ): String = result.android.info!! .email
102
+ }
103
+ actual object PreviousEmail : ActionCodeDataType<String>() {
104
+ override fun dataForResult (result : ActionCodeResult ): String = (result.android.info as ActionCodeEmailInfo ).previousEmail
105
+ }
106
+ actual object MultiFactor : ActionCodeDataType<MultiFactorInfo?>() {
107
+ override fun dataForResult (result : ActionCodeResult ): MultiFactorInfo ? = (result.android.info as ? ActionCodeMultiFactorInfo )?.multiFactorInfo?.let { MultiFactorInfo (it) }
108
+ }
109
+ }
110
+
111
+ actual class SignInMethodQueryResult (val android : com.google.firebase.auth.SignInMethodQueryResult ) {
112
+ actual val signInMethods: List <String >
113
+ get() = android.signInMethods ? : emptyList()
114
+ }
115
+
116
+ actual class ActionCodeSettings private constructor(val android : com.google.firebase.auth.ActionCodeSettings ) {
117
+
118
+ actual constructor (url: String ,
119
+ androidPackageName: AndroidPackageName ? ,
120
+ dynamicLinkDomain: String? ,
121
+ canHandleCodeInApp: Boolean ,
122
+ iOSBundleId: String?
123
+ ) : this (com.google.firebase.auth.ActionCodeSettings .newBuilder().apply {
124
+ this .url = url
125
+ androidPackageName?.let {
126
+ this .setAndroidPackageName(it.androidPackageName, it.installIfNotAvailable, it.minimumVersion)
127
+ }
128
+ this .dynamicLinkDomain = dynamicLinkDomain
129
+ this .handleCodeInApp = canHandleCodeInApp
130
+ this .iosBundleId = iosBundleId
131
+ }.build())
132
+
133
+ actual val canHandleCodeInApp: Boolean
134
+ get() = android.canHandleCodeInApp()
135
+ actual val androidPackageName: AndroidPackageName ?
136
+ get() = android.androidPackageName?.let {
137
+ AndroidPackageName (it, android.androidInstallApp, android.androidMinimumVersion)
138
+ }
139
+ actual val iOSBundle: String?
140
+ get() = android.iosBundle
141
+ actual val url: String
142
+ get() = android.url
76
143
}
77
144
78
145
actual typealias FirebaseAuthException = com.google.firebase.auth.FirebaseAuthException
79
146
actual typealias FirebaseAuthActionCodeException = com.google.firebase.auth.FirebaseAuthActionCodeException
80
147
actual typealias FirebaseAuthEmailException = com.google.firebase.auth.FirebaseAuthEmailException
81
148
actual typealias FirebaseAuthInvalidCredentialsException = com.google.firebase.auth.FirebaseAuthInvalidCredentialsException
82
149
actual typealias FirebaseAuthInvalidUserException = com.google.firebase.auth.FirebaseAuthInvalidUserException
150
+ actual typealias FirebaseAuthMultiFactorException = com.google.firebase.auth.FirebaseAuthMultiFactorException
83
151
actual typealias FirebaseAuthRecentLoginRequiredException = com.google.firebase.auth.FirebaseAuthRecentLoginRequiredException
84
152
actual typealias FirebaseAuthUserCollisionException = com.google.firebase.auth.FirebaseAuthUserCollisionException
85
153
actual typealias FirebaseAuthWebException = com.google.firebase.auth.FirebaseAuthWebException
86
-
87
- actual object EmailAuthProvider {
88
- actual fun credentialWithEmail (
89
- email : String ,
90
- password : String
91
- ): AuthCredential = AuthCredential (EmailAuthProvider .getCredential(email, password))
92
- }
0 commit comments