FIRUser.h 22 KB

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