Skip to content

Commit

Permalink
Update firebase implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Aug 31, 2023
1 parent a111046 commit 54e52f4
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,46 @@
// SPDX-License-Identifier: MIT
//

import Foundation
import SpeziAccount
import SwiftUI

// TODO docs
public struct FirebaseEmailVerifiedKey: OptionalAccountValueKey {

/// Flag indicating if the firebase account has a verified email address.
///
/// - Important: This key is read-only and cannot be modified.
public struct FirebaseEmailVerifiedKey: AccountKey {
public typealias Value = Bool
public static var name: LocalizedStringResource = "E-Mail Verified"
public static var category: AccountKeyCategory = .other
public static var initialValue: InitialValue<Bool> = .default(false)
}


extension AccountKeys {
/// The email-verified ``FirebaseEmailVerifiedKey`` metatype.
public var isEmailVerified: FirebaseEmailVerifiedKey.Type {
FirebaseEmailVerifiedKey.self
}
}


extension AccountValues {
/// Access if the user's email of their firebase account is verified.
public var isEmailVerified: Bool {
storage[FirebaseEmailVerifiedKey.self] ?? false
}
}

// this property is not supported in SignupRequests
extension AccountDetails {
public var isEmailVerified: Bool? { // swiftlint:disable:this discouraged_optional_boolean
storage[FirebaseEmailVerifiedKey.self]

extension FirebaseEmailVerifiedKey {
public struct DataEntry: DataEntryView {
public typealias Key = FirebaseEmailVerifiedKey

public var body: some View {
Text("The FirebaseEmailVerifiedKey cannot be set!")
}

public init(_ value: Binding<Value>) {}
}
}
16 changes: 11 additions & 5 deletions Sources/SpeziFirebaseAccount/FirebaseAccountConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class FirebaseAccountConfiguration: Component {
private let emulatorSettings: (host: String, port: Int)?
private let authenticationMethods: FirebaseAuthAuthenticationMethods

public let accountService: FirebaseEmailPasswordAccountService // TODO this protocol requirement requires us to make the service public!
@Provide var accountServices: [any AccountService]

/// - Parameters:
/// - emulatorSettings: The emulator settings. The default value is `nil`, connecting the FirebaseAccount module to the Firebase Auth cloud instance.
Expand All @@ -51,10 +51,11 @@ public final class FirebaseAccountConfiguration: Component {
) {
self.emulatorSettings = emulatorSettings
self.authenticationMethods = authenticationMethods
self.accountServices = []

// TODO at least one authenticationMethod!
// if authenticationMethods.contains(.emailAndPassword)
self.accountService = FirebaseEmailPasswordAccountService()
if authenticationMethods.contains(.emailAndPassword) {
self.accountServices.append(FirebaseEmailPasswordAccountService())
}
}

public func configure() {
Expand All @@ -63,7 +64,12 @@ public final class FirebaseAccountConfiguration: Component {
}

Task {
await accountService.configure()
// We might be configured above the AccountConfiguration and therfore the `Account` object
// might not be injected yet.
try? await Task.sleep(for: .milliseconds(10))
for accountService in accountServices {
await (accountService as? FirebaseEmailPasswordAccountService)?.configure()
}
}
}
}
12 changes: 12 additions & 0 deletions Sources/SpeziFirebaseAccount/FirebaseAccountError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ enum FirebaseAccountError: LocalizedError {
case invalidCredentials
case internalPasswordResetError
case setupError
case notSignedIn
case requireRecentLogin
case unknown(AuthErrorCode.Code)


Expand All @@ -34,6 +36,10 @@ enum FirebaseAccountError: LocalizedError {
return "FIREBASE_ACCOUNT_FAILED_PASSWORD_RESET"
case .setupError:
return "FIREBASE_ACCOUNT_SETUP_ERROR"
case .notSignedIn:
return "FIREBASE_ACCOUNT_SIGN_IN_ERROR"
case .requireRecentLogin:
return "FIREBASE_ACCOUNT_REQUIRE_RECENT_LOGIN_ERROR"
case .unknown:
return "FIREBASE_ACCOUNT_UNKNOWN"
}
Expand All @@ -57,6 +63,10 @@ enum FirebaseAccountError: LocalizedError {
return "FIREBASE_ACCOUNT_FAILED_PASSWORD_RESET_SUGGESTION"
case .setupError:
return "FIREBASE_ACCOUNT_SETUP_ERROR_SUGGESTION"
case .notSignedIn:
return "FIREBASE_ACCOUNT_SIGN_IN_ERROR_SUGGESTION"
case .requireRecentLogin:
return "FIREBASE_ACCOUNT_REQUIRE_RECENT_LOGIN_ERROR_SUGGESTION"
case .unknown:
return "FIREBASE_ACCOUNT_UNKNOWN_SUGGESTION"
}
Expand All @@ -81,6 +91,8 @@ enum FirebaseAccountError: LocalizedError {
self = .internalPasswordResetError
case .operationNotAllowed, .invalidAPIKey, .appNotAuthorized, .keychainError, .internalError:
self = .setupError
case .requiresRecentLogin:
self = .requireRecentLogin
default:
self = .unknown(authErrorCode.code)
}
Expand Down
Loading

0 comments on commit 54e52f4

Please sign in to comment.