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.

490 lines
24 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
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 Renews the user's authentication tokens by validating a fresh set of credentials supplied
  181. by the user and returns additional identity provider data.
  182. @param credential A user-supplied credential, which will be validated by the server. This can be
  183. a successful third-party identity provider sign-in, or an email address and password.
  184. @param completion Optionally; the block invoked when the re-authentication operation has
  185. finished. Invoked asynchronously on the main thread in the future.
  186. @remarks If the user associated with the supplied credential is different from the current user,
  187. or if the validation of the supplied credentials fails; an error is returned and the current
  188. user remains signed in.
  189. @remarks Possible error codes:
  190. + `FIRAuthErrorCodeInvalidCredential` - Indicates the supplied credential is invalid.
  191. This could happen if it has expired or it is malformed.
  192. + `FIRAuthErrorCodeOperationNotAllowed` - Indicates that accounts with the
  193. identity provider represented by the credential are not enabled. Enable them in the
  194. Auth section of the Firebase console.
  195. + `FIRAuthErrorCodeEmailAlreadyInUse` - Indicates the email asserted by the credential
  196. (e.g. the email in a Facebook access token) is already in use by an existing account,
  197. that cannot be authenticated with this method. Call fetchProvidersForEmail for
  198. this users email and then prompt them to sign in with any of the sign-in providers
  199. returned. This error will only be thrown if the "One account per email address"
  200. setting is enabled in the Firebase console, under Auth settings. Please note that the
  201. error code raised in this specific situation may not be the same on Web and Android.
  202. + `FIRAuthErrorCodeUserDisabled` - Indicates the user's account is disabled.
  203. + `FIRAuthErrorCodeWrongPassword` - Indicates the user attempted reauthentication with
  204. an incorrect password, if credential is of the type EmailPasswordAuthCredential.
  205. + `FIRAuthErrorCodeUserMismatch` - Indicates that an attempt was made to
  206. reauthenticate with a user which is not the current user.
  207. + `FIRAuthErrorCodeInvalidEmail` - Indicates the email address is malformed.
  208. @remarks See `FIRAuthErrors` for a list of error codes that are common to all API methods.
  209. */
  210. - (void)reauthenticateWithCredential:(FIRAuthCredential *)credential
  211. completion:(nullable FIRAuthDataResultCallback)completion;
  212. /** @fn reauthenticateAndRetrieveDataWithCredential:completion:
  213. @brief Please use linkWithCredential:completion: for Objective-C
  214. or link(withCredential:completion:) for Swift instead.
  215. */
  216. - (void)reauthenticateAndRetrieveDataWithCredential:(FIRAuthCredential *)credential
  217. completion:(nullable FIRAuthDataResultCallback)completion
  218. DEPRECATED_MSG_ATTRIBUTE( "Please use reauthenticateWithCredential:completion: for"
  219. " Objective-C or reauthenticate(withCredential:completion:)"
  220. " for Swift instead.");
  221. /** @fn getIDTokenResultWithCompletion:
  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 `FIRAuthErrors` for a list of error codes that are common to all API methods.
  226. */
  227. - (void)getIDTokenResultWithCompletion:(nullable FIRAuthTokenResultCallback)completion
  228. NS_SWIFT_NAME(getIDTokenResult(completion:));
  229. /** @fn getIDTokenResultForcingRefresh:completion:
  230. @brief Retrieves the Firebase authentication token, possibly refreshing it if it has expired.
  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)getIDTokenResultForcingRefresh:(BOOL)forceRefresh
  240. completion:(nullable FIRAuthTokenResultCallback)completion
  241. NS_SWIFT_NAME(getIDTokenResult(forcingRefresh:completion:));
  242. /** @fn getIDTokenWithCompletion:
  243. @brief Retrieves the Firebase authentication token, possibly refreshing it if it has expired.
  244. @param completion Optionally; the block invoked when the token is available. Invoked
  245. asynchronously on the main thread in the future.
  246. @remarks See `FIRAuthErrors` for a list of error codes that are common to all API methods.
  247. */
  248. - (void)getIDTokenWithCompletion:(nullable FIRAuthTokenCallback)completion
  249. NS_SWIFT_NAME(getIDToken(completion:));
  250. /** @fn getIDTokenForcingRefresh:completion:
  251. @brief Retrieves the Firebase authentication token, possibly refreshing it if it has expired.
  252. @param forceRefresh Forces a token refresh. Useful if the token becomes invalid for some reason
  253. other than an expiration.
  254. @param completion Optionally; the block invoked when the token is available. Invoked
  255. asynchronously on the main thread in the future.
  256. @remarks The authentication token will be refreshed (by making a network request) if it has
  257. expired, or if `forceRefresh` is YES.
  258. @remarks See `FIRAuthErrors` for a list of error codes that are common to all API methods.
  259. */
  260. - (void)getIDTokenForcingRefresh:(BOOL)forceRefresh
  261. completion:(nullable FIRAuthTokenCallback)completion;
  262. /** @fn linkAndRetrieveDataWithCredential:completion:
  263. @brief Please use linkWithCredential:completion: for Objective-C
  264. or link(withCredential:completion:) for Swift instead.
  265. */
  266. - (void)linkAndRetrieveDataWithCredential:(FIRAuthCredential *)credential
  267. completion:(nullable FIRAuthDataResultCallback)completion
  268. DEPRECATED_MSG_ATTRIBUTE("Please use linkWithCredential:completion: for Objective-C "
  269. "or link(withCredential:completion:) for Swift instead.");
  270. /** @fn linkWithCredential:completion:
  271. @brief Associates a user account from a third-party identity provider with this user and
  272. returns additional identity provider data.
  273. @param credential The credential for the identity provider.
  274. @param completion Optionally; the block invoked when the unlinking is complete, or fails.
  275. Invoked asynchronously on the main thread in the future.
  276. @remarks Possible error codes:
  277. + `FIRAuthErrorCodeProviderAlreadyLinked` - Indicates an attempt to link a provider of a
  278. type already linked to this account.
  279. + `FIRAuthErrorCodeCredentialAlreadyInUse` - Indicates an attempt to link with a
  280. credential that has already been linked with a different Firebase account.
  281. + `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. @remarks This method may also return error codes associated with updateEmail:completion: and
  285. updatePassword:completion: on FIRUser.
  286. @remarks See `FIRAuthErrors` for a list of error codes that are common to all FIRUser methods.
  287. */
  288. - (void)linkWithCredential:(FIRAuthCredential *)credential
  289. completion:(nullable FIRAuthDataResultCallback)completion;
  290. /** @fn unlinkFromProvider:completion:
  291. @brief Disassociates a user account from a third-party identity provider with this user.
  292. @param provider The provider ID of the provider to unlink.
  293. @param completion Optionally; the block invoked when the unlinking is complete, or fails.
  294. Invoked asynchronously on the main thread in the future.
  295. @remarks Possible error codes:
  296. + `FIRAuthErrorCodeNoSuchProvider` - Indicates an attempt to unlink a provider
  297. that is not linked to the account.
  298. + `FIRAuthErrorCodeRequiresRecentLogin` - Updating email is a security sensitive
  299. operation that requires a recent login from the user. This error indicates the user
  300. has not signed in recently enough. To resolve, reauthenticate the user by invoking
  301. reauthenticateWithCredential:completion: on FIRUser.
  302. @remarks See `FIRAuthErrors` for a list of error codes that are common to all FIRUser methods.
  303. */
  304. - (void)unlinkFromProvider:(NSString *)provider
  305. completion:(nullable FIRAuthResultCallback)completion;
  306. /** @fn sendEmailVerificationWithCompletion:
  307. @brief Initiates email verification for the user.
  308. @param completion Optionally; the block invoked when the request to send an email verification
  309. is complete, or fails. Invoked asynchronously on the main thread in the future.
  310. @remarks Possible error codes:
  311. + `FIRAuthErrorCodeInvalidRecipientEmail` - Indicates an invalid recipient email was
  312. sent in the request.
  313. + `FIRAuthErrorCodeInvalidSender` - Indicates an invalid sender email is set in
  314. the console for this action.
  315. + `FIRAuthErrorCodeInvalidMessagePayload` - Indicates an invalid email template for
  316. sending update email.
  317. + `FIRAuthErrorCodeUserNotFound` - Indicates the user account was not found.
  318. @remarks See `FIRAuthErrors` for a list of error codes that are common to all FIRUser methods.
  319. */
  320. - (void)sendEmailVerificationWithCompletion:(nullable FIRSendEmailVerificationCallback)completion;
  321. /** @fn sendEmailVerificationWithActionCodeSettings:completion:
  322. @brief Initiates email verification for the user.
  323. @param actionCodeSettings An `FIRActionCodeSettings` object containing settings related to
  324. handling action codes.
  325. @remarks Possible error codes:
  326. + `FIRAuthErrorCodeInvalidRecipientEmail` - Indicates an invalid recipient email was
  327. sent in the request.
  328. + `FIRAuthErrorCodeInvalidSender` - Indicates an invalid sender email is set in
  329. the console for this action.
  330. + `FIRAuthErrorCodeInvalidMessagePayload` - Indicates an invalid email template for
  331. sending update email.
  332. + `FIRAuthErrorCodeUserNotFound` - Indicates the user account was not found.
  333. + `FIRAuthErrorCodeMissingIosBundleID` - Indicates that the iOS bundle ID is missing when
  334. a iOS App Store ID is provided.
  335. + `FIRAuthErrorCodeMissingAndroidPackageName` - Indicates that the android package name
  336. is missing when the `androidInstallApp` flag is set to true.
  337. + `FIRAuthErrorCodeUnauthorizedDomain` - Indicates that the domain specified in the
  338. continue URL is not whitelisted in the Firebase console.
  339. + `FIRAuthErrorCodeInvalidContinueURI` - Indicates that the domain specified in the
  340. continue URI is not valid.
  341. */
  342. - (void)sendEmailVerificationWithActionCodeSettings:(FIRActionCodeSettings *)actionCodeSettings
  343. completion:(nullable FIRSendEmailVerificationCallback)
  344. completion;
  345. /** @fn deleteWithCompletion:
  346. @brief Deletes the user account (also signs out the user, if this was the current user).
  347. @param completion Optionally; the block invoked when the request to delete the account is
  348. complete, or fails. Invoked asynchronously on the main thread in the future.
  349. @remarks Possible error codes:
  350. + `FIRAuthErrorCodeRequiresRecentLogin` - Updating email is a security sensitive
  351. operation that requires a recent login from the user. This error indicates the user
  352. has not signed in recently enough. To resolve, reauthenticate the user by invoking
  353. reauthenticateWithCredential:completion: on FIRUser.
  354. @remarks See `FIRAuthErrors` for a list of error codes that are common to all FIRUser methods.
  355. */
  356. - (void)deleteWithCompletion:(nullable FIRUserProfileChangeCallback)completion;
  357. @end
  358. /** @class FIRUserProfileChangeRequest
  359. @brief Represents an object capable of updating a user's profile data.
  360. @remarks Properties are marked as being part of a profile update when they are set. Setting a
  361. property value to nil is not the same as leaving the property unassigned.
  362. */
  363. NS_SWIFT_NAME(UserProfileChangeRequest)
  364. @interface FIRUserProfileChangeRequest : NSObject
  365. /** @fn init
  366. @brief Please use `FIRUser.profileChangeRequest`
  367. */
  368. - (instancetype)init NS_UNAVAILABLE;
  369. /** @property displayName
  370. @brief The user's display name.
  371. @remarks It is an error to set this property after calling
  372. `FIRUserProfileChangeRequest.commitChangesWithCallback:`
  373. */
  374. @property(nonatomic, copy, nullable) NSString *displayName;
  375. /** @property photoURL
  376. @brief The user's photo URL.
  377. @remarks It is an error to set this property after calling
  378. `FIRUserProfileChangeRequest.commitChangesWithCallback:`
  379. */
  380. @property(nonatomic, copy, nullable) NSURL *photoURL;
  381. /** @fn commitChangesWithCompletion:
  382. @brief Commits any pending changes.
  383. @remarks This method should only be called once. Once called, property values should not be
  384. changed.
  385. @param completion Optionally; the block invoked when the user profile change has been applied.
  386. Invoked asynchronously on the main thread in the future.
  387. */
  388. - (void)commitChangesWithCompletion:(nullable FIRUserProfileChangeCallback)completion;
  389. @end
  390. NS_ASSUME_NONNULL_END