Auth+Combine.swift 26 KB

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