You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

495 lines
24 KiB

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