AuthMenu.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2020 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 UIKit
  15. /// Firebase Auth supported identity providers and other methods of authentication
  16. enum AuthMenu: String {
  17. case settings = "Settings"
  18. case google = "google.com"
  19. case apple = "apple.com"
  20. case twitter = "twitter.com"
  21. case microsoft = "microsoft.com"
  22. case gitHub = "github.com"
  23. case yahoo = "yahoo.com"
  24. case facebook = "facebook.com"
  25. case emailPassword = "password"
  26. case passwordless = "emailLink"
  27. case phoneNumber = "phone"
  28. case anonymous
  29. case custom
  30. case initRecaptcha
  31. case customAuthDomain
  32. /// More intuitively named getter for `rawValue`.
  33. var id: String { rawValue }
  34. /// The UI friendly name of the `AuthMenu`. Used for display.
  35. var name: String {
  36. switch self {
  37. case .settings:
  38. return "Settings"
  39. case .google:
  40. return "Google"
  41. case .apple:
  42. return "Apple"
  43. case .twitter:
  44. return "Twitter"
  45. case .microsoft:
  46. return "Microsoft"
  47. case .gitHub:
  48. return "GitHub"
  49. case .yahoo:
  50. return "Yahoo"
  51. case .facebook:
  52. return "Facebook"
  53. case .emailPassword:
  54. return "Email & Password Login"
  55. case .passwordless:
  56. return "Email Link/Passwordless"
  57. case .phoneNumber:
  58. return "Phone Number"
  59. case .anonymous:
  60. return "Anonymous Authentication"
  61. case .custom:
  62. return "Custom Auth System"
  63. case .initRecaptcha:
  64. return "Initialize reCAPTCHA Enterprise"
  65. case .customAuthDomain:
  66. return "Set Custom Auth Domain"
  67. }
  68. }
  69. /// Failable initializer to create an `AuthMenu` from it's corresponding `name` value.
  70. /// - Parameter rawValue: String value representing `AuthMenu`'s name or type.
  71. init?(rawValue: String) {
  72. switch rawValue {
  73. case "Settings":
  74. self = .settings
  75. case "Google":
  76. self = .google
  77. case "Apple":
  78. self = .apple
  79. case "Twitter":
  80. self = .twitter
  81. case "Microsoft":
  82. self = .microsoft
  83. case "GitHub":
  84. self = .gitHub
  85. case "Yahoo":
  86. self = .yahoo
  87. case "Facebook":
  88. self = .facebook
  89. case "Email & Password Login":
  90. self = .emailPassword
  91. case "Email Link/Passwordless":
  92. self = .passwordless
  93. case "Phone Number":
  94. self = .phoneNumber
  95. case "Anonymous Authentication":
  96. self = .anonymous
  97. case "Custom Auth System":
  98. self = .custom
  99. case "Initialize reCAPTCHA Enterprise":
  100. self = .initRecaptcha
  101. case "Set Custom Auth Domain":
  102. self = .customAuthDomain
  103. default: return nil
  104. }
  105. }
  106. }
  107. // MARK: DataSourceProvidable
  108. extension AuthMenu: DataSourceProvidable {
  109. private static var providers: [AuthMenu] {
  110. [.google, .apple, .twitter, .microsoft, .gitHub, .yahoo, .facebook]
  111. }
  112. static var settingsSection: Section {
  113. let header = "Auth Settings"
  114. let item = Item(title: settings.name, hasNestedContent: true)
  115. return Section(headerDescription: header, items: [item])
  116. }
  117. static var providerSection: Section {
  118. let providers = self.providers.map { Item(title: $0.name) }
  119. let header = "Identity Providers"
  120. let footer = "Choose a login flow from one of the identity providers above."
  121. return Section(headerDescription: header, footerDescription: footer, items: providers)
  122. }
  123. static var emailPasswordSection: Section {
  124. let image = UIImage(named: "firebaseIcon")
  125. let header = "Email and Password Login"
  126. let item = Item(title: emailPassword.name, hasNestedContent: true, image: image)
  127. return Section(headerDescription: header, items: [item])
  128. }
  129. static var otherSection: Section {
  130. let lockSymbol = UIImage.systemImage("lock.slash.fill", tintColor: .systemOrange)
  131. let phoneSymbol = UIImage.systemImage("phone.fill", tintColor: .systemOrange)
  132. let anonSymbol = UIImage.systemImage("questionmark.circle.fill", tintColor: .systemOrange)
  133. let shieldSymbol = UIImage.systemImage("lock.shield.fill", tintColor: .systemOrange)
  134. let otherOptions = [
  135. Item(title: passwordless.name, image: lockSymbol),
  136. Item(title: phoneNumber.name, image: phoneSymbol),
  137. Item(title: anonymous.name, image: anonSymbol),
  138. Item(title: custom.name, image: shieldSymbol),
  139. ]
  140. let header = "Other Authentication Methods"
  141. return Section(headerDescription: header, items: otherOptions)
  142. }
  143. static var recaptchaSection: Section {
  144. let image = UIImage(named: "firebaseIcon")
  145. let header = "Initialize reCAPTCHA Enterprise"
  146. let item = Item(title: initRecaptcha.name, hasNestedContent: false, image: image)
  147. return Section(headerDescription: header, items: [item])
  148. }
  149. static var customAuthDomainSection: Section {
  150. let image = UIImage(named: "firebaseIcon")
  151. let header = "Custom Auth Domain"
  152. let item = Item(title: customAuthDomain.name, hasNestedContent: false, image: image)
  153. return Section(headerDescription: header, items: [item])
  154. }
  155. static var sections: [Section] {
  156. [settingsSection, providerSection, emailPasswordSection, otherSection, recaptchaSection,
  157. customAuthDomainSection]
  158. }
  159. static var authLinkSections: [Section] {
  160. let allItems = AuthMenu.sections.flatMap { $0.items }
  161. let header = "Manage linking between providers"
  162. let footer =
  163. "Select an unchecked row to link the currently signed in user to that auth provider. To unlink the user from a linked provider, select its corresponding row marked with a checkmark."
  164. return [Section(headerDescription: header, footerDescription: footer, items: allItems)]
  165. }
  166. var sections: [Section] { AuthMenu.sections }
  167. }