FIRUser.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 <Foundation/Foundation.h>
  17. #import "FIRAuth.h"
  18. #import "FIRAuthDataResult.h"
  19. #import "FIRAuthSwiftNameSupport.h"
  20. #import "FIRUserInfo.h"
  21. @class FIRPhoneAuthCredential;
  22. @class FIRUserProfileChangeRequest;
  23. NS_ASSUME_NONNULL_BEGIN
  24. /** @typedef FIRAuthTokenCallback
  25. @brief The type of block called when a token is ready for use.
  26. @see FIRUser.getIDTokenWithCompletion:
  27. @see FIRUser.getIDTokenForcingRefresh:withCompletion:
  28. @param token Optionally; an access token if the request was successful.
  29. @param error Optionally; the error which occurred - or nil if the request was successful.
  30. @remarks One of: @c token or @c error will always be non-nil.
  31. */
  32. typedef void (^FIRAuthTokenCallback)(NSString *_Nullable token, NSError *_Nullable error)
  33. FIR_SWIFT_NAME(AuthTokenCallback);
  34. /** @typedef FIRUserProfileChangeCallback
  35. @brief The type of block called when a user profile change has finished.
  36. @param error Optionally; the error which occurred - or nil if the request was successful.
  37. */
  38. typedef void (^FIRUserProfileChangeCallback)(NSError *_Nullable error)
  39. FIR_SWIFT_NAME(UserProfileChangeCallback);
  40. /** @typedef FIRSendEmailVerificationCallback
  41. @brief The type of block called when a request to send an email verification has finished.
  42. @param error Optionally; the error which occurred - or nil if the request was successful.
  43. */
  44. typedef void (^FIRSendEmailVerificationCallback)(NSError *_Nullable error)
  45. FIR_SWIFT_NAME(SendEmailVerificationCallback);
  46. /** @class FIRUser
  47. @brief Represents a user.
  48. @remarks This class is thread-safe.
  49. */
  50. FIR_SWIFT_NAME(User)
  51. @interface FIRUser : NSObject <FIRUserInfo>
  52. /** @property anonymous
  53. @brief Indicates the user represents an anonymous user.
  54. */
  55. @property(nonatomic, readonly, getter=isAnonymous) BOOL anonymous;
  56. /** @property emailVerified
  57. @brief Indicates the email address associated with this user has been verified.
  58. */
  59. @property(nonatomic, readonly, getter=isEmailVerified) BOOL emailVerified;
  60. /** @property refreshToken
  61. @brief A refresh token; useful for obtaining new access tokens independently.
  62. @remarks This property should only be used for advanced scenarios, and is not typically needed.
  63. */
  64. @property(nonatomic, readonly, nullable) NSString *refreshToken;
  65. /** @property providerData
  66. @brief Profile data for each identity provider, if any.
  67. @remarks This data is cached on sign-in and updated when linking or unlinking.
  68. */
  69. @property(nonatomic, readonly, nonnull) NSArray<id<FIRUserInfo>> *providerData;
  70. /** @fn init
  71. @brief This class should not be instantiated.
  72. @remarks To retrieve the current user, use @c FIRAuth.currentUser. To sign a user
  73. in or out, use the methods on @c FIRAuth.
  74. */
  75. - (instancetype)init NS_UNAVAILABLE;
  76. /** @fn updateEmail:completion:
  77. @brief Updates the email address for the user. On success, the cached user profile data is
  78. updated.
  79. @remarks May fail if there is already an account with this email address that was created using
  80. email and password authentication.
  81. @param email The email address for the user.
  82. @param completion Optionally; the block invoked when the user profile change has finished.
  83. Invoked asynchronously on the main thread in the future.
  84. @remarks Possible error codes:
  85. <ul>
  86. <li>@c FIRAuthErrorCodeInvalidRecipientEmail - Indicates an invalid recipient email was
  87. sent in the request.
  88. </li>
  89. <li>@c FIRAuthErrorCodeInvalidSender - Indicates an invalid sender email is set in
  90. the console for this action.
  91. </li>
  92. <li>@c FIRAuthErrorCodeInvalidMessagePayload - Indicates an invalid email template for
  93. sending update email.
  94. </li>
  95. <li>@c FIRAuthErrorCodeEmailAlreadyInUse - Indicates the email is already in use by another
  96. account.
  97. </li>
  98. <li>@c FIRAuthErrorCodeInvalidEmail - Indicates the email address is malformed.
  99. </li>
  100. <li>@c FIRAuthErrorCodeRequiresRecentLogin - Updating a user’s email is a security
  101. sensitive operation that requires a recent login from the user. This error indicates
  102. the user has not signed in recently enough. To resolve, reauthenticate the user by
  103. invoking reauthenticateWithCredential:completion: on FIRUser.
  104. </li>
  105. </ul>
  106. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  107. */
  108. - (void)updateEmail:(NSString *)email completion:(nullable FIRUserProfileChangeCallback)completion
  109. FIR_SWIFT_NAME(updateEmail(to:completion:));
  110. /** @fn updatePassword:completion:
  111. @brief Updates the password for the user. On success, the cached user profile data is updated.
  112. @param password The new password for the user.
  113. @param completion Optionally; the block invoked when the user profile change has finished.
  114. Invoked asynchronously on the main thread in the future.
  115. @remarks Possible error codes:
  116. <ul>
  117. <li>@c FIRAuthErrorCodeOperationNotAllowed - Indicates the administrator disabled
  118. sign in with the specified identity provider.
  119. </li>
  120. <li>@c FIRAuthErrorCodeRequiresRecentLogin - Updating a user’s password is a security
  121. sensitive operation that requires a recent login from the user. This error indicates
  122. the user has not signed in recently enough. To resolve, reauthenticate the user by
  123. invoking reauthenticateWithCredential:completion: on FIRUser.
  124. </li>
  125. <li>@c FIRAuthErrorCodeWeakPassword - Indicates an attempt to set a password that is
  126. considered too weak. The NSLocalizedFailureReasonErrorKey field in the NSError.userInfo
  127. dictionary object will contain more detailed explanation that can be shown to the user.
  128. </li>
  129. </ul>
  130. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  131. */
  132. - (void)updatePassword:(NSString *)password
  133. completion:(nullable FIRUserProfileChangeCallback)completion
  134. FIR_SWIFT_NAME(updatePassword(to:completion:));
  135. /** @fn updatePhoneNumberCredential:completion:
  136. @brief Updates the phone number for the user. On success, the cached user profile data is
  137. updated.
  138. @param phoneNumberCredential The new phone number credential corresponding to the phone number
  139. to be added to the firebaes account, if a phone number is already linked to the account this
  140. new phone number will replace it.
  141. @param completion Optionally; the block invoked when the user profile change has finished.
  142. Invoked asynchronously on the main thread in the future.
  143. @remarks Possible error codes:
  144. <ul>
  145. <li>@c FIRAuthErrorCodeRequiresRecentLogin - Updating a user’s phone number is a security
  146. sensitive operation that requires a recent login from the user. This error indicates
  147. the user has not signed in recently enough. To resolve, reauthenticate the user by
  148. invoking reauthenticateWithCredential:completion: on FIRUser.
  149. </li>
  150. </ul>
  151. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  152. */
  153. - (void)updatePhoneNumberCredential:(FIRPhoneAuthCredential *)phoneNumberCredential
  154. completion:(nullable FIRUserProfileChangeCallback)completion;
  155. /** @fn profileChangeRequest
  156. @brief Creates an object which may be used to change the user's profile data.
  157. @remarks Set the properties of the returned object, then call
  158. @c FIRUserProfileChangeRequest.commitChangesWithCallback: to perform the updates atomically.
  159. @return An object which may be used to change the user's profile data atomically.
  160. */
  161. - (FIRUserProfileChangeRequest *)profileChangeRequest FIR_SWIFT_NAME(createProfileChangeRequest());
  162. /** @fn reloadWithCompletion:
  163. @brief Reloads the user's profile data from the server.
  164. @param completion Optionally; the block invoked when the reload has finished. Invoked
  165. asynchronously on the main thread in the future.
  166. @remarks May fail with a @c FIRAuthErrorCodeRequiresRecentLogin error code. In this case
  167. you should call @c FIRUser.reauthenticateWithCredential:completion: before re-invoking
  168. @c FIRUser.updateEmail:completion:.
  169. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  170. */
  171. - (void)reloadWithCompletion:(nullable FIRUserProfileChangeCallback)completion;
  172. /** @fn reauthenticateWithCredential:completion:
  173. @brief Convenience method for @c reauthenticateAndRetrieveDataWithCredential:completion: This
  174. method doesn't return additional identity provider data.
  175. */
  176. - (void)reauthenticateWithCredential:(FIRAuthCredential *)credential
  177. completion:(nullable FIRUserProfileChangeCallback)completion;
  178. /** @fn reauthenticateWithCredential:completion:
  179. @brief Renews the user's authentication tokens by validating a fresh set of credentials supplied
  180. by the user and returns additional identity provider data.
  181. @param credential A user-supplied credential, which will be validated by the server. This can be
  182. a successful third-party identity provider sign-in, or an email address and password.
  183. @param completion Optionally; the block invoked when the re-authentication operation has
  184. finished. Invoked asynchronously on the main thread in the future.
  185. @remarks If the user associated with the supplied credential is different from the current user,
  186. or if the validation of the supplied credentials fails; an error is returned and the current
  187. user remains signed in.
  188. @remarks Possible error codes:
  189. <ul>
  190. <li>@c FIRAuthErrorCodeInvalidCredential - Indicates the supplied credential is invalid.
  191. This could happen if it has expired or it is malformed.
  192. </li>
  193. <li>@c FIRAuthErrorCodeOperationNotAllowed - Indicates that accounts with the
  194. identity provider represented by the credential are not enabled. Enable them in the
  195. Auth section of the Firebase console.
  196. </li>
  197. <li>@c FIRAuthErrorCodeEmailAlreadyInUse - Indicates the email asserted by the credential
  198. (e.g. the email in a Facebook access token) is already in use by an existing account,
  199. that cannot be authenticated with this method. Call fetchProvidersForEmail for
  200. this user’s email and then prompt them to sign in with any of the sign-in providers
  201. returned. This error will only be thrown if the "One account per email address"
  202. setting is enabled in the Firebase console, under Auth settings. Please note that the
  203. error code raised in this specific situation may not be the same on Web and Android.
  204. </li>
  205. <li>@c FIRAuthErrorCodeUserDisabled - Indicates the user's account is disabled.
  206. </li>
  207. <li>@c FIRAuthErrorCodeWrongPassword - Indicates the user attempted reauthentication with
  208. an incorrect password, if credential is of the type EmailPasswordAuthCredential.
  209. </li>
  210. <li>@c FIRAuthErrorCodeUserMismatch - Indicates that an attempt was made to
  211. reauthenticate with a user which is not the current user.
  212. </li>
  213. <li>@c FIRAuthErrorCodeInvalidEmail - Indicates the email address is malformed.</li>
  214. </ul>
  215. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  216. */
  217. - (void)reauthenticateAndRetrieveDataWithCredential:(FIRAuthCredential *) credential
  218. completion:(nullable FIRAuthDataResultCallback) completion;
  219. /** @fn getIDTokenWithCompletion:
  220. @brief Retrieves the Firebase authentication token, possibly refreshing it if it has expired.
  221. @param completion Optionally; the block invoked when the token is available. Invoked
  222. asynchronously on the main thread in the future.
  223. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  224. */
  225. - (void)getIDTokenWithCompletion:(nullable FIRAuthTokenCallback)completion
  226. FIR_SWIFT_NAME(getIDToken(completion:));
  227. /** @fn getTokenWithCompletion:
  228. @brief Please use @c getIDTokenWithCompletion: instead.
  229. @param completion Optionally; the block invoked when the token is available. Invoked
  230. asynchronously on the main thread in the future.
  231. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  232. */
  233. - (void)getTokenWithCompletion:(nullable FIRAuthTokenCallback)completion
  234. FIR_SWIFT_NAME(getToken(completion:)) __attribute__((deprecated));
  235. /** @fn getIDTokenForcingRefresh:completion:
  236. @brief Retrieves the Firebase authentication token, possibly refreshing it if it has expired.
  237. @param forceRefresh Forces a token refresh. Useful if the token becomes invalid for some reason
  238. other than an expiration.
  239. @param completion Optionally; the block invoked when the token is available. Invoked
  240. asynchronously on the main thread in the future.
  241. @remarks The authentication token will be refreshed (by making a network request) if it has
  242. expired, or if @c forceRefresh is YES.
  243. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  244. */
  245. - (void)getIDTokenForcingRefresh:(BOOL)forceRefresh
  246. completion:(nullable FIRAuthTokenCallback)completion;
  247. /** @fn getTokenForcingRefresh:completion:
  248. @brief Please use getIDTokenForcingRefresh:completion instead.
  249. @param forceRefresh Forces a token refresh. Useful if the token becomes invalid for some reason
  250. other than an expiration.
  251. @param completion Optionally; the block invoked when the token is available. Invoked
  252. asynchronously on the main thread in the future.
  253. @remarks The authentication token will be refreshed (by making a network request) if it has
  254. expired, or if @c forceRefresh is YES.
  255. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  256. */
  257. - (void)getTokenForcingRefresh:(BOOL)forceRefresh
  258. completion:(nullable FIRAuthTokenCallback)completion
  259. __attribute__((deprecated));
  260. /** @fn linkWithCredential:completion:
  261. @brief Convenience method for @c linkAndRetrieveDataWithCredential:completion: This method
  262. doesn't return additional identity provider data.
  263. */
  264. - (void)linkWithCredential:(FIRAuthCredential *)credential
  265. completion:(nullable FIRAuthResultCallback)completion;
  266. /** @fn linkAndRetrieveDataWithCredential:completion:
  267. @brief Associates a user account from a third-party identity provider with this user and
  268. returns additional identity provider data.
  269. @param credential The credential for the identity provider.
  270. @param completion Optionally; the block invoked when the unlinking is complete, or fails.
  271. Invoked asynchronously on the main thread in the future.
  272. @remarks Possible error codes:
  273. <ul>
  274. <li>@c FIRAuthErrorCodeProviderAlreadyLinked - Indicates an attempt to link a provider of a
  275. type already linked to this account.
  276. </li>
  277. <li>@c FIRAuthErrorCodeCredentialAlreadyInUse - Indicates an attempt to link with a
  278. credential
  279. that has already been linked with a different Firebase account.
  280. </li>
  281. <li>@c FIRAuthErrorCodeOperationNotAllowed - Indicates that accounts with the identity
  282. provider represented by the credential are not enabled. Enable them in the Auth section
  283. of the Firebase console.
  284. </li>
  285. </ul>
  286. @remarks This method may also return error codes associated with updateEmail:completion: and
  287. updatePassword:completion: on FIRUser.
  288. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  289. */
  290. - (void)linkAndRetrieveDataWithCredential:(FIRAuthCredential *) credential
  291. completion:(nullable FIRAuthDataResultCallback) completion;
  292. /** @fn unlinkFromProvider:completion:
  293. @brief Disassociates a user account from a third-party identity provider with this user.
  294. @param provider The provider ID of the provider to unlink.
  295. @param completion Optionally; the block invoked when the unlinking is complete, or fails.
  296. Invoked asynchronously on the main thread in the future.
  297. @remarks Possible error codes:
  298. <ul>
  299. <li>@c FIRAuthErrorCodeNoSuchProvider - Indicates an attempt to unlink a provider
  300. that is not linked to the account.
  301. </li>
  302. <li>@c FIRAuthErrorCodeRequiresRecentLogin - Updating email is a security sensitive
  303. operation that requires a recent login from the user. This error indicates the user
  304. has not signed in recently enough. To resolve, reauthenticate the user by invoking
  305. reauthenticateWithCredential:completion: on FIRUser.
  306. </li>
  307. </ul>
  308. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  309. */
  310. - (void)unlinkFromProvider:(NSString *)provider
  311. completion:(nullable FIRAuthResultCallback)completion;
  312. /** @fn sendEmailVerificationWithCompletion:
  313. @brief Initiates email verification for the user.
  314. @param completion Optionally; the block invoked when the request to send an email verification
  315. is complete, or fails. Invoked asynchronously on the main thread in the future.
  316. @remarks Possible error codes:
  317. <ul>
  318. <li>@c FIRAuthErrorCodeInvalidRecipientEmail - Indicates an invalid recipient email was
  319. sent in the request.
  320. </li>
  321. <li>@c FIRAuthErrorCodeInvalidSender - Indicates an invalid sender email is set in
  322. the console for this action.
  323. </li>
  324. <li>@c FIRAuthErrorCodeInvalidMessagePayload - Indicates an invalid email template for
  325. sending update email.
  326. </li>
  327. <li>@c FIRAuthErrorCodeUserNotFound - Indicates the user account was not found.</li>
  328. </ul>
  329. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  330. */
  331. - (void)sendEmailVerificationWithCompletion:(nullable FIRSendEmailVerificationCallback)completion;
  332. /** @fn deleteWithCompletion:
  333. @brief Deletes the user account (also signs out the user, if this was the current user).
  334. @param completion Optionally; the block invoked when the request to delete the account is
  335. complete, or fails. Invoked asynchronously on the main thread in the future.
  336. @remarks Possible error codes:
  337. <ul>
  338. <li>@c FIRAuthErrorCodeRequiresRecentLogin - Updating email is a security sensitive
  339. operation that requires a recent login from the user. This error indicates the user
  340. has not signed in recently enough. To resolve, reauthenticate the user by invoking
  341. reauthenticateWithCredential:completion: on FIRUser.
  342. </li>
  343. </ul>
  344. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  345. */
  346. - (void)deleteWithCompletion:(nullable FIRUserProfileChangeCallback)completion;
  347. @end
  348. /** @class FIRUserProfileChangeRequest
  349. @brief Represents an object capable of updating a user's profile data.
  350. @remarks Properties are marked as being part of a profile update when they are set. Setting a
  351. property value to nil is not the same as leaving the property unassigned.
  352. */
  353. FIR_SWIFT_NAME(UserProfileChangeRequest)
  354. @interface FIRUserProfileChangeRequest : NSObject
  355. /** @fn init
  356. @brief Please use @c FIRUser.profileChangeRequest
  357. */
  358. - (instancetype)init NS_UNAVAILABLE;
  359. /** @property displayName
  360. @brief The user's display name.
  361. @remarks It is an error to set this property after calling
  362. @c FIRUserProfileChangeRequest.commitChangesWithCallback:
  363. */
  364. @property(nonatomic, copy, nullable) NSString *displayName;
  365. /** @property photoURL
  366. @brief The user's photo URL.
  367. @remarks It is an error to set this property after calling
  368. @c FIRUserProfileChangeRequest.commitChangesWithCallback:
  369. */
  370. @property(nonatomic, copy, nullable) NSURL *photoURL;
  371. /** @fn commitChangesWithCompletion:
  372. @brief Commits any pending changes.
  373. @remarks This method should only be called once. Once called, property values should not be
  374. changed.
  375. @param completion Optionally; the block invoked when the user profile change has been applied.
  376. Invoked asynchronously on the main thread in the future.
  377. */
  378. - (void)commitChangesWithCompletion:(nullable FIRUserProfileChangeCallback)completion;
  379. @end
  380. NS_ASSUME_NONNULL_END