@protobase/cli 0.62.3 → 0.62.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/package.js +1 -1
- package/package.json +7 -7
- package/skills/protobase/SKILL.md +1 -0
- package/skills/protobase/references/features/email-and-public-forms.md +2 -0
- package/skills/protobase/references/guides/send-from-gmail.md +68 -0
- package/skills/protobase/references/reference/auth.md +2 -2
- package/skills/protobase/references/reference/blocks.md +1 -1
- package/skills/protobase/references/reference/config/sign-in.md +13 -0
- package/skills/protobase/references/reference/email.md +55 -0
package/dist/cli/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.62.
|
|
3
|
+
"version": "0.62.5",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@better-auth/core": "^1.7.12",
|
|
44
44
|
"@hono/node-server": "^2.1.3",
|
|
45
|
-
"@protobase/config": "0.62.
|
|
46
|
-
"@protobase/layout": "0.62.
|
|
47
|
-
"@protobase/query": "0.62.
|
|
48
|
-
"@protobase/schema": "0.62.
|
|
49
|
-
"@protobase/server": "0.62.
|
|
50
|
-
"@protobase/ui": "0.62.
|
|
45
|
+
"@protobase/config": "0.62.5",
|
|
46
|
+
"@protobase/layout": "0.62.5",
|
|
47
|
+
"@protobase/query": "0.62.5",
|
|
48
|
+
"@protobase/schema": "0.62.5",
|
|
49
|
+
"@protobase/server": "0.62.5",
|
|
50
|
+
"@protobase/ui": "0.62.5",
|
|
51
51
|
"@react-email/render": "^2.1.0",
|
|
52
52
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
53
53
|
"@tailwindcss/vite": "^4.3.3",
|
|
@@ -57,6 +57,7 @@ Guides:
|
|
|
57
57
|
- When letting users save a list as Excel or CSV → read [Exporting a list](references/guides/export-a-list.md)
|
|
58
58
|
- When importing records from a spreadsheet → read [Importing records from a spreadsheet](references/guides/import-records.md)
|
|
59
59
|
- When telling users about a change, in the bell and as push on their browsers → read [Send notifications](references/guides/push-notifications.md)
|
|
60
|
+
- When sending mail from a person's own Gmail, such as an invoice with its PDF attached → read [Send mail from Gmail](references/guides/send-from-gmail.md)
|
|
60
61
|
- When replacing a resource's create form with a page of your own → read [A create page of your own](references/guides/create-page.md)
|
|
61
62
|
- When letting each person set a choice of their own, such as which emails they get → read [Add a personal setting](references/guides/personal-setting.md)
|
|
62
63
|
- When running the app on iOS or Android → read [A mobile app](references/guides/mobile-app.md)
|
|
@@ -7,6 +7,8 @@ Mail the app sends, and forms for people without an account.
|
|
|
7
7
|
- [Email](../reference/email.md#configuration): The app sends mail through one SMTP server or a mailer you supply.
|
|
8
8
|
- [Built-in emails](../reference/email.md#protobases-own-mail): Sign-in codes, password resets, invitations and mentions are sent in the app's name.
|
|
9
9
|
- [Email templates](../reference/email.md#templates): Your own emails are React components with typed data, sent from jobs, hooks or functions.
|
|
10
|
+
- [Attachments](../reference/email.md#attachments): A message carries files, such as an invoice's PDF, through the app's mail or someone's own mailbox.
|
|
11
|
+
- [Mail from your own mailbox](../reference/email.md#from-someones-own-mailbox) (beta): People link their Gmail on the account page, and the app's functions send mail from it, with a clear error when the link is missing or expired.
|
|
10
12
|
- [Public forms](../reference/forms.md#what-the-visitor-sees): A form at its own address lets people without an account create a record, such as a contact request.
|
|
11
13
|
- [Bot check](../reference/forms.md#bot-check): Each submission passes a check in the browser, without a third-party service or a puzzle.
|
|
12
14
|
- [Form rate limit](../reference/forms.md#rate-limit): Each client sends only a set number of submissions per minute.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from docs/website/src/content/docs/guides/send-from-gmail.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Send mail from Gmail
|
|
4
|
+
|
|
5
|
+
Set up a Google OAuth client so people link their Gmail on the account page, and send mail with attachments from it in an API function.
|
|
6
|
+
|
|
7
|
+
People of your app link their own Gmail once, on the account page, and your [API functions](api-functions.md) send mail from it, such as an invoice with its PDF attached. This guide sets up the Google side, turns it on in the config and sends a first message. [From someone's own mailbox](../reference/email.md#from-someones-own-mailbox) has the details.
|
|
8
|
+
|
|
9
|
+
## 1. Make the Google OAuth client
|
|
10
|
+
|
|
11
|
+
In the Google Cloud console, in a project of your own:
|
|
12
|
+
|
|
13
|
+
1. **Turn on the Gmail API:** APIs & Services → Library → Gmail API → Enable.
|
|
14
|
+
2. **Set up the consent screen** (Google Auth Platform → Branding and Audience): the app's name, support address and home page, and the user type **External**, so any Google account can link, not only those of your Workspace.
|
|
15
|
+
3. **Add the scope** (Data Access): `https://www.googleapis.com/auth/gmail.send`, beside `openid`, `email` and `profile`. It lets the app send mail as the person and nothing else: it cannot read their mailbox.
|
|
16
|
+
4. **Publish the app** (Audience → Publish app), so its publishing status is **In production**. Do not leave it in **Testing**: Google then gives refresh tokens that expire after 7 days, so every link stops working a week after it was made, and sending fails with `link_expired`.
|
|
17
|
+
5. **Make the client** (Clients → Create client): type **Web application**, with the authorized redirect URI `<url>/api/auth/callback/google`, such as `https://invoices.example.com/api/auth/callback/google`, and the same path on each of the app's [other origins](../reference/auth.md#several-origins) people link from. For local development add `http://localhost:<port>/api/auth/callback/google` too.
|
|
18
|
+
|
|
19
|
+
Keep the client ID and secret for the next step.
|
|
20
|
+
|
|
21
|
+
`gmail.send` is a sensitive scope: until Google has verified your app, the consent screen warns that the app is unverified, and at most 100 people can link. Submit the app for verification (Verification Center) before you open it to more.
|
|
22
|
+
|
|
23
|
+
## 2. Turn it on
|
|
24
|
+
|
|
25
|
+
Put the client's ID and secret in the app's environment, and name Gmail in `auth.personalMail`. Gmail links through Google sign-in, so `socialProviders` has `google`; it also offers "Continue with Google" on the sign-in page. `encryptOAuthTokens` stores the tokens encrypted with the app's secret:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
# .env
|
|
29
|
+
GOOGLE_CLIENT_ID=1234567890-abc.apps.googleusercontent.com
|
|
30
|
+
GOOGLE_CLIENT_SECRET=GOCSPX-...
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
// protobase.config.ts
|
|
35
|
+
export default defineConfig(({ env }) => ({
|
|
36
|
+
auth: {
|
|
37
|
+
socialProviders: { google: { clientId: env.GOOGLE_CLIENT_ID!, clientSecret: env.GOOGLE_CLIENT_SECRET! } },
|
|
38
|
+
personalMail: ['gmail'],
|
|
39
|
+
encryptOAuthTokens: true,
|
|
40
|
+
},
|
|
41
|
+
}))
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## 3. Link a mailbox
|
|
45
|
+
|
|
46
|
+
Signed in, open the account page (`/-/account`): under **Mailbox** it offers "Connect Gmail". Google asks which account to use and whether the app may send mail as it, then sends you back to the account page, which now says **Connected**. Someone who already signs in with Google still connects here, as their sign-in did not allow sending mail. The Google account may have another address than the one they sign in with.
|
|
47
|
+
|
|
48
|
+
## 4. Send from it
|
|
49
|
+
|
|
50
|
+
A protected function's context has the caller's mailbox as `personalMail`. Send a message with an attachment, and answer a missing or expired link with its own problem, which the function passes on as a `409` with its `code`:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// functions/send-invoice.ts
|
|
54
|
+
import { defineFunction } from '@protobase/server'
|
|
55
|
+
|
|
56
|
+
export default defineFunction(async (request, { personalMail }) => {
|
|
57
|
+
const { to, number, pdfBase64 } = (await request.json()) as { to: string; number: number; pdfBase64: string }
|
|
58
|
+
await personalMail.send({
|
|
59
|
+
to,
|
|
60
|
+
subject: `Invoice ${number}`,
|
|
61
|
+
text: 'Your invoice is attached.',
|
|
62
|
+
attachments: [{ filename: `invoice-${number}.pdf`, content: Buffer.from(pdfBase64, 'base64'), contentType: 'application/pdf' }],
|
|
63
|
+
})
|
|
64
|
+
return new Response(null, { status: 204 })
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The message goes out from the linked Gmail, and shows in its Sent folder. When the person linked nothing the call answers `409` with `code: "not_linked"`, and when Google no longer honours the link, as after they removed the app from their Google account, `code: "link_expired"`: point them to the account page to connect Gmail again.
|
|
@@ -189,12 +189,12 @@ Everyone signed in has an account page at `/-/account`, which "Account" in the u
|
|
|
189
189
|
|
|
190
190
|
| Tab | Address | Blocks |
|
|
191
191
|
| --- | --- | --- |
|
|
192
|
-
| Profile | `/-/account` | `Profile`, `DeleteAccount` |
|
|
192
|
+
| Profile | `/-/account` | `Profile`, `PersonalMail`, `DeleteAccount` |
|
|
193
193
|
| Sign-in & security | `/-/account/security` | `Password`, `Passkeys`, `TwoFactor`, `ConnectedAccounts` |
|
|
194
194
|
| Devices | `/-/account/devices` | `Devices` |
|
|
195
195
|
| Connected apps | `/-/account/apps` | `ConnectedApps` |
|
|
196
196
|
|
|
197
|
-
A tab whose blocks all leave themselves out is not offered, such as Connected apps in an app without [MCP](#oauth-for-mcp-clients). `DeleteAccount` leaves itself out when the app turns [account deletion](#deleting-the-account) off.
|
|
197
|
+
A tab whose blocks all leave themselves out is not offered, such as Connected apps in an app without [MCP](#oauth-for-mcp-clients). `DeleteAccount` leaves itself out when the app turns [account deletion](#deleting-the-account) off, and `PersonalMail` in an app without [personal mail](email.md#from-someones-own-mailbox).
|
|
198
198
|
|
|
199
199
|
To change it, export a page named `account` of your own: it replaces the standard one at `/-/account`, and is neither at `/account` nor in the sidebar. `accountTabs` holds the standard tabs by name (`profile`, `security`, `devices`, `apps`), so the page lists the ones it keeps beside its own:
|
|
200
200
|
|
|
@@ -390,7 +390,7 @@ Everyone who can sign in, for admins: find them by address, tick their roles, bl
|
|
|
390
390
|
|
|
391
391
|
### Account blocks
|
|
392
392
|
|
|
393
|
-
`Profile`, `Password`, `Passkeys`, `TwoFactor`, `ConnectedAccounts`, `Devices`, `ConnectedApps` and `DeleteAccount` are the parts of the signed-in person's own account: their name and address (which they can change, the address with an [emailed code](auth.md#changing-the-address)), their [password](auth.md#name-password-and-devices) (changed, or set for the first time), their [passkeys](auth.md#passkeys), [two-factor authentication](auth.md#two-factor-authentication), the sign-in providers they linked, the devices signed in as them (each one but this one to [sign out](auth.md#name-password-and-devices)), the apps such as MCP clients they allowed to act as them, below the app's MCP address to connect one, and the [deletion of their account](auth.md#deleting-the-account). Each takes no props or children, reads its own data, and leaves itself out where it does not apply: `ConnectedAccounts` without an OAuth sign-in provider, `ConnectedApps` without MCP, `DeleteAccount` with `auth.accountDeletion: false`. They act on whoever is signed in, so they are allowed on the page named `account` alone; anywhere else stops the config. That page is the [account page](auth.md#account-page) at `/-/account`, which `accountTabs` helps build from the standard tabs.
|
|
393
|
+
`Profile`, `Password`, `Passkeys`, `TwoFactor`, `ConnectedAccounts`, `PersonalMail`, `Devices`, `ConnectedApps` and `DeleteAccount` are the parts of the signed-in person's own account: their name and address (which they can change, the address with an [emailed code](auth.md#changing-the-address)), their [password](auth.md#name-password-and-devices) (changed, or set for the first time), their [passkeys](auth.md#passkeys), [two-factor authentication](auth.md#two-factor-authentication), the sign-in providers they linked, the [mailbox](email.md#from-someones-own-mailbox) they linked for the app to send mail from, the devices signed in as them (each one but this one to [sign out](auth.md#name-password-and-devices)), the apps such as MCP clients they allowed to act as them, below the app's MCP address to connect one, and the [deletion of their account](auth.md#deleting-the-account). Each takes no props or children, reads its own data, and leaves itself out where it does not apply: `ConnectedAccounts` without an OAuth sign-in provider, `PersonalMail` without `auth.personalMail`, `ConnectedApps` without MCP, `DeleteAccount` with `auth.accountDeletion: false`. They act on whoever is signed in, so they are allowed on the page named `account` alone; anywhere else stops the config. That page is the [account page](auth.md#account-page) at `/-/account`, which `accountTabs` helps build from the standard tabs.
|
|
394
394
|
|
|
395
395
|
```tsx
|
|
396
396
|
export const account = page('account',
|
|
@@ -128,6 +128,19 @@ export default defineConfig(({ env }) => ({
|
|
|
128
128
|
}))
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
+
## auth.personalMail
|
|
132
|
+
|
|
133
|
+
- **Type:** `'gmail'[]`
|
|
134
|
+
- **Default:** none
|
|
135
|
+
|
|
136
|
+
The mail providers people may link their own mailbox with, for the app's functions to send mail from it; each links through its sign-in provider in `socialProviders`, Gmail through `google`. See [From someone's own mailbox](../email.md#from-someones-own-mailbox).
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
export default defineConfig(({ env }) => ({
|
|
140
|
+
auth: { socialProviders: { google: { clientId: env.GOOGLE_CLIENT_ID!, clientSecret: env.GOOGLE_CLIENT_SECRET! } }, personalMail: ['gmail'] },
|
|
141
|
+
}))
|
|
142
|
+
```
|
|
143
|
+
|
|
131
144
|
## auth.encryptOAuthTokens
|
|
132
145
|
|
|
133
146
|
- **Type:** `boolean`
|
|
@@ -198,6 +198,61 @@ export default defineFunction({ roles: ['desk'] }, async (request, { records })
|
|
|
198
198
|
|
|
199
199
|
A message is sent once per call, so a job try that sends and then fails sends again: record what was sent, and check it first.
|
|
200
200
|
|
|
201
|
+
## Attachments
|
|
202
|
+
|
|
203
|
+
A message takes files beside its body, such as an invoice's PDF: `attachments`, each a `filename`, its bytes as `content` (a `Uint8Array`, such as a `Buffer`) and, optionally, its `contentType`, else the one the name implies. A template's `send` takes them as its fourth argument:
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
await invoiceEmail.send(client.email, { number: 7 }, mail, { attachments: [{ filename: 'invoice-7.pdf', content: pdf, contentType: 'application/pdf' }] })
|
|
207
|
+
await mail.send({ to: client.email, subject: 'Invoice 7', text: 'Attached.', attachments: [{ filename: 'invoice-7.pdf', content: pdf }] })
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
An attachment is only the bytes given: a file path or URL is never read in its place. A mailer of the project's own (`auth/mailer.ts`) gets them in `message.attachments`.
|
|
211
|
+
|
|
212
|
+
## From someone's own mailbox
|
|
213
|
+
|
|
214
|
+
:::caution[Beta]
|
|
215
|
+
Gmail is the only mail provider so far, and only API functions send from a mailbox: jobs cannot yet.
|
|
216
|
+
:::
|
|
217
|
+
|
|
218
|
+
An app can send mail from the signed-in person's own mailbox instead of the app's address, such as an invoice from the account holder's Gmail, so replies reach them. They link the mailbox once on the [account page](auth.md#account-page) ("Connect Gmail" under **Mailbox**), which asks the provider for leave to send mail and nothing else; the app's functions then send through it. `auth.personalMail` names the providers people may link, each through the app's sign-in provider of the same company; Gmail links through Google sign-in (see [Send mail from Gmail](../guides/send-from-gmail.md) for the Google setup):
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
export default defineConfig(({ env }) => ({
|
|
222
|
+
auth: {
|
|
223
|
+
socialProviders: { google: { clientId: env.GOOGLE_CLIENT_ID!, clientSecret: env.GOOGLE_CLIENT_SECRET! } },
|
|
224
|
+
personalMail: ['gmail'],
|
|
225
|
+
encryptOAuthTokens: true,
|
|
226
|
+
},
|
|
227
|
+
}))
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
A protected [API function](../guides/api-functions.md) gets the caller's mailbox as `personalMail`, a mailer like any other, so a template sends through it too:
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import { defineFunction } from '@protobase/server'
|
|
234
|
+
import { invoiceEmail } from '../emails/invoice'
|
|
235
|
+
|
|
236
|
+
export default defineFunction(async (request, { personalMail }) => {
|
|
237
|
+
const { to, number, pdf } = await readInvoice(request)
|
|
238
|
+
await invoiceEmail.send(to, { number }, personalMail, { attachments: [{ filename: `invoice-${number}.pdf`, content: pdf, contentType: 'application/pdf' }] })
|
|
239
|
+
return new Response(null, { status: 204 })
|
|
240
|
+
})
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
- **Tokens:** the link is the sign-in provider's account Better Auth keeps for the person, with its access and refresh tokens (encrypted with `encryptOAuthTokens: true`). An access token that has expired is refreshed before the message goes out, and the new one is stored.
|
|
244
|
+
- **When it cannot send:** `send` rejects with a `PersonalMailError` and sends nothing. Its `code` is `not_linked` when the person linked no mailbox, or signed in with Google without allowing it to send mail, and `link_expired` when the provider no longer honours the link, as when they revoked it or its refresh token expired: they connect it again. Thrown on, it answers the function's caller as a `409` problem with that `code` (`urn:protobase:problem:personal-mail-not-linked` or `-link-expired`), so the page can offer to link the mailbox:
|
|
245
|
+
|
|
246
|
+
```json
|
|
247
|
+
{ "type": "urn:protobase:problem:personal-mail-link-expired", "status": 409, "code": "link_expired", "provider": "gmail" }
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
- **Sender:** the provider sends as the linked address; Gmail sets the From header to it.
|
|
251
|
+
- **Only the person:** mail goes out from a mailbox only for someone signed in as themselves. Staff [signed in as them](auth.md#staff-sign-in), or an outside app acting for them, is refused with a `403` problem (`own-sign-in-only`), and nothing is sent.
|
|
252
|
+
- **Providers:** Gmail, through Google sign-in. A provider is added to Protobase behind the same `PersonalMailProvider` shape: its sign-in provider, the scopes linking asks for, and how it sends.
|
|
253
|
+
- <gap>Jobs cannot send from someone's own mailbox because a job's context has no personal mailer.</gap>
|
|
254
|
+
- <gap>Mailboxes other than Gmail cannot be linked because no other mail provider is implemented.</gap>
|
|
255
|
+
|
|
201
256
|
## In a bundle
|
|
202
257
|
|
|
203
258
|
A [bundle](cli.md#the-config-module) gets React and React Email's renderer from the serve runtime, as it gets `@protobase/server` and `@protobase/server/email`, so an email's component and the renderer share one React and hooks such as `useContext` work. React Email's other components are inlined, though `@protobase/server` uses them too: it loads them only to send its own mail, so the runtime does not supply them. Its `<Tailwind>` component needs `tailwindcss`, which the runtime does not supply, so a served config cannot use it: style with `style` props instead.
|