FIRUser.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. #if TARGET_OS_IOS
  136. /** @fn updatePhoneNumberCredential:completion:
  137. @brief Updates the phone number for the user. On success, the cached user profile data is
  138. updated.
  139. @param phoneNumberCredential The new phone number credential corresponding to the phone number
  140. to be added to the firebaes account, if a phone number is already linked to the account this
  141. new phone number will replace it.
  142. @param completion Optionally; the block invoked when the user profile change has finished.
  143. Invoked asynchronously on the main thread in the future.
  144. @remarks Possible error codes:
  145. <ul>
  146. <li>@c FIRAuthErrorCodeRequiresRecentLogin - Updating a user’s phone number is a security
  147. sensitive operation that requires a recent login from the user. This error indicates
  148. the user has not signed in recently enough. To resolve, reauthenticate the user by
  149. invoking reauthenticateWithCredential:completion: on FIRUser.
  150. </li>
  151. </ul>
  152. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  153. */
  154. - (void)updatePhoneNumberCredential:(FIRPhoneAuthCredential *)phoneNumberCredential
  155. completion:(nullable FIRUserProfileChangeCallback)completion;
  156. #endif
  157. /** @fn profileChangeRequest
  158. @brief Creates an object which may be used to change the user's profile data.
  159. @remarks Set the properties of the returned object, then call
  160. @c FIRUserProfileChangeRequest.commitChangesWithCallback: to perform the updates atomically.
  161. @return An object which may be used to change the user's profile data atomically.
  162. */
  163. - (FIRUserProfileChangeRequest *)profileChangeRequest FIR_SWIFT_NAME(createProfileChangeRequest());
  164. /** @fn reloadWithCompletion:
  165. @brief Reloads the user's profile data from the server.
  166. @param completion Optionally; the block invoked when the reload has finished. Invoked
  167. asynchronously on the main thread in the future.
  168. @remarks May fail with a @c FIRAuthErrorCodeRequiresRecentLogin error code. In this case
  169. you should call @c FIRUser.reauthenticateWithCredential:completion: before re-invoking
  170. @c FIRUser.updateEmail:completion:.
  171. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  172. */
  173. - (void)reloadWithCompletion:(nullable FIRUserProfileChangeCallback)completion;
  174. /** @fn reauthenticateWithCredential:completion:
  175. @brief Convenience method for @c reauthenticateAndRetrieveDataWithCredential:completion: This
  176. method doesn't return additional identity provider data.
  177. */
  178. - (void)reauthenticateWithCredential:(FIRAuthCredential *)credential
  179. completion:(nullable FIRUserProfileChangeCallback)completion;
  180. /** @fn reauthenticateWithCredential:completion:
  181. @brief Renews the user's authentication tokens by validating a fresh set of credentials supplied
  182. by the user and returns additional identity provider data.
  183. @param credential A user-supplied credential, which will be validated by the server. This can be
  184. a successful third-party identity provider sign-in, or an email address and password.
  185. @param completion Optionally; the block invoked when the re-authentication operation has
  186. finished. Invoked asynchronously on the main thread in the future.
  187. @remarks If the user associated with the supplied credential is different from the current user,
  188. or if the validation of the supplied credentials fails; an error is returned and the current
  189. user remains signed in.
  190. @remarks Possible error codes:
  191. <ul>
  192. <li>@c FIRAuthErrorCodeInvalidCredential - Indicates the supplied credential is invalid.
  193. This could happen if it has expired or it is malformed.
  194. </li>
  195. <li>@c FIRAuthErrorCodeOperationNotAllowed - Indicates that accounts with the
  196. identity provider represented by the credential are not enabled. Enable them in the
  197. Auth section of the Firebase console.
  198. </li>
  199. <li>@c FIRAuthErrorCodeEmailAlreadyInUse - Indicates the email asserted by the credential
  200. (e.g. the email in a Facebook access token) is already in use by an existing account,
  201. that cannot be authenticated with this method. Call fetchProvidersForEmail for
  202. this user’s email and then prompt them to sign in with any of the sign-in providers
  203. returned. This error will only be thrown if the "One account per email address"
  204. setting is enabled in the Firebase console, under Auth settings. Please note that the
  205. error code raised in this specific situation may not be the same on Web and Android.
  206. </li>
  207. <li>@c FIRAuthErrorCodeUserDisabled - Indicates the user's account is disabled.
  208. </li>
  209. <li>@c FIRAuthErrorCodeWrongPassword - Indicates the user attempted reauthentication with
  210. an incorrect password, if credential is of the type EmailPasswordAuthCredential.
  211. </li>
  212. <li>@c FIRAuthErrorCodeUserMismatch - Indicates that an attempt was made to
  213. reauthenticate with a user which is not the current user.
  214. </li>
  215. <li>@c FIRAuthErrorCodeInvalidEmail - Indicates the email address is malformed.</li>
  216. </ul>
  217. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  218. */
  219. - (void)reauthenticateAndRetrieveDataWithCredential:(FIRAuthCredential *) credential
  220. completion:(nullable FIRAuthDataResultCallback) completion;
  221. /** @fn getIDTokenWithCompletion:
  222. @brief Retrieves the Firebase authentication token, possibly refreshing it if it has expired.
  223. @param completion Optionally; the block invoked when the token is available. Invoked
  224. asynchronously on the main thread in the future.
  225. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  226. */
  227. - (void)getIDTokenWithCompletion:(nullable FIRAuthTokenCallback)completion
  228. FIR_SWIFT_NAME(getIDToken(completion:));
  229. /** @fn getTokenWithCompletion:
  230. @brief Please use @c getIDTokenWithCompletion: instead.
  231. @param completion Optionally; the block invoked when the token is available. Invoked
  232. asynchronously on the main thread in the future.
  233. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  234. */
  235. - (void)getTokenWithCompletion:(nullable FIRAuthTokenCallback)completion
  236. FIR_SWIFT_NAME(getToken(completion:)) __attribute__((deprecated));
  237. /** @fn getIDTokenForcingRefresh:completion:
  238. @brief Retrieves the Firebase authentication token, possibly refreshing it if it has expired.
  239. @param forceRefresh Forces a token refresh. Useful if the token becomes invalid for some reason
  240. other than an expiration.
  241. @param completion Optionally; the block invoked when the token is available. Invoked
  242. asynchronously on the main thread in the future.
  243. @remarks The authentication token will be refreshed (by making a network request) if it has
  244. expired, or if @c forceRefresh is YES.
  245. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  246. */
  247. - (void)getIDTokenForcingRefresh:(BOOL)forceRefresh
  248. completion:(nullable FIRAuthTokenCallback)completion;
  249. /** @fn getTokenForcingRefresh:completion:
  250. @brief Please use getIDTokenForcingRefresh:completion instead.
  251. @param forceRefresh Forces a token refresh. Useful if the token becomes invalid for some reason
  252. other than an expiration.
  253. @param completion Optionally; the block invoked when the token is available. Invoked
  254. asynchronously on the main thread in the future.
  255. @remarks The authentication token will be refreshed (by making a network request) if it has
  256. expired, or if @c forceRefresh is YES.
  257. @remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
  258. */
  259. - (void)getTokenForcingRefresh:(BOOL)forceRefresh
  260. completion:(nullable FIRAuthTokenCallback)completion
  261. __attribute__((deprecated));
  262. /** @fn linkWithCredential:completion:
  263. @brief Convenience method for @c linkAndRetrieveDataWithCredential:completion: This method
  264. doesn't return additional identity provider data.
  265. */
  266. - (void)linkWithCredential:(FIRAuthCredential *)credential
  267. completion:(nullable FIRAuthResultCallback)completion;
  268. /** @fn linkAndRetrieveDataWithCredential:completion:
  269. @brief Associates a user account from a third-party identity provider with this user and
  270. returns additional identity provider data.
  271. @param credential The credential for the identity provider.
  272. @param completion Optionally; the block invoked when the unlinking is complete, or fails.
  273. Invoked asynchronously on the main thread in the future.
  274. @remarks Possible error codes:
  275. <ul>
  276. <li>@c FIRAuthErrorCodeProviderAlreadyLinked - Indicates an attempt to link a provider of a
  277. type already linked to this account.
  278. </li>
  279. <li>@c FIRAuthErrorCodeCredentialAlreadyInUse - Indicates an attempt to link with a
  280. credential
  281. that has already been linked with a different Firebase account.
  282. </li>
  283. <li>@c FIRAuthErrorCodeOperationNotAllowed - Indicates that accounts with the identity
  284. provider represented by the credential are not enabled. Enable them in the Auth section
  285. of the Firebase console.
  286. </li>
  287. </ul>
  288. @remarks This method may also return error codes associated with updateEmail:completion: and
  289. updatePassword:completion: on FIRUser.
  290. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  291. */
  292. - (void)linkAndRetrieveDataWithCredential:(FIRAuthCredential *) credential
  293. completion:(nullable FIRAuthDataResultCallback) completion;
  294. /** @fn unlinkFromProvider:completion:
  295. @brief Disassociates a user account from a third-party identity provider with this user.
  296. @param provider The provider ID of the provider to unlink.
  297. @param completion Optionally; the block invoked when the unlinking is complete, or fails.
  298. Invoked asynchronously on the main thread in the future.
  299. @remarks Possible error codes:
  300. <ul>
  301. <li>@c FIRAuthErrorCodeNoSuchProvider - Indicates an attempt to unlink a provider
  302. that is not linked to the account.
  303. </li>
  304. <li>@c FIRAuthErrorCodeRequiresRecentLogin - Updating email is a security sensitive
  305. operation that requires a recent login from the user. This error indicates the user
  306. has not signed in recently enough. To resolve, reauthenticate the user by invoking
  307. reauthenticateWithCredential:completion: on FIRUser.
  308. </li>
  309. </ul>
  310. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  311. */
  312. - (void)unlinkFromProvider:(NSString *)provider
  313. completion:(nullable FIRAuthResultCallback)completion;
  314. /** @fn sendEmailVerificationWithCompletion:
  315. @brief Initiates email verification for the user.
  316. @param completion Optionally; the block invoked when the request to send an email verification
  317. is complete, or fails. Invoked asynchronously on the main thread in the future.
  318. @remarks Possible error codes:
  319. <ul>
  320. <li>@c FIRAuthErrorCodeInvalidRecipientEmail - Indicates an invalid recipient email was
  321. sent in the request.
  322. </li>
  323. <li>@c FIRAuthErrorCodeInvalidSender - Indicates an invalid sender email is set in
  324. the console for this action.
  325. </li>
  326. <li>@c FIRAuthErrorCodeInvalidMessagePayload - Indicates an invalid email template for
  327. sending update email.
  328. </li>
  329. <li>@c FIRAuthErrorCodeUserNotFound - Indicates the user account was not found.</li>
  330. </ul>
  331. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  332. */
  333. - (void)sendEmailVerificationWithCompletion:(nullable FIRSendEmailVerificationCallback)completion;
  334. /** @fn deleteWithCompletion:
  335. @brief Deletes the user account (also signs out the user, if this was the current user).
  336. @param completion Optionally; the block invoked when the request to delete the account is
  337. complete, or fails. Invoked asynchronously on the main thread in the future.
  338. @remarks Possible error codes:
  339. <ul>
  340. <li>@c FIRAuthErrorCodeRequiresRecentLogin - Updating email is a security sensitive
  341. operation that requires a recent login from the user. This error indicates the user
  342. has not signed in recently enough. To resolve, reauthenticate the user by invoking
  343. reauthenticateWithCredential:completion: on FIRUser.
  344. </li>
  345. </ul>
  346. @remarks See @c FIRAuthErrors for a list of error codes that are common to all FIRUser methods.
  347. */
  348. - (void)deleteWithCompletion:(nullable FIRUserProfileChangeCallback)completion;
  349. @end
  350. /** @class FIRUserProfileChangeRequest
  351. @brief Represents an object capable of updating a user's profile data.
  352. @remarks Properties are marked as being part of a profile update when they are set. Setting a
  353. property value to nil is not the same as leaving the property unassigned.
  354. */
  355. FIR_SWIFT_NAME(UserProfileChangeRequest)
  356. @interface FIRUserProfileChangeRequest : NSObject
  357. /** @fn init
  358. @brief Please use @c FIRUser.profileChangeRequest
  359. */
  360. - (instancetype)init NS_UNAVAILABLE;
  361. /** @property displayName
  362. @brief The user's display name.
  363. @remarks It is an error to set this property after calling
  364. @c FIRUserProfileChangeRequest.commitChangesWithCallback:
  365. */
  366. @property(nonatomic, copy, nullable) NSString *displayName;
  367. /** @property photoURL
  368. @brief The user's photo URL.
  369. @remarks It is an error to set this property after calling
  370. @c FIRUserProfileChangeRequest.commitChangesWithCallback:
  371. */
  372. @property(nonatomic, copy, nullable) NSURL *photoURL;
  373. /** @fn commitChangesWithCompletion:
  374. @brief Commits any pending changes.
  375. @remarks This method should only be called once. Once called, property values should not be
  376. changed.
  377. @param completion Optionally; the block invoked when the user profile change has been applied.
  378. Invoked asynchronously on the main thread in the future.
  379. */
  380. - (void)commitChangesWithCompletion:(nullable FIRUserProfileChangeCallback)completion;
  381. @end
  382. NS_ASSUME_NONNULL_END