RLMUser.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import <Realm/RLMConstants.h>
  19. #import <Realm/RLMCredentials.h>
  20. #import <Realm/RLMRealmConfiguration.h>
  21. #import <Realm/RLMSyncConfiguration.h>
  22. @class RLMUser, RLMSyncSession, RLMRealm, RLMUserIdentity, RLMAPIKeyAuth, RLMMongoClient, RLMMongoDatabase, RLMMongoCollection, RLMUserProfile;
  23. @protocol RLMBSON;
  24. /**
  25. The state of the user object.
  26. */
  27. typedef NS_ENUM(NSUInteger, RLMUserState) {
  28. /// The user is logged out. Call `logInWithCredentials:...` with valid credentials to log the user back in.
  29. RLMUserStateLoggedOut,
  30. /// The user is logged in, and any Realms associated with it are syncing with Atlas App Services.
  31. RLMUserStateLoggedIn,
  32. /// The user has been removed, and cannot be used.
  33. RLMUserStateRemoved
  34. };
  35. /// A block type used to report an error related to a specific user.
  36. RLM_SWIFT_SENDABLE
  37. typedef void(^RLMOptionalUserBlock)(RLMUser * _Nullable, NSError * _Nullable);
  38. /// A block type used to report an error on a network request from the user.
  39. RLM_SWIFT_SENDABLE
  40. typedef void(^RLMUserOptionalErrorBlock)(NSError * _Nullable);
  41. /// A block which returns a dictionary should there be any custom data set for a user
  42. RLM_SWIFT_SENDABLE
  43. typedef void(^RLMUserCustomDataBlock)(NSDictionary * _Nullable, NSError * _Nullable);
  44. /// A block type for returning from function calls.
  45. RLM_SWIFT_SENDABLE
  46. typedef void(^RLMCallFunctionCompletionBlock)(id<RLMBSON> _Nullable, NSError * _Nullable);
  47. RLM_HEADER_AUDIT_BEGIN(nullability, sendability)
  48. /**
  49. A `RLMUser` instance represents a single Realm App user account.
  50. A user may have one or more credentials associated with it. These credentials
  51. uniquely identify the user to the authentication provider, and are used to sign
  52. into an Atlas App Services user account.
  53. Note that user objects are only vended out via SDK APIs, and cannot be directly
  54. initialized. User objects can be accessed from any thread.
  55. */
  56. RLM_SWIFT_SENDABLE RLM_FINAL // internally thread-safe
  57. @interface RLMUser : NSObject
  58. /**
  59. The unique Atlas App Services string identifying this user.
  60. Note this is different from an identity: A user may have multiple identities but has a single identifier. See RLMUserIdentity.
  61. */
  62. @property (nonatomic, readonly) NSString *identifier NS_SWIFT_NAME(id);
  63. /// Returns an array of identities currently linked to a user.
  64. @property (nonatomic, readonly) NSArray<RLMUserIdentity *> *identities;
  65. /**
  66. The user's refresh token used to access App Services.
  67. By default, refresh tokens expire 60 days after they are issued.
  68. You can configure this time for your App's refresh tokens to be
  69. anywhere between 30 minutes and 180 days.
  70. You can configure the refresh token expiration time for all sessions in
  71. an App from the Admin UI or Admin API.
  72. */
  73. @property (nullable, nonatomic, readonly) NSString *refreshToken;
  74. /**
  75. The user's access token used to access App Services.
  76. This is required to make HTTP requests to Atlas App Services like the Data API or GraphQL.
  77. It should be treated as sensitive data.
  78. The Realm SDK automatically manages access tokens and refreshes them
  79. when they expire.
  80. */
  81. @property (nullable, nonatomic, readonly) NSString *accessToken;
  82. /**
  83. The current state of the user.
  84. */
  85. @property (nonatomic, readonly) RLMUserState state;
  86. /**
  87. Indicates if the user is logged in or not. Returns true if the access token and refresh token are not empty.
  88. */
  89. @property (nonatomic, readonly) BOOL isLoggedIn;
  90. #pragma mark - Lifecycle
  91. /**
  92. Create a partition-based sync configuration instance for the given partition value.
  93. @param partitionValue The `RLMBSON` value the Realm is partitioned on.
  94. @return A default configuration object with the sync configuration set to use the given partition value.
  95. */
  96. - (RLMRealmConfiguration *)configurationWithPartitionValue:(nullable id<RLMBSON>)partitionValue NS_REFINED_FOR_SWIFT;
  97. /**
  98. Create a partition-based sync configuration instance for the given partition value.
  99. @param partitionValue The `RLMBSON` value the Realm is partitioned on.
  100. @param clientResetMode Determines file recovery behavior in the event of a client reset.
  101. See: https://docs.mongodb.com/realm/sync/error-handling/client-resets/
  102. @return A configuration object with the sync configuration set to use the given partition value.
  103. */
  104. - (RLMRealmConfiguration *)configurationWithPartitionValue:(nullable id<RLMBSON>)partitionValue
  105. clientResetMode:(RLMClientResetMode)clientResetMode NS_REFINED_FOR_SWIFT;
  106. /**
  107. Create a partition-based sync configuration instance for the given partition value.
  108. @param partitionValue The `RLMBSON` value the Realm is partitioned on.
  109. @param clientResetMode Determines file recovery behavior in the event of a client reset.
  110. See: https://docs.mongodb.com/realm/sync/error-handling/client-resets/
  111. @param beforeResetBlock A callback which notifies prior to a client reset occurring. See: `RLMClientResetBeforeBlock`
  112. @param afterResetBlock A callback which notifies after a client reset has occurred. See: `RLMClientResetAfterBlock`
  113. @return A configuration object with the sync configuration set to use the given partition value.
  114. */
  115. - (RLMRealmConfiguration *)configurationWithPartitionValue:(nullable id<RLMBSON>)partitionValue
  116. clientResetMode:(RLMClientResetMode)clientResetMode
  117. notifyBeforeReset:(nullable RLMClientResetBeforeBlock)beforeResetBlock
  118. notifyAfterReset:(nullable RLMClientResetAfterBlock)afterResetBlock NS_REFINED_FOR_SWIFT;
  119. /**
  120. Create a partition-based sync configuration instance for the given partition value.
  121. @param partitionValue The `RLMBSON` value the Realm is partitioned on.
  122. @param clientResetMode Determines file recovery behavior in the event of a client reset.
  123. See: https://docs.mongodb.com/realm/sync/error-handling/client-resets/
  124. @param manualClientResetHandler An error reporting block that is invoked during a client reset.
  125. @See ``RLMSyncErrorReportingBlock`` and ``RLMClientResetInfo``
  126. @return A configuration object with the sync configuration set to use the given partition value.
  127. */
  128. - (RLMRealmConfiguration *)configurationWithPartitionValue:(nullable id<RLMBSON>)partitionValue
  129. clientResetMode:(RLMClientResetMode)clientResetMode
  130. manualClientResetHandler:(nullable RLMSyncErrorReportingBlock)manualClientResetHandler NS_REFINED_FOR_SWIFT;
  131. /**
  132. Create a flexible sync configuration instance, which can be used to open a Realm that
  133. supports flexible sync.
  134. @note A single server-side Device Sync App can sync data with either partition-based realms or flexible sync based realms.
  135. In order for an application to contain both partition-based and flexible sync realms, more than one
  136. server-side Device Sync App must be used.
  137. @return A ``RLMRealmConfiguration`` instance with a flexible sync configuration.
  138. */
  139. - (RLMRealmConfiguration *)flexibleSyncConfiguration NS_REFINED_FOR_SWIFT;
  140. /**
  141. Create a flexible sync configuration instance, which can be used to open a Realm that
  142. supports flexible sync.
  143. @note A single server-side Device Sync App can sync data with either partition-based realms or flexible sync based realms.
  144. In order for an application to contain both partition-based and flexible sync realms, more than one
  145. server-side Device Sync App must be used.
  146. @param clientResetMode Determines file recovery behavior in the event of a client reset.
  147. See: https://docs.mongodb.com/realm/sync/error-handling/client-resets/
  148. @param beforeResetBlock A callback which notifies prior to a client reset occurring. See: `RLMClientResetBeforeBlock`
  149. @param afterResetBlock A callback which notifies after a client reset has occurred. See: `RLMClientResetAfterBlock`
  150. @return A `RLMRealmConfiguration` instance with a flexible sync configuration.
  151. */
  152. - (RLMRealmConfiguration *)flexibleSyncConfigurationWithClientResetMode:(RLMClientResetMode)clientResetMode
  153. notifyBeforeReset:(nullable RLMClientResetBeforeBlock)beforeResetBlock
  154. notifyAfterReset:(nullable RLMClientResetAfterBlock)afterResetBlock NS_REFINED_FOR_SWIFT;
  155. /**
  156. Create a flexible sync configuration instance, which can be used to open a Realm that
  157. supports flexible sync.
  158. @note A single server-side Device Sync App can sync data with either partition-based realms or flexible sync based realms.
  159. In order for an application to contain both partition-based and flexible sync realms, more than one
  160. server-side Device Sync App must be used.
  161. @param clientResetMode Determines file recovery behavior in the event of a client reset.
  162. See: https://docs.mongodb.com/realm/sync/error-handling/client-resets/
  163. @param manualClientResetHandler An error reporting block that is invoked during a client reset.
  164. @See `RLMSyncErrorReportingBlock` and `RLMClientResetInfo`
  165. @return A `RLMRealmConfiguration` instance with a flexible sync configuration.
  166. */
  167. - (RLMRealmConfiguration *)flexibleSyncConfigurationWithClientResetMode:(RLMClientResetMode)clientResetMode
  168. manualClientResetHandler:(nullable RLMSyncErrorReportingBlock)manualClientResetHandler NS_REFINED_FOR_SWIFT;
  169. /**
  170. Create a flexible sync configuration instance, which can be used to open a Realm that
  171. supports flexible sync.
  172. @note A single server-side Device Sync App can sync data with either partition-based realms or flexible sync based realms.
  173. In order for an application to contain both partition-based and flexible sync realms, more than one
  174. server-side Device Sync App must be used.
  175. @param initialSubscriptions A block which receives a subscription set instance, that can be
  176. used to add an initial set of subscriptions which will be executed
  177. when the Realm is first opened.
  178. @param rerunOnOpen If true, allows to run the initial set of subscriptions specified, on every app startup.
  179. This can be used to re-run dynamic time ranges and other queries that require a
  180. re-computation of a static variable.
  181. @return A `RLMRealmConfiguration` instance with a flexible sync configuration.
  182. */
  183. - (RLMRealmConfiguration *)flexibleSyncConfigurationWithInitialSubscriptions:(RLMFlexibleSyncInitialSubscriptionsBlock)initialSubscriptions
  184. rerunOnOpen:(BOOL)rerunOnOpen NS_REFINED_FOR_SWIFT;
  185. /**
  186. Create a flexible sync configuration instance, which can be used to open a Realm that
  187. supports flexible sync.
  188. @note A single server-side Device Sync App can sync data with either partition-based realms or flexible sync based realms.
  189. In order for an application to contain both partition-based and flexible sync realms, more than one
  190. server-side Device Sync App must be used.
  191. @param initialSubscriptions A block which receives a subscription set instance, that can be
  192. used to add an initial set of subscriptions which will be executed
  193. when the Realm is first opened.
  194. @param rerunOnOpen If true, allows to run the initial set of subscriptions specified, on every app startup.
  195. This can be used to re-run dynamic time ranges and other queries that require a
  196. re-computation of a static variable.
  197. @param clientResetMode Determines file recovery behavior in the event of a client reset.
  198. See: https://docs.mongodb.com/realm/sync/error-handling/client-resets/
  199. @param beforeResetBlock A callback which notifies prior to a client reset occurring. See: `RLMClientResetBeforeBlock`
  200. @param afterResetBlock A callback which notifies after a client reset has occurred. See: `RLMClientResetAfterBlock`
  201. @return A `RLMRealmConfiguration` instance with a flexible sync configuration.
  202. */
  203. - (RLMRealmConfiguration *)flexibleSyncConfigurationWithInitialSubscriptions:(RLMFlexibleSyncInitialSubscriptionsBlock)initialSubscriptions
  204. rerunOnOpen:(BOOL)rerunOnOpen
  205. clientResetMode:(RLMClientResetMode)clientResetMode
  206. notifyBeforeReset:(nullable RLMClientResetBeforeBlock)beforeResetBlock
  207. notifyAfterReset:(nullable RLMClientResetAfterBlock)afterResetBlock NS_REFINED_FOR_SWIFT;
  208. /**
  209. Create a flexible sync configuration instance, which can be used to open a Realm that
  210. supports flexible sync.
  211. @note A single server-side Device Sync App can sync data with either partition-based realms or flexible sync based realms.
  212. In order for an application to contain both partition-based and flexible sync realms, more than one
  213. server-side Device Sync App must be used.
  214. @param initialSubscriptions A block which receives a subscription set instance, that can be
  215. used to add an initial set of subscriptions which will be executed
  216. when the Realm is first opened.
  217. @param rerunOnOpen If true, allows to run the initial set of subscriptions specified, on every app startup.
  218. This can be used to re-run dynamic time ranges and other queries that require a
  219. re-computation of a static variable.
  220. @param clientResetMode Determines file recovery behavior in the event of a client reset.
  221. See: https://docs.mongodb.com/realm/sync/error-handling/client-resets/
  222. @param manualClientResetHandler An error reporting block that is invoked during a client reset.
  223. @See `RLMSyncErrorReportingBlock` and `RLMClientResetInfo`
  224. @return A `RLMRealmConfiguration` instance with a flexible sync configuration.
  225. */
  226. - (RLMRealmConfiguration *)flexibleSyncConfigurationWithInitialSubscriptions:(RLMFlexibleSyncInitialSubscriptionsBlock)initialSubscriptions
  227. rerunOnOpen:(BOOL)rerunOnOpen
  228. clientResetMode:(RLMClientResetMode)clientResetMode
  229. manualClientResetHandler:(nullable RLMSyncErrorReportingBlock)manualClientResetHandler NS_REFINED_FOR_SWIFT;
  230. #pragma mark - Sessions
  231. /**
  232. Retrieve a valid session object belonging to this user for a given URL, or `nil`
  233. if no such object exists.
  234. */
  235. - (nullable RLMSyncSession *)sessionForPartitionValue:(id<RLMBSON>)partitionValue;
  236. /// Retrieve all the valid sessions belonging to this user.
  237. @property (nonatomic, readonly) NSArray<RLMSyncSession *> *allSessions;
  238. #pragma mark - Custom Data
  239. /**
  240. The custom data of the user.
  241. This is configured in your Atlas App Services app.
  242. */
  243. @property (nonatomic, readonly) NSDictionary *customData NS_REFINED_FOR_SWIFT;
  244. /**
  245. The profile of the user.
  246. */
  247. @property (nonatomic, readonly) RLMUserProfile *profile;
  248. /**
  249. Refresh a user's custom data. This will, in effect, refresh the user's auth session.
  250. */
  251. - (void)refreshCustomDataWithCompletion:(RLMUserCustomDataBlock)completion;
  252. /**
  253. Links the currently authenticated user with a new identity, where the identity is defined by the credential
  254. specified as a parameter. This will only be successful if this `RLMUser` is the currently authenticated
  255. with the client from which it was created. On success a new user will be returned with the new linked credentials.
  256. @param credentials The `RLMCredentials` used to link the user to a new identity.
  257. @param completion The completion handler to call when the linking is complete.
  258. If the operation is successful, the result will contain a new
  259. `RLMUser` object representing the currently logged in user.
  260. */
  261. - (void)linkUserWithCredentials:(RLMCredentials *)credentials
  262. completion:(RLMOptionalUserBlock)completion NS_REFINED_FOR_SWIFT;
  263. /**
  264. Removes the user
  265. This logs out and destroys the session related to this user. The completion block will return an error
  266. if the user is not found or is already removed.
  267. @param completion A callback invoked on completion
  268. */
  269. - (void)removeWithCompletion:(RLMUserOptionalErrorBlock)completion;
  270. /**
  271. Permanently deletes this user from your Atlas App Services app.
  272. The users state will be set to `Removed` and the session will be destroyed.
  273. If the delete request fails, the local authentication state will be untouched.
  274. @param completion A callback invoked on completion
  275. */
  276. - (void)deleteWithCompletion:(RLMUserOptionalErrorBlock)completion;
  277. /**
  278. Logs out the current user
  279. The users state will be set to `Removed` is they are an anonymous user or `LoggedOut` if they are authenticated by an email / password or third party auth clients
  280. If the logout request fails, this method will still clear local authentication state.
  281. @param completion A callback invoked on completion
  282. */
  283. - (void)logOutWithCompletion:(RLMUserOptionalErrorBlock)completion;
  284. /**
  285. A client for the user API key authentication provider which
  286. can be used to create and modify user API keys.
  287. This client should only be used by an authenticated user.
  288. */
  289. @property (nonatomic, readonly) RLMAPIKeyAuth *apiKeysAuth;
  290. /// A client for interacting with a remote MongoDB instance
  291. /// @param serviceName The name of the MongoDB service
  292. - (RLMMongoClient *)mongoClientWithServiceName:(NSString *)serviceName NS_REFINED_FOR_SWIFT;
  293. /**
  294. Calls the Atlas App Services function with the provided name and arguments.
  295. @param name The name of the Atlas App Services function to be called.
  296. @param arguments The `BSONArray` of arguments to be provided to the function.
  297. @param completion The completion handler to call when the function call is complete.
  298. This handler is executed on a non-main global `DispatchQueue`.
  299. */
  300. - (void)callFunctionNamed:(NSString *)name
  301. arguments:(NSArray<id<RLMBSON>> *)arguments
  302. completionBlock:(RLMCallFunctionCompletionBlock)completion NS_REFINED_FOR_SWIFT;
  303. /// :nodoc:
  304. - (instancetype)init __attribute__((unavailable("RLMUser cannot be created directly")));
  305. /// :nodoc:
  306. + (instancetype)new __attribute__((unavailable("RLMUser cannot be created directly")));
  307. @end
  308. #pragma mark - User info classes
  309. /**
  310. An identity of a user. A user can have multiple identities, usually associated with multiple providers.
  311. Note this is different from a user's unique identifier string.
  312. @seeAlso `RLMUser.identifier`
  313. */
  314. RLM_SWIFT_SENDABLE RLM_FINAL // immutable final class
  315. @interface RLMUserIdentity : NSObject
  316. /**
  317. The associated provider type
  318. */
  319. @property (nonatomic, readonly) NSString *providerType;
  320. /**
  321. The string which identifies the RLMUserIdentity
  322. */
  323. @property (nonatomic, readonly) NSString *identifier;
  324. /**
  325. Initialize an RLMUserIdentity for the given identifier and provider type.
  326. @param providerType the associated provider type
  327. @param identifier the identifier of the identity
  328. */
  329. - (instancetype)initUserIdentityWithProviderType:(NSString *)providerType
  330. identifier:(NSString *)identifier;
  331. @end
  332. /**
  333. A profile for a given User.
  334. */
  335. RLM_SWIFT_SENDABLE RLM_FINAL // immutable final class
  336. @interface RLMUserProfile : NSObject
  337. /// The full name of the user.
  338. @property (nonatomic, readonly, nullable) NSString *name;
  339. /// The email address of the user.
  340. @property (nonatomic, readonly, nullable) NSString *email;
  341. /// A URL to the user's profile picture.
  342. @property (nonatomic, readonly, nullable) NSString *pictureURL;
  343. /// The first name of the user.
  344. @property (nonatomic, readonly, nullable) NSString *firstName;
  345. /// The last name of the user.
  346. @property (nonatomic, readonly, nullable) NSString *lastName;
  347. /// The gender of the user.
  348. @property (nonatomic, readonly, nullable) NSString *gender;
  349. /// The birthdate of the user.
  350. @property (nonatomic, readonly, nullable) NSString *birthday;
  351. /// The minimum age of the user.
  352. @property (nonatomic, readonly, nullable) NSString *minAge;
  353. /// The maximum age of the user.
  354. @property (nonatomic, readonly, nullable) NSString *maxAge;
  355. /// The BSON dictionary of metadata associated with this user.
  356. @property (nonatomic, readonly) NSDictionary *metadata NS_REFINED_FOR_SWIFT;
  357. @end
  358. RLM_HEADER_AUDIT_END(nullability, sendability)