Auth+Combine.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 Combine
  15. import FirebaseAuth
  16. import Foundation
  17. // Make this class discoverable from Objective-C. Don't instantiate directly.
  18. @objc(FIRCombineAuthLibrary) private class __CombineAuthLibrary: NSObject {}
  19. @available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
  20. public extension Auth {
  21. // MARK: - Authentication State Management
  22. /// Registers a publisher that publishes authentication state changes.
  23. ///
  24. /// The publisher emits values when:
  25. ///
  26. /// - It is registered,
  27. /// - a user with a different UID from the current user has signed in, or
  28. /// - the current user has signed out.
  29. ///
  30. /// The publisher will emit events on the **main** thread.
  31. ///
  32. /// - Returns: A publisher emitting a `User` instance (if the user has signed in) or `nil` (if
  33. /// the user has signed out).
  34. /// The publisher will emit on the *main* thread.
  35. func authStateDidChangePublisher() -> AnyPublisher<User?, Never> {
  36. let subject = PassthroughSubject<User?, Never>()
  37. let handle = addStateDidChangeListener { auth, user in
  38. subject.send(user)
  39. }
  40. return subject
  41. .handleEvents(receiveCancel: {
  42. self.removeStateDidChangeListener(handle)
  43. })
  44. .eraseToAnyPublisher()
  45. }
  46. /// Registers a publisher that publishes ID token state changes.
  47. ///
  48. /// The publisher emits values when:
  49. ///
  50. /// - It is registered,
  51. /// - a user with a different UID from the current user has signed in,
  52. /// - the ID token of the current user has been refreshed, or
  53. /// - the current user has signed out.
  54. ///
  55. /// The publisher will emit events on the **main** thread.
  56. ///
  57. /// - Returns: A publisher emitting a `User` instance (if a different user is signed in or
  58. /// the ID token of the current user has changed) or `nil` (if the user has signed out).
  59. /// The publisher will emit on the *main* thread.
  60. func idTokenDidChangePublisher() -> AnyPublisher<User?, Never> {
  61. let subject = PassthroughSubject<User?, Never>()
  62. let handle = addIDTokenDidChangeListener { auth, user in
  63. subject.send(user)
  64. }
  65. return subject
  66. .handleEvents(receiveCancel: {
  67. self.removeIDTokenDidChangeListener(handle)
  68. })
  69. .eraseToAnyPublisher()
  70. }
  71. /// Sets the `currentUser` on the calling Auth instance to the provided `user` object.
  72. ///
  73. /// The publisher will emit events on the **main** thread.
  74. ///
  75. /// - Parameter user: The user object to be set as the current user of the calling Auth
  76. /// instance.
  77. /// - Returns: A publisher that emits when the user of the calling Auth instance has been
  78. /// updated or
  79. /// an error was encountered. The publisher will emit on the **main** thread.
  80. @discardableResult
  81. func updateCurrentUser(_ user: User) -> Future<Void, Error> {
  82. Future<Void, Error> { promise in
  83. self.updateCurrentUser(user) { error in
  84. if let error {
  85. promise(.failure(error))
  86. } else {
  87. promise(.success(()))
  88. }
  89. }
  90. }
  91. }
  92. // MARK: - Anonymous Authentication
  93. /// Asynchronously creates an anonymous user and assigns it as the calling Auth instance's
  94. /// current user.
  95. ///
  96. /// If there is already an anonymous user signed in, that user will be returned instead.
  97. /// If there is any other existing user signed in, that user will be signed out.
  98. ///
  99. /// The publisher will emit events on the **main** thread.
  100. ///
  101. /// - Returns: A publisher that emits the result of the sign in flow. The publisher will emit on
  102. /// the *main* thread.
  103. /// - Remark:
  104. /// Possible error codes:
  105. /// - `AuthErrorCodeOperationNotAllowed` - Indicates that anonymous accounts are
  106. /// not enabled. Enable them in the Auth section of the Firebase console.
  107. ///
  108. /// See `AuthErrors` for a list of error codes that are common to all API methods
  109. @discardableResult
  110. func signInAnonymously() -> Future<AuthDataResult, Error> {
  111. Future<AuthDataResult, Error> { promise in
  112. self.signInAnonymously { authDataResult, error in
  113. if let error {
  114. promise(.failure(error))
  115. } else if let authDataResult {
  116. promise(.success(authDataResult))
  117. }
  118. }
  119. }
  120. }
  121. // MARK: - Email/Password Authentication
  122. /// Creates and, on success, signs in a user with the given email address and password.
  123. ///
  124. /// The publisher will emit events on the **main** thread.
  125. ///
  126. /// - Parameters:
  127. /// - email: The user's email address.
  128. /// - password: The user's desired password.
  129. /// - Returns: A publisher that emits the result of the sign in flow. The publisher will emit on
  130. /// the *main* thread.
  131. /// - Remark:
  132. /// Possible error codes:
  133. /// - `AuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  134. /// - `AuthErrorCodeEmailAlreadyInUse` - Indicates the email used to attempt sign up
  135. /// already exists. Call fetchProvidersForEmail to check which sign-in mechanisms the user
  136. /// used, and prompt the user to sign in with one of those.
  137. /// - `AuthErrorCodeOperationNotAllowed` - Indicates that email and password accounts
  138. /// are not enabled. Enable them in the Auth section of the Firebase console.
  139. /// - `AuthErrorCodeWeakPassword` - Indicates an attempt to set a password that is
  140. /// considered too weak. The NSLocalizedFailureReasonErrorKey field in the NSError.userInfo
  141. /// dictionary object will contain more detailed explanation that can be shown to the user.
  142. ///
  143. /// See `AuthErrors` for a list of error codes that are common to all API methods
  144. @discardableResult
  145. func createUser(withEmail email: String,
  146. password: String) -> Future<AuthDataResult, Error> {
  147. Future<AuthDataResult, Error> { promise in
  148. self.createUser(withEmail: email, password: password) { authDataResult, error in
  149. if let error {
  150. promise(.failure(error))
  151. } else if let authDataResult {
  152. promise(.success(authDataResult))
  153. }
  154. }
  155. }
  156. }
  157. /// Signs in a user with the given email address and password.
  158. ///
  159. /// The publisher will emit events on the **main** thread.
  160. ///
  161. /// - Parameters:
  162. /// - email: The user's email address.
  163. /// - password: The user's password.
  164. /// - Returns: A publisher that emits the result of the sign in flow. The publisher will emit on
  165. /// the *main* thread.
  166. /// - Remark:
  167. /// Possible error codes:
  168. /// - `AuthErrorCodeOperationNotAllowed` - Indicates that email and password
  169. /// accounts are not enabled. Enable them in the Auth section of the
  170. /// Firebase console.
  171. /// - `AuthErrorCodeUserDisabled` - Indicates the user's account is disabled.
  172. /// - `AuthErrorCodeWrongPassword` - Indicates the user attempted
  173. /// sign in with an incorrect password.
  174. /// - `AuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  175. ///
  176. /// See `AuthErrors` for a list of error codes that are common to all API methods
  177. @discardableResult
  178. func signIn(withEmail email: String,
  179. password: String) -> Future<AuthDataResult, Error> {
  180. Future<AuthDataResult, Error> { promise in
  181. self.signIn(withEmail: email, password: password) { authDataResult, error in
  182. if let error {
  183. promise(.failure(error))
  184. } else if let authDataResult {
  185. promise(.success(authDataResult))
  186. }
  187. }
  188. }
  189. }
  190. // MARK: - Email/Link Authentication
  191. /// Signs in using an email address and email sign-in link.
  192. ///
  193. /// The publisher will emit events on the **main** thread.
  194. ///
  195. /// - Parameters:
  196. /// - email: The user's email address.
  197. /// - link: The email sign-in link.
  198. /// - Returns: A publisher that emits the result of the sign in flow. The publisher will emit on
  199. /// the *main* thread.
  200. /// - Remark:
  201. /// Possible error codes:
  202. /// - `AuthErrorCodeOperationNotAllowed` - Indicates that email and password
  203. /// accounts are not enabled. Enable them in the Auth section of the
  204. /// Firebase console.
  205. /// - `AuthErrorCodeUserDisabled` - Indicates the user's account is disabled.
  206. /// - `AuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  207. ///
  208. /// See `AuthErrors` for a list of error codes that are common to all API methods
  209. @available(watchOS, unavailable)
  210. @discardableResult
  211. func signIn(withEmail email: String,
  212. link: String) -> Future<AuthDataResult, Error> {
  213. Future<AuthDataResult, Error> { promise in
  214. self.signIn(withEmail: email, link: link) { authDataResult, error in
  215. if let error {
  216. promise(.failure(error))
  217. } else if let authDataResult {
  218. promise(.success(authDataResult))
  219. }
  220. }
  221. }
  222. }
  223. /// Sends a sign in with email link to provided email address.
  224. ///
  225. /// The publisher will emit events on the **main** thread.
  226. ///
  227. /// - Parameters:
  228. /// - email: The email address of the user.
  229. /// - actionCodeSettings: An `ActionCodeSettings` object containing settings related to
  230. /// handling action codes.
  231. /// - Returns: A publisher that emits whether the call was successful or not. The publisher will
  232. /// emit on the *main* thread.
  233. @available(watchOS, unavailable)
  234. @discardableResult
  235. func sendSignInLink(toEmail email: String,
  236. actionCodeSettings: ActionCodeSettings) -> Future<Void, Error> {
  237. Future<Void, Error> { promise in
  238. self.sendSignInLink(toEmail: email, actionCodeSettings: actionCodeSettings) { error in
  239. if let error {
  240. promise(.failure(error))
  241. } else {
  242. promise(.success(()))
  243. }
  244. }
  245. }
  246. }
  247. // MARK: - Email-based Authentication Helpers
  248. /// Fetches the list of all sign-in methods previously used for the provided email address.
  249. ///
  250. /// The publisher will emit events on the **main** thread.
  251. ///
  252. /// - Parameter email: The email address for which to obtain a list of sign-in methods.
  253. /// - Returns: A publisher that emits a list of sign-in methods for the specified email
  254. /// address, or an error if one occurred. The publisher will emit on the *main* thread.
  255. /// - Remark: Possible error codes:
  256. /// - `AuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  257. ///
  258. /// See `AuthErrors` for a list of error codes that are common to all API methods
  259. func fetchSignInMethods(forEmail email: String) -> Future<[String], Error> {
  260. Future<[String], Error> { promise in
  261. self.fetchSignInMethods(forEmail: email) { signInMethods, error in
  262. if let error {
  263. promise(.failure(error))
  264. } else if let signInMethods {
  265. promise(.success(signInMethods))
  266. }
  267. }
  268. }
  269. }
  270. // MARK: - Password Reset
  271. /// Resets the password given a code sent to the user outside of the app and a new password for
  272. /// the user.
  273. ///
  274. /// The publisher will emit events on the **main** thread.
  275. ///
  276. /// - Parameters:
  277. /// - code: Out-of-band (OOB) code given to the user outside of the app.
  278. /// - newPassword: The new password.
  279. /// - Returns: A publisher that emits whether the call was successful or not. The publisher will
  280. /// emit on the *main* thread.
  281. /// - Remark: Possible error codes:
  282. /// - `AuthErrorCodeWeakPassword` - Indicates an attempt to set a password that is considered
  283. /// too weak.
  284. /// - `AuthErrorCodeOperationNotAllowed` - Indicates the admin disabled sign in with the
  285. /// specified identity provider.
  286. /// - `AuthErrorCodeExpiredActionCode` - Indicates the OOB code is expired.
  287. /// - `AuthErrorCodeInvalidActionCode` - Indicates the OOB code is invalid.
  288. ///
  289. /// See `AuthErrors` for a list of error codes that are common to all API methods
  290. @discardableResult
  291. func confirmPasswordReset(withCode code: String,
  292. newPassword: String) -> Future<Void, Error> {
  293. Future<Void, Error> { promise in
  294. self.confirmPasswordReset(withCode: code, newPassword: newPassword) { error in
  295. if let error {
  296. promise(.failure(error))
  297. } else {
  298. promise(.success(()))
  299. }
  300. }
  301. }
  302. }
  303. /// Checks the validity of a verify password reset code.
  304. ///
  305. /// The publisher will emit events on the **main** thread.
  306. ///
  307. /// - Parameter code: The password reset code to be verified.
  308. /// - Returns: A publisher that emits an error if the code could not be verified. If the code
  309. /// could be
  310. /// verified, the publisher will emit the email address of the account the code was issued
  311. /// for.
  312. /// The publisher will emit on the *main* thread.
  313. @discardableResult
  314. func verifyPasswordResetCode(_ code: String) -> Future<String, Error> {
  315. Future<String, Error> { promise in
  316. self.verifyPasswordResetCode(code) { email, error in
  317. if let error {
  318. promise(.failure(error))
  319. } else if let email {
  320. promise(.success(email))
  321. }
  322. }
  323. }
  324. }
  325. /// Checks the validity of an out of band code.
  326. ///
  327. /// The publisher will emit events on the **main** thread.
  328. ///
  329. /// - Parameter code: The out of band code to check validity.
  330. /// - Returns: A publisher that emits the email address of the account the code was issued for
  331. /// or an error if
  332. /// the code could not be verified. The publisher will emit on the *main* thread.
  333. @discardableResult
  334. func checkActionCode(code: String) -> Future<ActionCodeInfo, Error> {
  335. Future<ActionCodeInfo, Error> { promise in
  336. self.checkActionCode(code) { actionCodeInfo, error in
  337. if let error {
  338. promise(.failure(error))
  339. } else if let actionCodeInfo {
  340. promise(.success(actionCodeInfo))
  341. }
  342. }
  343. }
  344. }
  345. /// Applies out of band code.
  346. ///
  347. /// The publisher will emit events on the **main** thread.
  348. ///
  349. /// - Parameter code: The out-of-band (OOB) code to be applied.
  350. /// - Returns: A publisher that emits an error if the code could not be applied. The publisher
  351. /// will emit on the *main* thread.
  352. /// - Remark: This method will not work for out-of-band codes which require an additional
  353. /// parameter,
  354. /// such as password reset codes.
  355. @discardableResult
  356. func applyActionCode(code: String) -> Future<Void, Error> {
  357. Future<Void, Error> { promise in
  358. self.applyActionCode(code) { error in
  359. if let error {
  360. promise(.failure(error))
  361. } else {
  362. promise(.success(()))
  363. }
  364. }
  365. }
  366. }
  367. /// Initiates a password reset for the given email address.
  368. ///
  369. /// The publisher will emit events on the **main** thread.
  370. ///
  371. /// - Parameter email: The email address of the user.
  372. /// - Returns: A publisher that emits whether the call was successful or not. The publisher will
  373. /// emit on the *main* thread.
  374. /// - Remark: Possible error codes:
  375. /// - `AuthErrorCodeInvalidRecipientEmail` - Indicates an invalid recipient email was sent in
  376. /// the request.
  377. /// - `AuthErrorCodeInvalidSender` - Indicates an invalid sender email is set in the console
  378. /// for this action.
  379. /// - `AuthErrorCodeInvalidMessagePayload` - Indicates an invalid email template for sending
  380. /// update email.
  381. ///
  382. /// See `AuthErrors` for a list of error codes that are common to all API methods
  383. @discardableResult
  384. func sendPasswordReset(withEmail email: String) -> Future<Void, Error> {
  385. Future<Void, Error> { promise in
  386. self.sendPasswordReset(withEmail: email) { error in
  387. if let error {
  388. promise(.failure(error))
  389. } else {
  390. promise(.success(()))
  391. }
  392. }
  393. }
  394. }
  395. /// Initiates a password reset for the given email address and `ActionCodeSettings`.
  396. ///
  397. /// The publisher will emit events on the **main** thread.
  398. ///
  399. /// - Parameter email: The email address of the user.
  400. /// - Parameter actionCodeSettings: An `ActionCodeSettings` object containing settings related
  401. /// to
  402. /// handling action codes.
  403. /// - Returns: A publisher that emits whether the call was successful or not. The publisher will
  404. /// emit on the *main* thread.
  405. /// - Remark: Possible error codes:
  406. /// - `AuthErrorCodeInvalidRecipientEmail` - Indicates an invalid recipient email was sent in
  407. /// the request.
  408. /// - `AuthErrorCodeInvalidSender` - Indicates an invalid sender email is set in the console
  409. /// for this action.
  410. /// - `AuthErrorCodeInvalidMessagePayload` - Indicates an invalid email template for sending
  411. /// update email.
  412. /// - `AuthErrorCodeMissingIosBundleID` - Indicates that the iOS bundle ID is missing
  413. /// when `handleCodeInApp` is set to YES.
  414. /// - `AuthErrorCodeMissingAndroidPackageName` - Indicates that the android package name is
  415. /// missing
  416. /// when the `androidInstallApp` flag is set to true.
  417. /// - `AuthErrorCodeUnauthorizedDomain` - Indicates that the domain specified in the continue
  418. /// URL is not whitelisted
  419. /// in the Firebase console.
  420. /// - `AuthErrorCodeInvalidContinueURI` - Indicates that the domain specified in the continue
  421. /// URI is not valid.
  422. ///
  423. /// See `AuthErrors` for a list of error codes that are common to all API methods
  424. @discardableResult
  425. func sendPasswordReset(withEmail email: String,
  426. actionCodeSettings: ActionCodeSettings) -> Future<Void, Error> {
  427. Future<Void, Error> { promise in
  428. self.sendPasswordReset(withEmail: email, actionCodeSettings: actionCodeSettings) { error in
  429. if let error {
  430. promise(.failure(error))
  431. } else {
  432. promise(.success(()))
  433. }
  434. }
  435. }
  436. }
  437. // MARK: - Other Authentication providers
  438. #if os(iOS) || targetEnvironment(macCatalyst)
  439. /// Signs in using the provided auth provider instance.
  440. ///
  441. /// The publisher will emit events on the **main** thread.
  442. ///
  443. /// - Parameters:
  444. /// - provider: An instance of an auth provider used to initiate the sign-in flow.
  445. /// - uiDelegate: Optionally, an instance of a class conforming to the `AuthUIDelegate`
  446. /// protocol. This is used for presenting the web context. If `nil`, a default
  447. /// `AuthUIDelegate`
  448. /// will be used.
  449. /// - Returns: A publisher that emits an `AuthDataResult` when the sign-in flow completed
  450. /// successfully, or an error otherwise. The publisher will emit on the *main* thread.
  451. /// - Remark: Possible error codes:
  452. /// - `AuthErrorCodeOperationNotAllowed` - Indicates that email and password accounts are
  453. /// not enabled.
  454. /// Enable them in the Auth section of the Firebase console.
  455. /// - `AuthErrorCodeUserDisabled` - Indicates the user's account is disabled.
  456. /// - `AuthErrorCodeWebNetworkRequestFailed` - Indicates that a network request within a
  457. /// `SFSafariViewController` or `WKWebView` failed.
  458. /// - `AuthErrorCodeWebInternalError` - Indicates that an internal error occurred within a
  459. /// `SFSafariViewController` or `WKWebView`.`
  460. /// - `AuthErrorCodeWebSignInUserInteractionFailure` - Indicates a general failure during a
  461. /// web sign-in flow.`
  462. /// - `AuthErrorCodeWebContextAlreadyPresented` - Indicates that an attempt was made to
  463. /// present a new web
  464. /// context while one was already being presented.`
  465. /// - `AuthErrorCodeWebContextCancelled` - Indicates that the URL presentation was cancelled
  466. /// prematurely
  467. /// by the user.`
  468. /// - `AuthErrorCodeAccountExistsWithDifferentCredential` - Indicates the email asserted by
  469. /// the credential
  470. /// (e.g. the email in a Facebook access token) is already in use by an existing account
  471. /// that cannot be
  472. /// authenticated with this sign-in method. Call `fetchProvidersForEmail` for this user’s
  473. /// email and then
  474. /// prompt them to sign in with any of the sign-in providers returned. This error will
  475. /// only be thrown if
  476. /// the "One account per email address" setting is enabled in the Firebase console, under
  477. /// Auth settings.
  478. ///
  479. /// See `AuthErrors` for a list of error codes that are common to all API methods
  480. @discardableResult
  481. func signIn(with provider: FederatedAuthProvider,
  482. uiDelegate: AuthUIDelegate?) -> Future<AuthDataResult, Error> {
  483. Future<AuthDataResult, Error> { promise in
  484. self.signIn(with: provider, uiDelegate: uiDelegate) { authDataResult, error in
  485. if let error {
  486. promise(.failure(error))
  487. } else if let authDataResult {
  488. promise(.success(authDataResult))
  489. }
  490. }
  491. }
  492. }
  493. #endif // os(iOS) || targetEnvironment(macCatalyst)
  494. /// Asynchronously signs in to Firebase with the given Auth token.
  495. ///
  496. /// The publisher will emit events on the **main** thread.
  497. ///
  498. /// - Parameter token: A self-signed custom auth token.
  499. /// - Returns: A publisher that emits an `AuthDataResult` when the sign-in flow completed
  500. /// successfully, or an error otherwise. The publisher will emit on the *main* thread.
  501. /// - Remark: Possible error codes:
  502. /// - `AuthErrorCodeInvalidCustomToken` - Indicates a validation error with the custom token.
  503. /// - `AuthErrorCodeUserDisabled` - Indicates the user's account is disabled.
  504. /// - `AuthErrorCodeCustomTokenMismatch` - Indicates the service account and the API key
  505. /// belong to different projects.
  506. ///
  507. /// See `AuthErrors` for a list of error codes that are common to all API methods
  508. @discardableResult
  509. func signIn(withCustomToken token: String) -> Future<AuthDataResult, Error> {
  510. Future<AuthDataResult, Error> { promise in
  511. self.signIn(withCustomToken: token) { authDataResult, error in
  512. if let error {
  513. promise(.failure(error))
  514. } else if let authDataResult {
  515. promise(.success(authDataResult))
  516. }
  517. }
  518. }
  519. }
  520. /// Asynchronously signs in to Firebase with the given 3rd-party credentials (e.g. a Facebook
  521. /// login Access Token, a Google ID Token/Access Token pair, etc.) and returns additional
  522. /// identity provider data.
  523. ///
  524. /// The publisher will emit events on the **main** thread.
  525. ///
  526. /// - Parameter credential: The credential supplied by the IdP.
  527. /// - Returns: A publisher that emits an `AuthDataResult` when the sign-in flow completed
  528. /// successfully, or an error otherwise. The publisher will emit on the *main* thread.
  529. /// - Remark: Possible error codes:
  530. /// - `FIRAuthErrorCodeInvalidCredential` - Indicates the supplied credential is invalid.
  531. /// This could happen if it has expired or it is malformed.
  532. /// - `FIRAuthErrorCodeOperationNotAllowed` - Indicates that accounts
  533. /// with the identity provider represented by the credential are not enabled.
  534. /// Enable them in the Auth section of the Firebase console.
  535. /// - `FIRAuthErrorCodeAccountExistsWithDifferentCredential` - Indicates the email asserted
  536. /// by the credential (e.g. the email in a Facebook access token) is already in use by an
  537. /// existing account, that cannot be authenticated with this sign-in method. Call
  538. /// fetchProvidersForEmail for this user’s email and then prompt them to sign in with any of
  539. /// the sign-in providers returned. This error will only be thrown if the "One account per
  540. /// email address" setting is enabled in the Firebase console, under Auth settings.
  541. /// - `FIRAuthErrorCodeUserDisabled` - Indicates the user's account is disabled.
  542. /// - `FIRAuthErrorCodeWrongPassword` - Indicates the user attempted sign in with an
  543. /// incorrect password, if credential is of the type EmailPasswordAuthCredential.
  544. /// - `FIRAuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  545. /// - `FIRAuthErrorCodeMissingVerificationID` - Indicates that the phone auth credential was
  546. /// created with an empty verification ID.
  547. /// - `FIRAuthErrorCodeMissingVerificationCode` - Indicates that the phone auth credential
  548. /// was created with an empty verification code.
  549. /// - `FIRAuthErrorCodeInvalidVerificationCode` - Indicates that the phone auth credential
  550. /// was created with an invalid verification Code.
  551. /// - `FIRAuthErrorCodeInvalidVerificationID` - Indicates that the phone auth credential was
  552. /// created with an invalid verification ID.
  553. /// - `FIRAuthErrorCodeSessionExpired` - Indicates that the SMS code has expired.
  554. ///
  555. /// See `AuthErrors` for a list of error codes that are common to all API methods
  556. @discardableResult
  557. func signIn(with credential: AuthCredential) -> Future<AuthDataResult, Error> {
  558. Future<AuthDataResult, Error> { promise in
  559. self.signIn(with: credential) { authDataResult, error in
  560. if let error {
  561. promise(.failure(error))
  562. } else if let authDataResult {
  563. promise(.success(authDataResult))
  564. }
  565. }
  566. }
  567. }
  568. }