AuthComponent.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import FirebaseAppCheckInterop
  16. import FirebaseAuthInterop
  17. import FirebaseCore
  18. import FirebaseCoreExtension
  19. import FirebaseCoreInternal
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. @objc(FIRAuthComponent)
  22. class AuthComponent: NSObject, Library, ComponentLifecycleMaintainer {
  23. // MARK: - Private Variables
  24. /// The app associated with all Auth instances in this container.
  25. /// This is `unowned` instead of `weak` so it can be used without unwrapping in `auth()`
  26. private unowned let app: FirebaseApp
  27. /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
  28. /// containing all instances of Auth associated with the given app.
  29. private var instances: [String: Auth] = [:]
  30. /// Lock to manage access to the instances array to avoid race conditions.
  31. private let instancesLock: FirebaseCoreInternal.FIRAllocatedUnfairLock<Void> = .init()
  32. // MARK: - Initializers
  33. required init(app: FirebaseApp) {
  34. self.app = app
  35. }
  36. // MARK: - Library conformance
  37. static func componentsToRegister() -> [Component] {
  38. let authCreationBlock: ComponentCreationBlock = { container, isCacheable in
  39. guard let app = container.app else { return nil }
  40. isCacheable.pointee = true
  41. return Auth(app: app)
  42. }
  43. let authInterop = Component(AuthInterop.self,
  44. instantiationTiming: .alwaysEager,
  45. creationBlock: authCreationBlock)
  46. return [authInterop]
  47. }
  48. // MARK: - AuthProvider conformance
  49. @discardableResult func auth() -> Auth {
  50. instancesLock.lock()
  51. // Unlock before the function returns.
  52. defer { instancesLock.unlock() }
  53. if let instance = instances[app.name] {
  54. return instance
  55. }
  56. let newInstance = Auth(app: app)
  57. instances[app.name] = newInstance
  58. return newInstance
  59. }
  60. // MARK: - ComponentLifecycleMaintainer conformance
  61. func appWillBeDeleted(_ app: FirebaseApp) {
  62. kAuthGlobalWorkQueue.async {
  63. // This doesn't stop any request already issued, see b/27704535
  64. if let keychainServiceName = Auth.deleteKeychainServiceNameForAppName(app.name) {
  65. let keychain = AuthKeychainServices(service: keychainServiceName)
  66. let userKey = "\(app.name)_firebase_user"
  67. try? keychain.removeData(forKey: userKey)
  68. }
  69. DispatchQueue.main.async {
  70. // TODO(ObjC): Move over to fire an event instead, once ready.
  71. NotificationCenter.default.post(name: Auth.authStateDidChangeNotification, object: nil)
  72. }
  73. }
  74. }
  75. }