AuthComponent.swift 3.3 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 appCheckInterop = Dependency(with: AppCheckInterop.self, isRequired: false)
  43. let authInterop = Component(AuthInterop.self,
  44. instantiationTiming: .alwaysEager,
  45. dependencies: [appCheckInterop],
  46. creationBlock: authCreationBlock)
  47. return [authInterop]
  48. }
  49. // MARK: - AuthProvider conformance
  50. @discardableResult func auth() -> Auth {
  51. os_unfair_lock_lock(&instancesLock)
  52. // Unlock before the function returns.
  53. defer { os_unfair_lock_unlock(&instancesLock) }
  54. if let instance = instances[app.name] {
  55. return instance
  56. }
  57. let newInstance = FirebaseAuth.Auth(app: app)
  58. instances[app.name] = newInstance
  59. return newInstance
  60. }
  61. // MARK: - ComponentLifecycleMaintainer conformance
  62. func appWillBeDeleted(_ app: FirebaseApp) {
  63. kAuthGlobalWorkQueue.async {
  64. // This doesn't stop any request already issued, see b/27704535
  65. if let keychainServiceName = Auth.keychainServiceName(forAppName: app.name) {
  66. Auth.deleteKeychainServiceNameForAppName(app.name)
  67. let keychain = AuthKeychainServices(service: keychainServiceName)
  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. }