|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +@file:JvmName("android") |
| 6 | +package dev.gitlive.firebase.auth |
| 7 | + |
| 8 | +import com.google.firebase.auth.ActionCodeEmailInfo |
| 9 | +import com.google.firebase.auth.ActionCodeMultiFactorInfo |
| 10 | +import com.google.firebase.auth.ActionCodeResult.* |
| 11 | +import com.google.firebase.auth.FirebaseAuth.AuthStateListener |
| 12 | +import dev.gitlive.firebase.Firebase |
| 13 | +import dev.gitlive.firebase.FirebaseApp |
| 14 | +import kotlinx.coroutines.channels.awaitClose |
| 15 | +import kotlinx.coroutines.flow.Flow |
| 16 | +import kotlinx.coroutines.flow.callbackFlow |
| 17 | +import kotlinx.coroutines.tasks.await |
| 18 | + |
| 19 | +actual val Firebase.auth |
| 20 | + get() = FirebaseAuth(com.google.firebase.auth.FirebaseAuth.getInstance()) |
| 21 | + |
| 22 | +actual fun Firebase.auth(app: FirebaseApp) = |
| 23 | + FirebaseAuth(com.google.firebase.auth.FirebaseAuth.getInstance(app.android)) |
| 24 | + |
| 25 | +actual class FirebaseAuth internal constructor(val android: com.google.firebase.auth.FirebaseAuth) { |
| 26 | + actual val currentUser: FirebaseUser? |
| 27 | + get() = android.currentUser?.let { FirebaseUser(it) } |
| 28 | + |
| 29 | + actual val authStateChanged: Flow<FirebaseUser?> get() = callbackFlow { |
| 30 | + val listener = object : AuthStateListener { |
| 31 | + override fun onAuthStateChanged(auth: com.google.firebase.auth.FirebaseAuth) { |
| 32 | + trySend(auth.currentUser?.let { FirebaseUser(it) }) |
| 33 | + } |
| 34 | + } |
| 35 | + android.addAuthStateListener(listener) |
| 36 | + awaitClose { android.removeAuthStateListener(listener) } |
| 37 | + } |
| 38 | + |
| 39 | + actual val idTokenChanged get(): Flow<FirebaseUser?> = callbackFlow { |
| 40 | + val listener = object : com.google.firebase.auth.FirebaseAuth.IdTokenListener { |
| 41 | + override fun onIdTokenChanged(auth: com.google.firebase.auth.FirebaseAuth) { |
| 42 | + trySend(auth.currentUser?.let { FirebaseUser(it) }) |
| 43 | + } |
| 44 | + } |
| 45 | + android.addIdTokenListener(listener) |
| 46 | + awaitClose { android.removeIdTokenListener(listener) } |
| 47 | + } |
| 48 | + |
| 49 | + actual var languageCode: String |
| 50 | + get() = android.languageCode.orEmpty() |
| 51 | + set(value) { android.setLanguageCode(value) } |
| 52 | + |
| 53 | + |
| 54 | + actual suspend fun applyActionCode(code: String) = android.applyActionCode(code).await().run { Unit } |
| 55 | + actual suspend fun confirmPasswordReset(code: String, newPassword: String) = android.confirmPasswordReset(code, newPassword).await().run { Unit } |
| 56 | + |
| 57 | + actual suspend fun createUserWithEmailAndPassword(email: String, password: String) = |
| 58 | + AuthResult(android.createUserWithEmailAndPassword(email, password).await()) |
| 59 | + |
| 60 | + actual suspend fun fetchSignInMethodsForEmail(email: String): List<String> = android.fetchSignInMethodsForEmail(email).await().signInMethods.orEmpty() |
| 61 | + |
| 62 | + actual suspend fun sendPasswordResetEmail(email: String, actionCodeSettings: ActionCodeSettings?) { |
| 63 | + android.sendPasswordResetEmail(email, actionCodeSettings?.toAndroid()).await() |
| 64 | + } |
| 65 | + |
| 66 | + actual suspend fun sendSignInLinkToEmail(email: String, actionCodeSettings: ActionCodeSettings) = android.sendSignInLinkToEmail(email, actionCodeSettings.toAndroid()).await().run { Unit } |
| 67 | + |
| 68 | + actual fun isSignInWithEmailLink(link: String) = android.isSignInWithEmailLink(link) |
| 69 | + |
| 70 | + actual suspend fun signInWithEmailAndPassword(email: String, password: String) = |
| 71 | + AuthResult(android.signInWithEmailAndPassword(email, password).await()) |
| 72 | + |
| 73 | + actual suspend fun signInWithCustomToken(token: String) = |
| 74 | + AuthResult(android.signInWithCustomToken(token).await()) |
| 75 | + |
| 76 | + actual suspend fun signInAnonymously() = AuthResult(android.signInAnonymously().await()) |
| 77 | + |
| 78 | + actual suspend fun signInWithCredential(authCredential: AuthCredential) = |
| 79 | + AuthResult(android.signInWithCredential(authCredential.android).await()) |
| 80 | + |
| 81 | + actual suspend fun signInWithEmailLink(email: String, link: String) = |
| 82 | + AuthResult(android.signInWithEmailLink(email, link).await()) |
| 83 | + |
| 84 | + actual suspend fun signOut() = android.signOut() |
| 85 | + |
| 86 | + actual suspend fun updateCurrentUser(user: FirebaseUser) = android.updateCurrentUser(user.android).await().run { Unit } |
| 87 | + actual suspend fun verifyPasswordResetCode(code: String): String = android.verifyPasswordResetCode(code).await() |
| 88 | + |
| 89 | + actual suspend fun <T : ActionCodeResult> checkActionCode(code: String): T { |
| 90 | + val result = android.checkActionCode(code).await() |
| 91 | + @Suppress("UNCHECKED_CAST") |
| 92 | + return when(result.operation) { |
| 93 | + SIGN_IN_WITH_EMAIL_LINK -> ActionCodeResult.SignInWithEmailLink |
| 94 | + VERIFY_EMAIL -> ActionCodeResult.VerifyEmail(result.info!!.email) |
| 95 | + PASSWORD_RESET -> ActionCodeResult.PasswordReset(result.info!!.email) |
| 96 | + RECOVER_EMAIL -> (result.info as ActionCodeEmailInfo).run { |
| 97 | + ActionCodeResult.RecoverEmail(email, previousEmail) |
| 98 | + } |
| 99 | + VERIFY_BEFORE_CHANGE_EMAIL -> (result.info as ActionCodeEmailInfo).run { |
| 100 | + ActionCodeResult.VerifyBeforeChangeEmail(email, previousEmail) |
| 101 | + } |
| 102 | + REVERT_SECOND_FACTOR_ADDITION -> (result.info as ActionCodeMultiFactorInfo).run { |
| 103 | + ActionCodeResult.RevertSecondFactorAddition(email, MultiFactorInfo(multiFactorInfo)) |
| 104 | + } |
| 105 | + ERROR -> throw UnsupportedOperationException(result.operation.toString()) |
| 106 | + else -> throw UnsupportedOperationException(result.operation.toString()) |
| 107 | + } as T |
| 108 | + } |
| 109 | + |
| 110 | + actual fun useEmulator(host: String, port: Int) = android.useEmulator(host, port) |
| 111 | +} |
| 112 | + |
| 113 | +actual class AuthResult internal constructor(val android: com.google.firebase.auth.AuthResult) { |
| 114 | + actual val user: FirebaseUser? |
| 115 | + get() = android.user?.let { FirebaseUser(it) } |
| 116 | +} |
| 117 | + |
| 118 | +actual class AuthTokenResult(val android: com.google.firebase.auth.GetTokenResult) { |
| 119 | + // actual val authTimestamp: Long |
| 120 | +// get() = android.authTimestamp |
| 121 | + actual val claims: Map<String, Any> |
| 122 | + get() = android.claims |
| 123 | + // actual val expirationTimestamp: Long |
| 124 | +// get() = android.expirationTimestamp |
| 125 | +// actual val issuedAtTimestamp: Long |
| 126 | +// get() = android.issuedAtTimestamp |
| 127 | + actual val signInProvider: String? |
| 128 | + get() = android.signInProvider |
| 129 | + actual val token: String? |
| 130 | + get() = android.token |
| 131 | +} |
| 132 | + |
| 133 | +internal fun ActionCodeSettings.toAndroid() = com.google.firebase.auth.ActionCodeSettings.newBuilder() |
| 134 | + .setUrl(url) |
| 135 | + .also { androidPackageName?.run { it.setAndroidPackageName(packageName, installIfNotAvailable, minimumVersion) } } |
| 136 | + .also { dynamicLinkDomain?.run { it.setDynamicLinkDomain(this) } } |
| 137 | + .setHandleCodeInApp(canHandleCodeInApp) |
| 138 | + .also { iOSBundleId?.run { it.setIOSBundleId(this) } } |
| 139 | + .build() |
| 140 | + |
| 141 | +actual typealias FirebaseAuthException = com.google.firebase.auth.FirebaseAuthException |
| 142 | +actual typealias FirebaseAuthActionCodeException = com.google.firebase.auth.FirebaseAuthActionCodeException |
| 143 | +actual typealias FirebaseAuthEmailException = com.google.firebase.auth.FirebaseAuthEmailException |
| 144 | +actual typealias FirebaseAuthInvalidCredentialsException = com.google.firebase.auth.FirebaseAuthInvalidCredentialsException |
| 145 | +actual typealias FirebaseAuthWeakPasswordException = com.google.firebase.auth.FirebaseAuthWeakPasswordException |
| 146 | +actual typealias FirebaseAuthInvalidUserException = com.google.firebase.auth.FirebaseAuthInvalidUserException |
| 147 | +actual typealias FirebaseAuthMultiFactorException = com.google.firebase.auth.FirebaseAuthMultiFactorException |
| 148 | +actual typealias FirebaseAuthRecentLoginRequiredException = com.google.firebase.auth.FirebaseAuthRecentLoginRequiredException |
| 149 | +actual typealias FirebaseAuthUserCollisionException = com.google.firebase.auth.FirebaseAuthUserCollisionException |
| 150 | +actual typealias FirebaseAuthWebException = com.google.firebase.auth.FirebaseAuthWebException |
0 commit comments