Auth+Combine.swift 25 KB

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