Skip to content

Commit

Permalink
Merge 98fb5fe into 167651f
Browse files Browse the repository at this point in the history
  • Loading branch information
takameyer committed Nov 15, 2023
2 parents 167651f + 98fb5fe commit 830a01c
Show file tree
Hide file tree
Showing 35 changed files with 475 additions and 302 deletions.
45 changes: 39 additions & 6 deletions examples/rn-connection-and-error/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ It specifically addresses the following points:
* Initial subscriptions are added to allow syncing of a subset of data to the device (i.e. the kiosks and products belonging to a specific store).
* The Realm is opened immediately without waiting for downloads from the server.
* See [Offline Support](#note-offline-support) below.
* Tying custom user data to document permissions.

### Note: Offline Support

Expand Down Expand Up @@ -142,8 +143,6 @@ The server will [reset the client](https://www.mongodb.com/docs/atlas/app-servic

In this demo app, a client reset is triggered by calling a [custom Atlas Function](#add-an-atlas-function) that deletes the client files for the current user. Another way to simulate a client reset is to terminate and re-enable Device Sync.

> ⚠️ At the time of writing (Realm JS version 12.2.0), pre and post client reset listeners are not fired as expected. Instead, the sync error callback is invoked with an error named `ClientReset`. This will be fixed as soon as possible.
### Logging and App Activity Monitoring

App Services logs all incoming requests and application events such as Device Sync operations and user authentication. In this demo app, we log messages to the `console` when certain changes and activities are detected, but you can replace the logger used with your preferred logging mechanism or service.
Expand All @@ -152,6 +151,13 @@ To modify the [log level and logger](https://www.mongodb.com/docs/realm/sdk/reac

For the App Services logs, you can also choose to [forward the logs to a service](https://www.mongodb.com/docs/atlas/app-services/activity/forward-logs/). To read more about monitoring app activity, please see the [docs](https://www.mongodb.com/docs/atlas/app-services/activity/).

### Tying Custom User Data to Schema Rules
When a new user is created, they are automatically attached to the first store in the collection (or a new one if it doesn't exist). The schemas for Store, Kiosk and Products have rules set to only be accessible if the users `storeId` field matches with the appropriate field in each model. There is a function that can be triggered to switch the store for the current user. Updating the custom user data and refreshing the session will create a client reset, which will automatically update the UI with the newly selected Store.

To perform this in the application, after creating a new user and viewing the store page, one can press `Trigger Store Change`, which will call the `switchStore` function. At this point the UI will not be updated, as a refresh of both the session and the current users custom data must be performed. To do this, press `Refresh Access Token/User Data`. The refresh of the session will update the rules in the backend and the refresh of the custom user data will update the UI with the correct store Id.

If the refresh session does not happen automatically, one can either press `Refresh Session` or click `Disconnect` followed by `Reconnect`. If this is done without refreshing the user data, no store will be shown as the rules will be updated to the new store, but the UI will not be updated with the new store Id.

## Getting Started

### Prerequisites
Expand Down Expand Up @@ -204,19 +210,46 @@ To sync data used in this app you must first:

> If you set up your App Services App [via a CLI](#via-a-cli-recommended), you can **skip this step** as these functions should already be defined for you.
We will add a function for forcing a client reset. The function is solely used for demo purposes and should not be used in production. We will also add a function to be run on user creation that adds fields to the user's [custom user data](https://www.mongodb.com/docs/atlas/app-services/users/custom-metadata/) document.
We will add functions for the following:
* Forcing a client reset.
* This function is solely used for demo purposes and should not be used in production.
* Setting the default `storeId` for a user on creation on the associated [custom user data](https://www.mongodb.com/docs/atlas/app-services/users/custom-metadata/) document.
* An [authentication trigger](https://www.mongodb.com/docs/atlas/app-services/triggers/authentication-triggers/) will need to be created and configured to call this function.
* Switching the associated Store for the current user.
* Creating a new store document and getting all store documents.
* These are used to check if the demo stores already exist and create them if not.
* The functions need to be system calls, as the associated user will not have permissions to read or write any other store.

To set this up via the App Services UI:

1. [Define two functions](https://www.mongodb.com/docs/atlas/app-services/functions/#define-a-function) with the following configurations:
1. [Define five functions](https://www.mongodb.com/docs/atlas/app-services/functions/#define-a-function) with the following configurations:
* Function name: `triggerClientReset`
* Authentication: `System`
* Private: `false`
* Code: See [backend function](./backend/functions/triggerClientReset.js)
* Function name: `onUserCreation`
* Function name: `setUserDefaultStoreId`
* Authentication: `Application Authentication`
* Private: `false`
* Code: See [backend function](./backend/functions/onUserCreation.js)
* Code: See [backend function](./backend/functions/setUserDefaultStoreId.js)
* Function name: `switchStore`
* Authentication: `Application Authentication`
* Private: `false`
* Code: See [backend function](./backend/functions/switchStore.js)
* Function name: `createNewStore`
* Authentication: `System`
* Private: `true`
* Code: See [backend function](./backend/functions/createNewStore.js)
* Function name: `getAllStores`
* Authentication: `Application Authentication`
* Private: `true`
* Code: See [backend function](./backend/functions/getAllStores.js)
2. [Define an authentication trigger](https://www.mongodb.com/docs/atlas/app-services/triggers/authentication-triggers/#create-an-authentication-trigger)
* Trigger type: `Authentication`
* Trigger name: `onUserCreation`
* Action type: `Create`
* Providers: `Email/Password`
* EventType: `Function`
* Function: `setUserDefaultStoreId`

### Install Dependencies

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"enabled": true,
"mongo_service_name": "mongodb-atlas",
"database_name": "AuthExample",
"database_name": "StoreDemo",
"collection_name": "Users",
"user_id_field": "user_id",
"on_user_creation_function_name": "onUserCreation"
"user_id_field": "userId",
"on_user_creation_function_name": "setUserDefaultStoreId"
}
4 changes: 2 additions & 2 deletions examples/rn-connection-and-error/backend/auth/providers.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"local-userpass": {
"name": "local-userpass",
"type": "local-userpass",
"disabled": false,
"config": {
"autoConfirm": true,
"resetPasswordUrl": "https://",
"runConfirmationFunction": false,
"runResetFunction": false
},
"disabled": false
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"products": {
"ref": "#/relationship/mongodb-atlas/sync/Product",
"ref": "#/relationship/mongodb-atlas/StoreDemo/Product",
"source_key": "products",
"foreign_key": "_id",
"is_list": true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"database": "StoreDemo",
"collection": "Kiosk",
"roles": [
{
"name": "readAndWriteAll",
"apply_when": {},
"document_filters": {
"read": {
"storeId": "%%user.custom_data.storeId"
},
"write": {
"storeId": "%%user.custom_data.storeId"
}
},
"insert": true,
"delete": true,
"search": true,
"read": true,
"write": true
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"bsonType": "object",
"title": "Kiosk",
"type": "object",
"required": [
"_id",
"storeId"
],
"properties": {
"_id": {
"bsonType": "objectId"
Expand All @@ -13,10 +18,5 @@
"storeId": {
"bsonType": "objectId"
}
},
"required": [
"_id",
"storeId"
],
"title": "Kiosk"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"database": "StoreDemo",
"collection": "Product",
"roles": [
{
"name": "readAndWriteAll",
"apply_when": {},
"document_filters": {
"read": {
"storeId": "%%user.custom_data.storeId"
},
"write": {
"storeId": "%%user.custom_data.storeId"
}
},
"insert": true,
"delete": true,
"search": true,
"read": true,
"write": true
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"bsonType": "object",
"title": "Product",
"type": "object",
"required": [
"_id",
"name",
"numInStock",
"price",
"storeId"
],
"properties": {
"_id": {
"bsonType": "objectId"
Expand All @@ -16,13 +24,5 @@
"storeId": {
"bsonType": "objectId"
}
},
"required": [
"_id",
"storeId",
"name",
"price",
"numInStock"
],
"title": "Product"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"kiosks": {
"ref": "#/relationship/mongodb-atlas/sync/Kiosk",
"ref": "#/relationship/mongodb-atlas/StoreDemo/Kiosk",
"source_key": "kiosks",
"foreign_key": "_id",
"is_list": true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"database": "StoreDemo",
"collection": "Store",
"roles": [
{
"name": "readAndWriteAll",
"apply_when": {},
"document_filters": {
"read": {
"_id": "%%user.custom_data.storeId"
},
"write": {
"_id": "%%user.custom_data.storeId"
}
},
"insert": true,
"delete": true,
"search": true,
"read": true,
"write": true
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"bsonType": "object",
"title": "Store",
"type": "object",
"required": [
"_id"
],
"properties": {
"_id": {
"bsonType": "objectId"
Expand All @@ -10,9 +14,5 @@
"bsonType": "objectId"
}
}
},
"required": [
"_id"
],
"title": "Store"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"database": "StoreDemo",
"collection": "Users",
"roles": [
{
"name": "ThisUser",
"apply_when": {
"userId": "%%user.id"
},
"document_filters": {
"read": true,
"write": true
},
"insert": true,
"delete": true,
"search": true,
"read": true,
"write": true
}
]
}
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
{
"roles": [
{
"name": "readAndWriteAll",
"apply_when": {},
"document_filters": {
"write": true,
"read": true
},
"read": true,
"write": true,
"insert": true,
"delete": true,
"search": true
},
{
"name": "readAll",
"apply_when": {},
"document_filters": {
"write": false,
"read": true
},
"read": true,
"write": false,
"insert": false,
"delete": false,
"search": true
}
]
}
{}

This file was deleted.

This file was deleted.

0 comments on commit 830a01c

Please sign in to comment.