Skip to content

Commit 351521f

Browse files
author
Hein
committed
Add mongodb crud auth backend
1 parent 757fbf3 commit 351521f

File tree

7 files changed

+154
-1
lines changed

7 files changed

+154
-1
lines changed
61.5 KB
Loading

images/usage/tools/CloudCode.png

42.1 KB
Loading
384 KB
Loading
402 KB
Loading

mint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@
225225
"usage/tools/powersync-dashboard",
226226
"usage/tools/cli",
227227
"usage/tools/diagnostic-app",
228-
"usage/tools/monitoring-and-alerting"
228+
"usage/tools/monitoring-and-alerting",
229+
"usage/tools/mongodb-crud-auth-backend"
229230
]
230231
}
231232
]

usage/tools.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ mode: wide
99
<Card title="CLI (Beta)" icon="terminal" href="/usage/tools/cli" horizontal/>
1010
<Card title="Diagnostics App" icon="stethoscope" href="https://github.com/powersync-ja/powersync-js/tree/main/tools/diagnostics-app" horizontal/>
1111
<Card title="Monitoring and Alerting" icon="bell" href="/usage/tools/monitoring-and-alerting" horizontal/>
12+
<Card title="MongoDB CRUD Auth Backend" icon="database" href="/usage/tools/mongodb-crud-auth-backend" horizontal/>
1213
</CardGroup>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
title: MongoDB CRUD Auth Backend
3+
description:
4+
---
5+
6+
This project provides a serverless backend API for a client application using PowerSync.
7+
8+
For more information about CloudCode serverless functions, please visit [this page](https://docs.journeyapps.com/reference/cloudcode/triggering-a-cloudcode-task/trigger-cc-via-http).
9+
10+
To view the serverless functions, select the **CloudCode** option at the top of the IDE.
11+
12+
<Frame>
13+
<img src="/images/usage/tools/CloudCode.png" />
14+
</Frame>
15+
16+
Here you will find four CloudCode tasks:
17+
<Frame>
18+
<img src="/images/usage/tools/CloudCode-tasks.png" />
19+
</Frame>
20+
21+
1. `generate_keys` - This is a task that can be used to generate a private/public key pair which the `jwks` and `token` tasks require.
22+
This task does not expose an HTTP endpoint and should only be used for development and getting started.
23+
2. `jwks` - This exposes an HTTP endpoint which has a `GET` function which returns the public JWKS details.
24+
3. `token` - This task exposes an HTTP endpoint which has a `GET` function. This tasks is used by a PowerSync client to generate a token to validate against the PowerSync Service.
25+
For more information about custom authentication setups for PowerSync, please see [this page](https://docs.powersync.com/installation/authentication-setup/custom) from the PowerSync docs.
26+
4. `upload` - This task exposes an HTTP endpoint which has a `POST` function which is used to process the write events from a PowerSync client and writes it back to the source MongoDB database.
27+
28+
## Setup
29+
30+
### 1. Generate key pair
31+
Before using the serverless functions you need to generate a public/private key pair. Do the following to generate the key pair:
32+
1. Open the `generate_keys` CloudCode task.
33+
2. Select the **Test CloudCode Task** button at the top right. This will print the public and private key in the task logs window.
34+
<Frame>
35+
<img src="/images/usage/tools/test-cloudcode-task.png" />
36+
</Frame>
37+
3. Copy and paste the `POWERSYNC_PUBLIC_KEY` and `POWERSYNC_PRIVATE_KEY` to a file — we'll need this in the next step.
38+
39+
<Note>
40+
This step is only meant for testing and development because the keys are shown in the logs files.
41+
For production, [generate a key pair locally](https://github.com/powersync-ja/powersync-jwks-example?tab=readme-ov-file#1-generate-a-key-pair) and move onto step 2 and 3.
42+
</Note>
43+
44+
### 2. Configure a deployment
45+
Before using the tasks, we need to configure a deployment.
46+
1. At the top of the IDE, select the `Deployments` option.
47+
2. Create a new deployment by selecting the `+` icon at the top right, _or_ use the default `Testing` deployment. You can configure different deployments for different environments.
48+
3. Now select the `Deployment settings` button for the instance.
49+
4. In the `Deployment settings` - `General` tab, capture a `Domain` value in the text field. The application will validate the domain name to make sure it's available.
50+
5. Select `Save`.
51+
6. Deploy the deployment: you can do so by selecting the `Deploy app` button, which can be found on the far right for each of the deployments you have configured. After the deployment is completed, it will take a few minutes for the domain to be available.
52+
7. Your new domain will be available at `<domain_name>.poweredbyjourney.com`. Open the browser and navigate to the new domain. You should be presented with `Cannot GET /`, because there is no index route.
53+
54+
### 3. Configure environment variables
55+
To add a new variable, do the following:
56+
1. Head over to the `Deployment settings` option again.
57+
2. Select the `Environment Variables` tab.
58+
<Frame>
59+
<img src="/images/usage/tools/cloudcode-envvar.png" />
60+
</Frame>
61+
3. Capture the variable name in the `Name` text field.
62+
4. Capture the variable value in the `Value` text field.
63+
5. (Suggested) Check the `Masked` checkbox to obfuscate the variable value for security purposes.
64+
6. Repeat until all the variables are added.
65+
66+
To finalize the setup, do the following:
67+
1. Select the `Save` button. This is important, otherwise the variables will not save.
68+
2. Deploy the deployment: you can do so by selecting the `Deploy app` button.
69+
70+
To wrap up the deployment, we need to configure the environment variables. The following variables need to be set on the deployment:
71+
* `POWERSYNC_PUBLIC_KEY` - This is the `POWERSYNC_PUBLIC_KEY` from the values generated in step 1.
72+
* `POWERSYNC_PRIVATE_KEY` - This is the `POWERSYNC_PRIVATE_KEY` from the values generated in step 1.
73+
* `MONGO_URI` - This is the MongoDB URI e.g. `mongodb+srv://<username>:<password>@<database_domain>/<database>`
74+
* `POWERSYNC_URL` - This is the public PowerSync URL that is provided after creating a new PowerSync instance.
75+
76+
### 4. Test
77+
Open your browser and navigate to `<domain_name>.poweredbyjourney.com/jwks`.
78+
You must set the JWKS url during to configure the PowerSync instance.
79+
If the setup was successful, the `jwks` task will render the keys in JSON format. Make sure the format of your JWKS keys matches the format [in this example](https://hlstmcktecziostiaplz.supabase.co/functions/v1/powersync-jwks) JWKS endpoint.
80+
81+
## Usage
82+
Make sure you've configured a deployment and set up environment variables as described in the **Setup** steps before before using the API.
83+
84+
### Token
85+
You would call the `token` HTTP API endpoint when you implement the `fetchCredentials()` function on the client application.
86+
Send a HTTP GET request to `<domain_name>.poweredbyjourney.com/token?user_id=<user_id>` to fetch a JWT for a user. You must provide a `user_id` in the query string of the request, as this is included in the JWT that is generated.
87+
88+
The response of the request would look like this:
89+
```json
90+
{"token":"..."}
91+
```
92+
93+
### JWKS
94+
The `jwks` HTTP API endpoint is used by PowerSync to validate the token returned from the `<domain_name>.poweredbyjourney.com/token` endpoint. This URL must be set in the configuration of your PowerSync instance.
95+
Send an HTTP GET request to `<domain_name>.poweredbyjourney.com/jwks`.
96+
97+
An example of the format can be found using [this link](https://hlstmcktecziostiaplz.supabase.co/functions/v1/powersync-jwks).
98+
99+
### Upload
100+
You would call the `upload` HTTP API endpoint when you implement the `uploadData()` function on the client application.
101+
Send an HTTP POST request to `<domain_name>.poweredbyjourney.com/upload`.
102+
The body of the payload should look like this:
103+
```
104+
{
105+
"op": "PUT",
106+
"table": "lists",
107+
"id": "61d19021-0565-4686-acc4-3ea4f8c48839",
108+
"data": {
109+
"created_at": "2024-10-31 10:33:24",
110+
"name": "Name",
111+
"owner_id": "8ea4310a-b7c0-4dd7-ae54-51d6e1596b83"
112+
}
113+
}
114+
```
115+
116+
The API will respond with HTTP status `200` if the write was successful.
117+
118+
## Modifying and making changes
119+
120+
If you need to make changes to the way the `upload` task writes data to the source MongoDB database, do the following:
121+
122+
1. Open the `CloudCode` section at the top of the IDE.
123+
2. Select and expand the `upload` task in the panel on the left.
124+
3. The `index.ts` contains the entry point function that accepts the HTTP request.
125+
4. The `persister.ts` file connects to the MongoDB database and writes the data to the MongoDB database. You can update this file to introduce your database schema, etc.
126+
127+
Example MongoDB database schema setup:
128+
```typescript
129+
/**
130+
* Line 13 in upload/persister.ts
131+
* Sample schema using to-do list demo. Update this based on your DB schema.
132+
*/
133+
export const schema = {
134+
lists: {
135+
_id: types.string,
136+
created_at: types.date,
137+
name: types.string,
138+
owner_id: types.string
139+
},
140+
todos: {
141+
_id: types.string,
142+
completed: types.boolean,
143+
created_at: types.date,
144+
created_by: types.string,
145+
description: types.string,
146+
list_id: types.string,
147+
completed_at: types.date,
148+
completed_by: types.string
149+
}
150+
};
151+
```

0 commit comments

Comments
 (0)