FIRAuth.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <AvailabilityMacros.h>
  17. #import <Foundation/Foundation.h>
  18. #import "FIRAuthAPNSTokenType.h"
  19. #import "FIRAuthErrors.h"
  20. @class FIRActionCodeSettings;
  21. @class FIRApp;
  22. @class FIRAuth;
  23. @class FIRAuthCredential;
  24. @class FIRAuthDataResult;
  25. @class FIRAuthSettings;
  26. @class FIRUser;
  27. @protocol FIRAuthUIDelegate;
  28. @protocol FIRFederatedAuthProvider;
  29. NS_ASSUME_NONNULL_BEGIN
  30. /** @typedef FIRUserUpdateCallback
  31. @brief The type of block invoked when a request to update the current user is completed.
  32. */
  33. typedef void (^FIRUserUpdateCallback)(NSError *_Nullable error)
  34. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  35. /** @typedef FIRAuthStateDidChangeListenerHandle
  36. @brief The type of handle returned by `Auth.addAuthStateDidChangeListener(_:)`.
  37. */
  38. // clang-format off
  39. // clang-format12 merges the next two lines.
  40. typedef id<NSObject> FIRAuthStateDidChangeListenerHandle
  41. NS_SWIFT_NAME(AuthStateDidChangeListenerHandle);
  42. // clang-format on
  43. /** @typedef FIRAuthStateDidChangeListenerBlock
  44. @brief The type of block which can be registered as a listener for auth state did change events.
  45. @param auth The Auth object on which state changes occurred.
  46. @param user Optionally; the current signed in user, if any.
  47. */
  48. typedef void (^FIRAuthStateDidChangeListenerBlock)(FIRAuth *auth, FIRUser *_Nullable user)
  49. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  50. /** @typedef FIRIDTokenDidChangeListenerHandle
  51. @brief The type of handle returned by `Auth.addIDTokenDidChangeListener(_:)`.
  52. */
  53. // clang-format off
  54. // clang-format12 merges the next two lines.
  55. typedef id<NSObject> FIRIDTokenDidChangeListenerHandle
  56. NS_SWIFT_NAME(IDTokenDidChangeListenerHandle);
  57. // clang-format on
  58. /** @typedef FIRIDTokenDidChangeListenerBlock
  59. @brief The type of block which can be registered as a listener for ID token did change events.
  60. @param auth The Auth object on which ID token changes occurred.
  61. @param user Optionally; the current signed in user, if any.
  62. */
  63. typedef void (^FIRIDTokenDidChangeListenerBlock)(FIRAuth *auth, FIRUser *_Nullable user)
  64. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  65. /** @typedef FIRAuthDataResultCallback
  66. @brief The type of block invoked when sign-in related events complete.
  67. @param authResult Optionally; Result of sign-in request containing both the user and
  68. the additional user info associated with the user.
  69. @param error Optionally; the error which occurred - or nil if the request was successful.
  70. */
  71. typedef void (^FIRAuthDataResultCallback)(FIRAuthDataResult *_Nullable authResult,
  72. NSError *_Nullable error)
  73. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  74. /**
  75. @brief The name of the `NSNotificationCenter` notification which is posted when the auth state
  76. changes (for example, a new token has been produced, a user signs in or signs out). The
  77. object parameter of the notification is the sender `Auth` instance.
  78. */
  79. extern const NSNotificationName FIRAuthStateDidChangeNotification NS_SWIFT_NAME(AuthStateDidChange);
  80. /** @typedef FIRAuthResultCallback
  81. @brief The type of block invoked when sign-in related events complete.
  82. @param user Optionally; the signed in user, if any.
  83. @param error Optionally; if an error occurs, this is the NSError object that describes the
  84. problem. Set to nil otherwise.
  85. */
  86. typedef void (^FIRAuthResultCallback)(FIRUser *_Nullable user, NSError *_Nullable error)
  87. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  88. /** @typedef FIRProviderQueryCallback
  89. @brief The type of block invoked when a list of identity providers for a given email address is
  90. requested.
  91. @param providers Optionally; a list of provider identifiers, if any.
  92. @see GoogleAuthProviderID etc.
  93. @param error Optionally; if an error occurs, this is the NSError object that describes the
  94. problem. Set to nil otherwise.
  95. */
  96. typedef void (^FIRProviderQueryCallback)(NSArray<NSString *> *_Nullable providers,
  97. NSError *_Nullable error)
  98. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  99. /** @typedef FIRSignInMethodQueryCallback
  100. @brief The type of block invoked when a list of sign-in methods for a given email address is
  101. requested.
  102. */
  103. typedef void (^FIRSignInMethodQueryCallback)(NSArray<NSString *> *_Nullable, NSError *_Nullable)
  104. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  105. /** @typedef FIRSendPasswordResetCallback
  106. @brief The type of block invoked when sending a password reset email.
  107. @param error Optionally; if an error occurs, this is the NSError object that describes the
  108. problem. Set to nil otherwise.
  109. */
  110. typedef void (^FIRSendPasswordResetCallback)(NSError *_Nullable error)
  111. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  112. /** @typedef FIRSendSignInLinkToEmailCallback
  113. @brief The type of block invoked when sending an email sign-in link email.
  114. */
  115. typedef void (^FIRSendSignInLinkToEmailCallback)(NSError *_Nullable error)
  116. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  117. /** @typedef FIRConfirmPasswordResetCallback
  118. @brief The type of block invoked when performing a password reset.
  119. @param error Optionally; if an error occurs, this is the NSError object that describes the
  120. problem. Set to nil otherwise.
  121. */
  122. typedef void (^FIRConfirmPasswordResetCallback)(NSError *_Nullable error)
  123. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  124. /** @typedef FIRVerifyPasswordResetCodeCallback
  125. @brief The type of block invoked when verifying that an out of band code should be used to
  126. perform password reset.
  127. @param email Optionally; the email address of the user for which the out of band code applies.
  128. @param error Optionally; if an error occurs, this is the NSError object that describes the
  129. problem. Set to nil otherwise.
  130. */
  131. typedef void (^FIRVerifyPasswordResetCodeCallback)(NSString *_Nullable email,
  132. NSError *_Nullable error)
  133. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  134. /** @typedef FIRApplyActionCodeCallback
  135. @brief The type of block invoked when applying an action code.
  136. @param error Optionally; if an error occurs, this is the NSError object that describes the
  137. problem. Set to nil otherwise.
  138. */
  139. typedef void (^FIRApplyActionCodeCallback)(NSError *_Nullable error)
  140. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  141. typedef void (^FIRAuthVoidErrorCallback)(NSError *_Nullable)
  142. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  143. /** @class FIRActionCodeInfo
  144. @brief Manages information regarding action codes.
  145. */
  146. NS_SWIFT_NAME(ActionCodeInfo)
  147. @interface FIRActionCodeInfo : NSObject
  148. /**
  149. @brief Operations which can be performed with action codes.
  150. */
  151. typedef NS_ENUM(NSInteger, FIRActionCodeOperation) {
  152. /** Action code for unknown operation. */
  153. FIRActionCodeOperationUnknown = 0,
  154. /** Action code for password reset operation. */
  155. FIRActionCodeOperationPasswordReset = 1,
  156. /** Action code for verify email operation. */
  157. FIRActionCodeOperationVerifyEmail = 2,
  158. /** Action code for recover email operation. */
  159. FIRActionCodeOperationRecoverEmail = 3,
  160. /** Action code for email link operation. */
  161. FIRActionCodeOperationEmailLink = 4,
  162. /** Action code for verifying and changing email */
  163. FIRActionCodeOperationVerifyAndChangeEmail = 5,
  164. /** Action code for reverting second factor addition */
  165. FIRActionCodeOperationRevertSecondFactorAddition = 6,
  166. } NS_SWIFT_NAME(ActionCodeOperation);
  167. /**
  168. @brief The operation being performed.
  169. */
  170. @property(nonatomic, readonly) FIRActionCodeOperation operation;
  171. /** @property email
  172. @brief The email address to which the code was sent. The new email address in the case of
  173. `ActionCodeOperationRecoverEmail`.
  174. */
  175. @property(nonatomic, nullable, readonly, copy) NSString *email;
  176. /** @property previousEmail
  177. @brief The email that is being recovered in the case of `ActionCodeOperationRecoverEmail`.
  178. */
  179. @property(nonatomic, nullable, readonly, copy) NSString *previousEmail;
  180. /** @fn init
  181. @brief please use initWithOperation: instead.
  182. */
  183. - (instancetype)init NS_UNAVAILABLE;
  184. @end
  185. /** @class FIRActionCodeURL
  186. @brief This class will allow developers to easily extract information about out of band links.
  187. */
  188. NS_SWIFT_NAME(ActionCodeURL)
  189. @interface FIRActionCodeURL : NSObject
  190. /** @property APIKey
  191. @brief Returns the API key from the link. nil, if not provided.
  192. */
  193. @property(nonatomic, nullable, copy, readonly) NSString *APIKey;
  194. /** @property operation
  195. @brief Returns the mode of oob action. The property will be of `FIRActionCodeOperation` type.
  196. It will return `FIRActionCodeOperationUnknown` if no oob action is provided.
  197. */
  198. @property(nonatomic, readonly) FIRActionCodeOperation operation;
  199. /** @property code
  200. @brief Returns the email action code from the link. nil, if not provided.
  201. */
  202. @property(nonatomic, nullable, copy, readonly) NSString *code;
  203. /** @property continueURL
  204. @brief Returns the continue URL from the link. nil, if not provided.
  205. */
  206. @property(nonatomic, nullable, copy, readonly) NSURL *continueURL;
  207. /** @property languageCode
  208. @brief Returns the language code from the link. nil, if not provided.
  209. */
  210. @property(nonatomic, nullable, copy, readonly) NSString *languageCode;
  211. /** @fn actionCodeURLWithLink:
  212. @brief Construct an `ActionCodeURL` from an out of band link (e.g. email link).
  213. @param link The oob link string used to construct the action code URL.
  214. @return The `ActionCodeURL` object constructed based on the oob link provided.
  215. */
  216. + (nullable instancetype)actionCodeURLWithLink:(NSString *)link;
  217. /** @fn init
  218. @brief Please use `init(link:)` in Swift or `actionCodeURLWithLink:` in Objective-C
  219. instead.
  220. */
  221. - (instancetype)init NS_UNAVAILABLE;
  222. @end
  223. /** @typedef FIRCheckActionCodeCallBack
  224. @brief The type of block invoked when performing a check action code operation.
  225. @param info Metadata corresponding to the action code.
  226. @param error Optionally; if an error occurs, this is the NSError object that describes the
  227. problem. Set to nil otherwise.
  228. */
  229. typedef void (^FIRCheckActionCodeCallBack)(FIRActionCodeInfo *_Nullable info,
  230. NSError *_Nullable error)
  231. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  232. /** @class FIRAuth
  233. @brief Manages authentication for Firebase apps.
  234. @remarks This class is thread-safe.
  235. */
  236. NS_SWIFT_NAME(Auth)
  237. @interface FIRAuth : NSObject
  238. /** @fn auth
  239. @brief Gets the auth object for the default Firebase app.
  240. @remarks The default Firebase app must have already been configured or an exception will be
  241. raised.
  242. */
  243. + (FIRAuth *)auth NS_SWIFT_NAME(auth());
  244. /** @fn authWithApp:
  245. @brief Gets the auth object for a `FirebaseApp`.
  246. @param app The app for which to retrieve the associated `Auth` instance.
  247. @return The `Auth` instance associated with the given app.
  248. */
  249. + (FIRAuth *)authWithApp:(FIRApp *)app NS_SWIFT_NAME(auth(app:));
  250. /** @property app
  251. @brief Gets the `FirebaseApp` object that this auth object is connected to.
  252. */
  253. @property(nonatomic, weak, readonly, nullable) FIRApp *app;
  254. /** @property currentUser
  255. @brief Synchronously gets the cached current user, or null if there is none.
  256. */
  257. @property(nonatomic, strong, readonly, nullable) FIRUser *currentUser;
  258. /** @property languageCode
  259. @brief The current user language code. This property can be set to the app's current language by
  260. calling `useAppLanguage()`.
  261. @remarks The string used to set this property must be a language code that follows BCP 47.
  262. */
  263. @property(nonatomic, copy, nullable) NSString *languageCode;
  264. /** @property settings
  265. @brief Contains settings related to the auth object.
  266. */
  267. @property(nonatomic, copy, nullable) FIRAuthSettings *settings;
  268. /** @property userAccessGroup
  269. @brief The current user access group that the Auth instance is using. Default is nil.
  270. */
  271. @property(readonly, nonatomic, copy, nullable) NSString *userAccessGroup;
  272. /** @property shareAuthStateAcrossDevices
  273. @brief Contains shareAuthStateAcrossDevices setting related to the auth object.
  274. @remarks If userAccessGroup is not set, setting shareAuthStateAcrossDevices will
  275. have no effect. You should set shareAuthStateAcrossDevices to it's desired
  276. state and then set the userAccessGroup after.
  277. */
  278. @property(nonatomic) BOOL shareAuthStateAcrossDevices;
  279. /** @property tenantID
  280. @brief The tenant ID of the auth instance. nil if none is available.
  281. */
  282. @property(nonatomic, copy, nullable) NSString *tenantID;
  283. /** @property APNSToken
  284. @brief The APNs token used for phone number authentication. The type of the token (production
  285. or sandbox) will be automatically detected based on your provisioning profile.
  286. This property is available on iOS only.
  287. @remarks If swizzling is disabled, the APNs Token must be set for phone number auth to work,
  288. by either setting this property or by calling `setAPNSToken(_:type:)`.
  289. */
  290. @property(nonatomic, strong, nullable) NSData *APNSToken API_UNAVAILABLE(macos, tvos, watchos);
  291. /**
  292. * @property customAuthDomain
  293. * @brief The custom authentication domain used to handle all sign-in redirects. End-users will see
  294. * this domain when signing in. This domain must be allowlisted in the Firebase Console.
  295. */
  296. @property(nonatomic, copy, nullable) NSString *customAuthDomain;
  297. /** @fn init
  298. @brief Please access auth instances using `Auth.auth()` and `Auth.auth(app:)`.
  299. */
  300. - (instancetype)init NS_UNAVAILABLE;
  301. /** @fn updateCurrentUser:completion:
  302. @brief Sets the `currentUser` on the receiver to the provided user object.
  303. @param user The user object to be set as the current user of the calling Auth instance.
  304. @param completion Optionally; a block invoked after the user of the calling Auth instance has
  305. been updated or an error was encountered.
  306. */
  307. - (void)updateCurrentUser:(FIRUser *)user
  308. completion:(nullable void (^)(NSError *_Nullable error))completion;
  309. /** @fn fetchSignInMethodsForEmail:completion:
  310. @brief [Deprecated] Fetches the list of all sign-in methods previously used for the provided
  311. email address. This method returns an empty list when [Email Enumeration
  312. Protection](https://cloud.google.com/identity-platform/docs/admin/email-enumeration-protection)
  313. is enabled, irrespective of the number of authentication methods available for the given email.
  314. @param email The email address for which to obtain a list of sign-in methods.
  315. @param completion Optionally; a block which is invoked when the list of sign in methods for the
  316. specified email address is ready or an error was encountered. Invoked asynchronously on the
  317. main thread in the future.
  318. @remarks Possible error codes:
  319. + `AuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  320. @remarks See @c AuthErrors for a list of error codes that are common to all API methods.
  321. */
  322. - (void)fetchSignInMethodsForEmail:(NSString *)email
  323. completion:(nullable void (^)(NSArray<NSString *> *_Nullable,
  324. NSError *_Nullable))completion
  325. DEPRECATED_MSG_ATTRIBUTE(
  326. "This method is deprecated and will be removed in a future release. This method returns an "
  327. "empty list when Email Enumeration Protection is enabled.");
  328. /** @fn signInWithEmail:password:completion:
  329. @brief Signs in using an email address and password. When [Email Enumeration
  330. Protection](https://cloud.google.com/identity-platform/docs/admin/email-enumeration-protection)
  331. is enabled, this method fails with FIRAuthErrorCodeInvalidCredentials in case of an invalid
  332. email/password.
  333. @param email The user's email address.
  334. @param password The user's password.
  335. @param completion Optionally; a block which is invoked when the sign in flow finishes, or is
  336. canceled. Invoked asynchronously on the main thread in the future.
  337. @remarks Possible error codes:
  338. + `AuthErrorCodeOperationNotAllowed` - Indicates that email and password
  339. accounts are not enabled. Enable them in the Auth section of the
  340. Firebase console.
  341. + `AuthErrorCodeUserDisabled` - Indicates the user's account is disabled.
  342. + `AuthErrorCodeWrongPassword` - Indicates the user attempted
  343. sign in with an incorrect password.
  344. + `AuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  345. @remarks See `AuthErrors` for a list of error codes that are common to all API methods.
  346. */
  347. - (void)signInWithEmail:(NSString *)email
  348. password:(NSString *)password
  349. completion:(nullable void (^)(FIRAuthDataResult *_Nullable authResult,
  350. NSError *_Nullable error))completion;
  351. /** @fn signInWithEmail:link:completion:
  352. @brief Signs in using an email address and email sign-in link.
  353. @param email The user's email address.
  354. @param link The email sign-in link.
  355. @param completion Optionally; a block which is invoked when the sign in flow finishes, or is
  356. canceled. Invoked asynchronously on the main thread in the future.
  357. @remarks Possible error codes:
  358. + `AuthErrorCodeOperationNotAllowed` - Indicates that email and email sign-in link
  359. accounts are not enabled. Enable them in the Auth section of the
  360. Firebase console.
  361. + `AuthErrorCodeUserDisabled` - Indicates the user's account is disabled.
  362. + `AuthErrorCodeInvalidEmail` - Indicates the email address is invalid.
  363. @remarks See `AuthErrors` for a list of error codes that are common to all API methods.
  364. */
  365. - (void)signInWithEmail:(NSString *)email
  366. link:(NSString *)link
  367. completion:(nullable void (^)(FIRAuthDataResult *_Nullable authResult,
  368. NSError *_Nullable error))completion
  369. API_UNAVAILABLE(watchos);
  370. /** @fn signInWithProvider:UIDelegate:completion:
  371. @brief Signs in using the provided auth provider instance.
  372. This method is available on iOS, macOS Catalyst, and tvOS only.
  373. @param provider An instance of an auth provider used to initiate the sign-in flow.
  374. @param UIDelegate Optionally an instance of a class conforming to the AuthUIDelegate
  375. protocol, this is used for presenting the web context. If nil, a default AuthUIDelegate
  376. will be used.
  377. @param completion Optionally; a block which is invoked when the sign in flow finishes, or is
  378. canceled. Invoked asynchronously on the main thread in the future.
  379. @remarks Possible error codes:
  380. <ul>
  381. <li>@c AuthErrorCodeOperationNotAllowed - Indicates that email and password
  382. accounts are not enabled. Enable them in the Auth section of the
  383. Firebase console.
  384. </li>
  385. <li>@c AuthErrorCodeUserDisabled - Indicates the user's account is disabled.
  386. </li>
  387. <li>@c AuthErrorCodeWebNetworkRequestFailed - Indicates that a network request within a
  388. SFSafariViewController or WKWebView failed.
  389. </li>
  390. <li>@c AuthErrorCodeWebInternalError - Indicates that an internal error occurred within a
  391. SFSafariViewController or WKWebView.
  392. </li>
  393. <li>@c AuthErrorCodeWebSignInUserInteractionFailure - Indicates a general failure during
  394. a web sign-in flow.
  395. </li>
  396. <li>@c AuthErrorCodeWebContextAlreadyPresented - Indicates that an attempt was made to
  397. present a new web context while one was already being presented.
  398. </li>
  399. <li>@c AuthErrorCodeWebContextCancelled - Indicates that the URL presentation was
  400. cancelled prematurely by the user.
  401. </li>
  402. <li>@c AuthErrorCodeAccountExistsWithDifferentCredential - Indicates the email asserted
  403. by the credential (e.g. the email in a Facebook access token) is already in use by an
  404. existing account, that cannot be authenticated with this sign-in method. Call
  405. fetchProvidersForEmail for this user’s email and then prompt them to sign in with any of
  406. the sign-in providers returned. This error will only be thrown if the "One account per
  407. email address" setting is enabled in the Firebase console, under Auth settings.
  408. </li>
  409. </ul>
  410. @remarks See @c AuthErrors for a list of error codes that are common to all API methods.
  411. */
  412. - (void)signInWithProvider:(id<FIRFederatedAuthProvider>)provider
  413. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  414. completion:(nullable void (^)(FIRAuthDataResult *_Nullable authResult,
  415. NSError *_Nullable error))completion
  416. API_UNAVAILABLE(macosx, watchos);
  417. /** @fn signInWithCredential:completion:
  418. @brief Asynchronously signs in to Firebase with the given 3rd-party credentials (e.g. a Facebook
  419. login Access Token, a Google ID Token/Access Token pair, etc.) and returns additional
  420. identity provider data.
  421. @param credential The credential supplied by the IdP.
  422. @param completion Optionally; a block which is invoked when the sign in flow finishes, or is
  423. canceled. Invoked asynchronously on the main thread in the future.
  424. @remarks Possible error codes:
  425. + `AuthErrorCodeInvalidCredential` - Indicates the supplied credential is invalid.
  426. This could happen if it has expired or it is malformed.
  427. + `AuthErrorCodeOperationNotAllowed` - Indicates that accounts
  428. with the identity provider represented by the credential are not enabled.
  429. Enable them in the Auth section of the Firebase console.
  430. + `AuthErrorCodeAccountExistsWithDifferentCredential` - Indicates the email asserted
  431. by the credential (e.g. the email in a Facebook access token) is already in use by an
  432. existing account, that cannot be authenticated with this sign-in method. Call
  433. fetchProvidersForEmail for this user’s email and then prompt them to sign in with any of
  434. the sign-in providers returned. This error will only be thrown if the "One account per
  435. email address" setting is enabled in the Firebase console, under Auth settings.
  436. + `AuthErrorCodeUserDisabled` - Indicates the user's account is disabled.
  437. + `AuthErrorCodeWrongPassword` - Indicates the user attempted sign in with an
  438. incorrect password, if credential is of the type EmailPasswordAuthCredential.
  439. + `AuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  440. + `AuthErrorCodeMissingVerificationID` - Indicates that the phone auth credential was
  441. created with an empty verification ID.
  442. + `AuthErrorCodeMissingVerificationCode` - Indicates that the phone auth credential
  443. was created with an empty verification code.
  444. + `AuthErrorCodeInvalidVerificationCode` - Indicates that the phone auth credential
  445. was created with an invalid verification Code.
  446. + `AuthErrorCodeInvalidVerificationID` - Indicates that the phone auth credential was
  447. created with an invalid verification ID.
  448. + `AuthErrorCodeSessionExpired` - Indicates that the SMS code has expired.
  449. @remarks See `AuthErrors` for a list of error codes that are common to all API methods
  450. */
  451. - (void)signInWithCredential:(FIRAuthCredential *)credential
  452. completion:(nullable void (^)(FIRAuthDataResult *_Nullable authResult,
  453. NSError *_Nullable error))completion;
  454. /** @fn signInAnonymouslyWithCompletion:
  455. @brief Asynchronously creates and becomes an anonymous user.
  456. @param completion Optionally; a block which is invoked when the sign in finishes, or is
  457. canceled. Invoked asynchronously on the main thread in the future.
  458. @remarks If there is already an anonymous user signed in, that user will be returned instead.
  459. If there is any other existing user signed in, that user will be signed out.
  460. @remarks Possible error codes:
  461. + `AuthErrorCodeOperationNotAllowed` - Indicates that anonymous accounts are
  462. not enabled. Enable them in the Auth section of the Firebase console.
  463. @remarks See `AuthErrors` for a list of error codes that are common to all API methods.
  464. */
  465. - (void)signInAnonymouslyWithCompletion:(nullable void (^)(FIRAuthDataResult *_Nullable authResult,
  466. NSError *_Nullable error))completion;
  467. /** @fn signInWithCustomToken:completion:
  468. @brief Asynchronously signs in to Firebase with the given Auth token.
  469. @param token A self-signed custom auth token.
  470. @param completion Optionally; a block which is invoked when the sign in finishes, or is
  471. canceled. Invoked asynchronously on the main thread in the future.
  472. @remarks Possible error codes:
  473. + `AuthErrorCodeInvalidCustomToken` - Indicates a validation error with
  474. the custom token.
  475. + `AuthErrorCodeCustomTokenMismatch` - Indicates the service account and the API key
  476. belong to different projects.
  477. @remarks See `AuthErrors` for a list of error codes that are common to all API methods.
  478. */
  479. - (void)signInWithCustomToken:(NSString *)token
  480. completion:(nullable void (^)(FIRAuthDataResult *_Nullable authResult,
  481. NSError *_Nullable error))completion;
  482. /** @fn createUserWithEmail:password:completion:
  483. @brief Creates and, on success, signs in a user with the given email address and password.
  484. @param email The user's email address.
  485. @param password The user's desired password.
  486. @param completion Optionally; a block which is invoked when the sign up flow finishes, or is
  487. canceled. Invoked asynchronously on the main thread in the future.
  488. @remarks Possible error codes:
  489. + `AuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  490. + `AuthErrorCodeEmailAlreadyInUse` - Indicates the email used to attempt sign up
  491. already exists. Call fetchProvidersForEmail to check which sign-in mechanisms the user
  492. used, and prompt the user to sign in with one of those.
  493. + `AuthErrorCodeOperationNotAllowed` - Indicates that email and password accounts
  494. are not enabled. Enable them in the Auth section of the Firebase console.
  495. + `AuthErrorCodeWeakPassword` - Indicates an attempt to set a password that is
  496. considered too weak. The NSLocalizedFailureReasonErrorKey field in the NSError.userInfo
  497. dictionary object will contain more detailed explanation that can be shown to the user.
  498. @remarks See `AuthErrors` for a list of error codes that are common to all API methods.
  499. */
  500. - (void)createUserWithEmail:(NSString *)email
  501. password:(NSString *)password
  502. completion:(nullable void (^)(FIRAuthDataResult *_Nullable authResult,
  503. NSError *_Nullable error))completion;
  504. /** @fn confirmPasswordResetWithCode:newPassword:completion:
  505. @brief Resets the password given a code sent to the user outside of the app and a new password
  506. for the user.
  507. @param newPassword The new password.
  508. @param completion Optionally; a block which is invoked when the request finishes. Invoked
  509. asynchronously on the main thread in the future.
  510. @remarks Possible error codes:
  511. + `AuthErrorCodeWeakPassword` - Indicates an attempt to set a password that is
  512. considered too weak.
  513. + `AuthErrorCodeOperationNotAllowed` - Indicates the administrator disabled sign
  514. in with the specified identity provider.
  515. + `AuthErrorCodeExpiredActionCode` - Indicates the OOB code is expired.
  516. + `AuthErrorCodeInvalidActionCode` - Indicates the OOB code is invalid.
  517. @remarks See `AuthErrors` for a list of error codes that are common to all API methods.
  518. */
  519. - (void)confirmPasswordResetWithCode:(NSString *)code
  520. newPassword:(NSString *)newPassword
  521. completion:(void (^)(NSError *_Nullable error))completion;
  522. /** @fn checkActionCode:completion:
  523. @brief Checks the validity of an out of band code.
  524. @param code The out of band code to check validity.
  525. @param completion Optionally; a block which is invoked when the request finishes. Invoked
  526. asynchronously on the main thread in the future.
  527. */
  528. - (void)checkActionCode:(NSString *)code
  529. completion:
  530. (void (^)(FIRActionCodeInfo *_Nullable info, NSError *_Nullable error))completion;
  531. /** @fn verifyPasswordResetCode:completion:
  532. @brief Checks the validity of a verify password reset code.
  533. @param code The password reset code to be verified.
  534. @param completion Optionally; a block which is invoked when the request finishes. Invoked
  535. asynchronously on the main thread in the future.
  536. */
  537. - (void)verifyPasswordResetCode:(NSString *)code
  538. completion:
  539. (void (^)(NSString *_Nullable email, NSError *_Nullable error))completion;
  540. /** @fn applyActionCode:completion:
  541. @brief Applies out of band code.
  542. @param code The out of band code to be applied.
  543. @param completion Optionally; a block which is invoked when the request finishes. Invoked
  544. asynchronously on the main thread in the future.
  545. @remarks This method will not work for out of band codes which require an additional parameter,
  546. such as password reset code.
  547. */
  548. - (void)applyActionCode:(NSString *)code completion:(void (^)(NSError *_Nullable error))completion;
  549. /** @fn sendPasswordResetWithEmail:completion:
  550. @brief Initiates a password reset for the given email address. This method does not throw an
  551. error when there's no user account with the given email address and [Email Enumeration
  552. Protection](https://cloud.google.com/identity-platform/docs/admin/email-enumeration-protection)
  553. is enabled.
  554. @param email The email address of the user.
  555. @param completion Optionally; a block which is invoked when the request finishes. Invoked
  556. asynchronously on the main thread in the future.
  557. @remarks Possible error codes:
  558. + `AuthErrorCodeInvalidRecipientEmail` - Indicates an invalid recipient email was
  559. sent in the request.
  560. + `AuthErrorCodeInvalidSender` - Indicates an invalid sender email is set in
  561. the console for this action.
  562. + `AuthErrorCodeInvalidMessagePayload` - Indicates an invalid email template for
  563. sending update email.
  564. */
  565. - (void)sendPasswordResetWithEmail:(NSString *)email
  566. completion:(nullable void (^)(NSError *_Nullable error))completion;
  567. /** @fn sendPasswordResetWithEmail:actionCodeSetting:completion:
  568. @brief Initiates a password reset for the given email address and `ActionCodeSettings` object.
  569. @param email The email address of the user.
  570. @param actionCodeSettings An `ActionCodeSettings` object containing settings related to
  571. handling action codes.
  572. @param completion Optionally; a block which is invoked when the request finishes. Invoked
  573. asynchronously on the main thread in the future.
  574. @remarks Possible error codes:
  575. + `AuthErrorCodeInvalidRecipientEmail` - Indicates an invalid recipient email was
  576. sent in the request.
  577. + `AuthErrorCodeInvalidSender` - Indicates an invalid sender email is set in
  578. the console for this action.
  579. + `AuthErrorCodeInvalidMessagePayload` - Indicates an invalid email template for
  580. sending update email.
  581. + `AuthErrorCodeMissingIosBundleID` - Indicates that the iOS bundle ID is missing when
  582. `handleCodeInApp` is set to true.
  583. + `AuthErrorCodeMissingAndroidPackageName` - Indicates that the android package name
  584. is missing when the `androidInstallApp` flag is set to true.
  585. + `AuthErrorCodeUnauthorizedDomain` - Indicates that the domain specified in the
  586. continue URL is not allowlisted in the Firebase console.
  587. + `AuthErrorCodeInvalidContinueURI` - Indicates that the domain specified in the
  588. continue URL is not valid.
  589. */
  590. - (void)sendPasswordResetWithEmail:(NSString *)email
  591. actionCodeSettings:(FIRActionCodeSettings *)actionCodeSettings
  592. completion:(nullable void (^)(NSError *_Nullable error))completion;
  593. /** @fn sendSignInLinkToEmail:actionCodeSettings:completion:
  594. @brief Sends a sign in with email link to provided email address.
  595. @param email The email address of the user.
  596. @param actionCodeSettings An `ActionCodeSettings` object containing settings related to
  597. handling action codes.
  598. @param completion Optionally; a block which is invoked when the request finishes. Invoked
  599. asynchronously on the main thread in the future.
  600. */
  601. - (void)sendSignInLinkToEmail:(NSString *)email
  602. actionCodeSettings:(FIRActionCodeSettings *)actionCodeSettings
  603. completion:(nullable void (^)(NSError *_Nullable error))completion
  604. API_UNAVAILABLE(watchos);
  605. /** @fn signOut:
  606. @brief Signs out the current user.
  607. @param error Optionally; if an error occurs, upon return contains an NSError object that
  608. describes the problem; is nil otherwise.
  609. @return @YES when the sign out request was successful. @NO otherwise.
  610. @remarks Possible error codes:
  611. + `AuthErrorCodeKeychainError` - Indicates an error occurred when accessing the
  612. keychain. The `NSLocalizedFailureReasonErrorKey` field in the `userInfo`
  613. dictionary will contain more information about the error encountered.
  614. */
  615. - (BOOL)signOut:(NSError *_Nullable *_Nullable)error;
  616. /** @fn isSignInWithEmailLink
  617. @brief Checks if link is an email sign-in link.
  618. @param link The email sign-in link.
  619. @return Returns true when the link passed matches the expected format of an email sign-in link.
  620. */
  621. - (BOOL)isSignInWithEmailLink:(NSString *)link API_UNAVAILABLE(watchos);
  622. /** @fn addAuthStateDidChangeListener:
  623. @brief Registers a block as an "auth state did change" listener. To be invoked when:
  624. + The block is registered as a listener,
  625. + A user with a different UID from the current user has signed in, or
  626. + The current user has signed out.
  627. @param listener The block to be invoked. The block is always invoked asynchronously on the main
  628. thread, even for it's initial invocation after having been added as a listener.
  629. @remarks The block is invoked immediately after adding it according to it's standard invocation
  630. semantics, asynchronously on the main thread. Users should pay special attention to
  631. making sure the block does not inadvertently retain objects which should not be retained by
  632. the long-lived block. The block itself will be retained by `Auth` until it is
  633. unregistered or until the `Auth` instance is otherwise deallocated.
  634. @return A handle useful for manually unregistering the block as a listener.
  635. */
  636. - (FIRAuthStateDidChangeListenerHandle)addAuthStateDidChangeListener:
  637. (void (^)(FIRAuth *auth, FIRUser *_Nullable user))listener;
  638. /** @fn removeAuthStateDidChangeListener:
  639. @brief Unregisters a block as an "auth state did change" listener.
  640. @param listenerHandle The handle for the listener.
  641. */
  642. - (void)removeAuthStateDidChangeListener:(FIRAuthStateDidChangeListenerHandle)listenerHandle;
  643. /** @fn addIDTokenDidChangeListener:
  644. @brief Registers a block as an "ID token did change" listener. To be invoked when:
  645. + The block is registered as a listener,
  646. + A user with a different UID from the current user has signed in,
  647. + The ID token of the current user has been refreshed, or
  648. + The current user has signed out.
  649. @param listener The block to be invoked. The block is always invoked asynchronously on the main
  650. thread, even for it's initial invocation after having been added as a listener.
  651. @remarks The block is invoked immediately after adding it according to it's standard invocation
  652. semantics, asynchronously on the main thread. Users should pay special attention to
  653. making sure the block does not inadvertently retain objects which should not be retained by
  654. the long-lived block. The block itself will be retained by `Auth` until it is
  655. unregistered or until the `Auth` instance is otherwise deallocated.
  656. @return A handle useful for manually unregistering the block as a listener.
  657. */
  658. - (FIRIDTokenDidChangeListenerHandle)addIDTokenDidChangeListener:
  659. (void (^)(FIRAuth *auth, FIRUser *_Nullable user))listener;
  660. /** @fn removeIDTokenDidChangeListener:
  661. @brief Unregisters a block as an "ID token did change" listener.
  662. @param listenerHandle The handle for the listener.
  663. */
  664. - (void)removeIDTokenDidChangeListener:(FIRIDTokenDidChangeListenerHandle)listenerHandle;
  665. /** @fn useAppLanguage
  666. @brief Sets `languageCode` to the app's current language.
  667. */
  668. - (void)useAppLanguage;
  669. /** @fn useEmulatorWithHost:port
  670. @brief Configures Firebase Auth to connect to an emulated host instead of the remote backend.
  671. */
  672. - (void)useEmulatorWithHost:(NSString *)host port:(NSInteger)port;
  673. /** @fn canHandleURL:
  674. @brief Whether the specific URL is handled by `Auth` .
  675. This method is available on iOS only.
  676. @param URL The URL received by the application delegate from any of the openURL method.
  677. @return Whether or the URL is handled. YES means the URL is for Firebase Auth
  678. so the caller should ignore the URL from further processing, and NO means the
  679. the URL is for the app (or another library) so the caller should continue handling
  680. this URL as usual.
  681. @remarks If swizzling is disabled, URLs received by the application delegate must be forwarded
  682. to this method for phone number auth to work.
  683. */
  684. - (BOOL)canHandleURL:(nonnull NSURL *)URL API_UNAVAILABLE(macos, tvos, watchos);
  685. /** @fn setAPNSToken:type:
  686. @brief Sets the APNs token along with its type.
  687. This method is available on iOS only.
  688. @remarks If swizzling is disabled, the APNs Token must be set for phone number auth to work,
  689. by either setting calling this method or by setting the `APNSToken` property.
  690. */
  691. - (void)setAPNSToken:(NSData *)token
  692. type:(FIRAuthAPNSTokenType)type API_UNAVAILABLE(macos, tvos, watchos);
  693. /** @fn canHandleNotification:
  694. @brief Whether the specific remote notification is handled by `Auth` .
  695. This method is available on iOS only.
  696. @param userInfo A dictionary that contains information related to the
  697. notification in question.
  698. @return Whether or the notification is handled. A return value of true means the notification
  699. is for Firebase Auth so the caller should ignore the notification from further processing,
  700. and false means the notification is for the app (or another library) so the caller
  701. should continue handling this notification as usual.
  702. @remarks If swizzling is disabled, related remote notifications must be forwarded to this method
  703. for phone number auth to work.
  704. */
  705. - (BOOL)canHandleNotification:(NSDictionary *)userInfo API_UNAVAILABLE(macos, tvos, watchos);
  706. /** @fn revokeTokenWithAuthorizationCode:Completion
  707. @brief Revoke the users token with authorization code.
  708. @param completion (Optional) the block invoked when the request to revoke the token is
  709. complete, or fails. Invoked asynchronously on the main thread in the future.
  710. */
  711. - (void)revokeTokenWithAuthorizationCode:(NSString *)authorizationCode
  712. completion:(nullable void (^)(NSError *_Nullable error))completion;
  713. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
  714. /** @fn initializeRecaptchaConfigWithCompletion:completion:
  715. @brief Initializes reCAPTCHA using the settings configured for the project or
  716. tenant.
  717. If you change the tenant ID of the `Auth` instance, the configuration will be
  718. reloaded.
  719. */
  720. - (void)initializeRecaptchaConfigWithCompletion:
  721. (nullable void (^)(NSError *_Nullable error))completion;
  722. #endif
  723. #pragma mark - User sharing
  724. /** @fn useUserAccessGroup:error:
  725. @brief Switch userAccessGroup and current user to the given accessGroup and the user stored in
  726. it.
  727. */
  728. - (BOOL)useUserAccessGroup:(NSString *_Nullable)accessGroup
  729. error:(NSError *_Nullable *_Nullable)outError;
  730. /** @fn getStoredUserForAccessGroup:error:
  731. @brief Get the stored user in the given accessGroup.
  732. @note This API is not supported on tvOS when `shareAuthStateAcrossDevices` is set to `true`.
  733. This case will return `nil`.
  734. Please refer to https://github.com/firebase/firebase-ios-sdk/issues/8878 for details.
  735. */
  736. - (nullable FIRUser *)getStoredUserForAccessGroup:(NSString *_Nullable)accessGroup
  737. error:(NSError *_Nullable *_Nullable)outError
  738. __attribute__((swift_error(nonnull_error))); // This method can return `nil` on success.
  739. @end
  740. NS_ASSUME_NONNULL_END