AuthComponent.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  20. @objc(FIRAuthComponent)
  21. class AuthComponent: NSObject, Library, ComponentLifecycleMaintainer {
  22. // MARK: - Private Variables
  23. /// The app associated with all Auth instances in this container.
  24. /// This is `unowned` instead of `weak` so it can be used without unwrapping in `auth()`
  25. private unowned let app: FirebaseApp
  26. /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
  27. /// containing all instances of Auth associated with the given app.
  28. private var instances: [String: Auth] = [:]
  29. /// Lock to manage access to the instances array to avoid race conditions.
  30. private var instancesLock: os_unfair_lock = .init()
  31. // MARK: - Initializers
  32. required init(app: FirebaseApp) {
  33. self.app = app
  34. }
  35. // MARK: - Library conformance
  36. static func componentsToRegister() -> [Component] {
  37. let authCreationBlock: ComponentCreationBlock = { container, isCacheable in
  38. guard let app = container.app else { return nil }
  39. isCacheable.pointee = true
  40. return Auth(app: app)
  41. }
  42. let authInterop = Component(AuthInterop.self,
  43. instantiationTiming: .alwaysEager,
  44. creationBlock: authCreationBlock)
  45. return [authInterop]
  46. }
  47. // MARK: - AuthProvider conformance
  48. @discardableResult func auth() -> Auth {
  49. os_unfair_lock_lock(&instancesLock)
  50. // Unlock before the function returns.
  51. defer { os_unfair_lock_unlock(&instancesLock) }
  52. if let instance = instances[app.name] {
  53. return instance
  54. }
  55. let newInstance = Auth(app: app)
  56. instances[app.name] = newInstance
  57. return newInstance
  58. }
  59. // MARK: - ComponentLifecycleMaintainer conformance
  60. func appWillBeDeleted(_ app: FirebaseApp) {
  61. kAuthGlobalWorkQueue.async {
  62. // This doesn't stop any request already issued, see b/27704535
  63. if let keychainServiceName = Auth.deleteKeychainServiceNameForAppName(app.name) {
  64. let keychain = AuthKeychainServices(
  65. service: keychainServiceName,
  66. storage: AuthKeychainStorageReal.shared
  67. )
  68. let userKey = "\(app.name)_firebase_user"
  69. try? keychain.removeData(forKey: userKey)
  70. }
  71. DispatchQueue.main.async {
  72. // TODO(ObjC): Move over to fire an event instead, once ready.
  73. NotificationCenter.default.post(name: Auth.authStateDidChangeNotification, object: nil)
  74. }
  75. }
  76. }
  77. }