@stacksjs/defaults 0.75.661 → 0.75.667
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/ai/skills/stacks-actions/SKILL.md +1 -1
- package/ai/skills/stacks-auth/SKILL.md +1 -0
- package/ai/skills/stacks-composables/SKILL.md +6 -5
- package/ai/skills/stacks-dashboard/SKILL.md +2 -2
- package/ai/skills/stacks-logging/SKILL.md +5 -1
- package/ai/skills/stacks-models/SKILL.md +7 -0
- package/ai/skills/stacks-native/CATALOG.md +27 -25
- package/ai/skills/stacks-newsletter/SKILL.md +51 -0
- package/ai/skills/stacks-payments/SKILL.md +13 -0
- package/ai/skills/stacks-scheduler/SKILL.md +10 -0
- package/ai/skills/stacks-socials/SKILL.md +20 -2
- package/ai/skills/stacks-testing/SKILL.md +11 -0
- package/app/Actions/ConfirmSubscriptionAction.ts +53 -0
- package/app/Actions/SubscriberEmailAction.ts +123 -40
- package/app/Actions/UnsubscribeAction.ts +5 -24
- package/app/Actions/form-return.ts +65 -0
- package/app/Actions/subscription-page.ts +50 -0
- package/app/Mail/SubscriptionConfirmRequest.ts +56 -0
- package/app/Mail/SubscriptionConfirmation.ts +46 -6
- package/app/Models/FailedJob.ts +1 -1
- package/app/Models/Referral.ts +1 -1
- package/app/Models/commerce/Auction.ts +2 -2
- package/app/Models/commerce/AuctionItem.ts +1 -1
- package/app/Models/commerce/Bid.ts +1 -1
- package/app/Models/commerce/WaitlistProduct.ts +3 -3
- package/ide/vscode/package.json +1 -1
- package/package.json +7 -7
- package/project/storage/framework/types/src/auth.ts +3 -1
- package/resources/assets/scripts/main.ts +5 -6
- package/resources/components/Marketing/ComingSoon.stx +3 -101
- package/resources/components/Marketing/SubscribeForm.stx +315 -0
- package/resources/emails/subscription-confirm.stx +31 -0
- package/resources/emails/subscription-confirmation.stx +23 -88
- package/routes/dashboard.ts +2 -0
- package/scaffold/config/server.ts +141 -0
|
@@ -1,60 +1,143 @@
|
|
|
1
1
|
import { Action } from '@stacksjs/actions/runtime'
|
|
2
|
-
import {
|
|
2
|
+
import { emailAddress, NewsletterListUnavailableError, subscribe } from '@stacksjs/newsletter'
|
|
3
|
+
import { SubscriberEmail } from '@stacksjs/orm'
|
|
3
4
|
import { rateLimit } from '@stacksjs/router'
|
|
5
|
+
import { sendSubscriptionConfirmRequest } from '../Mail/SubscriptionConfirmRequest'
|
|
4
6
|
import { sendSubscriptionConfirmation } from '../Mail/SubscriptionConfirmation'
|
|
7
|
+
import { formReturnLocation, wantsFormRedirect } from './form-return'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The public newsletter signup, `POST /api/email/subscribe`, behind every
|
|
11
|
+
* `<SubscribeForm>` and any hand-written form.
|
|
12
|
+
*
|
|
13
|
+
* It goes through `@stacksjs/newsletter`'s `subscribe()`, so a signup joins a
|
|
14
|
+
* list (the default list, or a public one the form names in `list`) and
|
|
15
|
+
* campaigns reach it. It used to write only a `subscribers` row and join no
|
|
16
|
+
* list at all, so nobody who signed up on a website ever got a campaign.
|
|
17
|
+
*
|
|
18
|
+
* Fields: `email`, `source` (where they signed up, stored on the row), `list`
|
|
19
|
+
* (a public list's slug; private lists are refused), `return` (where a plain
|
|
20
|
+
* form post comes back to).
|
|
21
|
+
*
|
|
22
|
+
* What a signup came to. A plain form gets it back as `?subscribe=<outcome>`;
|
|
23
|
+
* a script gets the JSON beside it.
|
|
24
|
+
*
|
|
25
|
+
* - `subscribed`: a new address, or one that had unsubscribed and came back.
|
|
26
|
+
* - `pending`: the list has double opt-in on; a confirmation email went out
|
|
27
|
+
* and the address joins once its link is opened (ConfirmSubscriptionAction).
|
|
28
|
+
* - `already`: the address is on the list.
|
|
29
|
+
* - `invalid`: not an email address.
|
|
30
|
+
* - `busy`: too many attempts from this visitor in a minute.
|
|
31
|
+
* - `error`: anything else. Logged here; the visitor is asked to try again.
|
|
32
|
+
*/
|
|
33
|
+
type Outcome = 'subscribed' | 'pending' | 'already' | 'invalid' | 'busy' | 'error'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The tables this action writes, most of them through @stacksjs/newsletter
|
|
37
|
+
* rather than by model name. Stated here because frameworkRouteAdmission
|
|
38
|
+
* decides from this file's own source whether the route can be mounted: in an
|
|
39
|
+
* app without these tables the route is withheld instead of failing per
|
|
40
|
+
* request.
|
|
41
|
+
*/
|
|
42
|
+
export const subscriberTables = ['subscribers', 'email_lists', 'email_list_subscribers', 'subscriber_emails'] as const
|
|
5
43
|
|
|
6
44
|
export default new Action({
|
|
7
45
|
name: 'SubscriberEmailAction',
|
|
8
|
-
description: '
|
|
46
|
+
description: 'Subscribe an address to a newsletter list and send the welcome email',
|
|
9
47
|
method: 'POST',
|
|
10
48
|
|
|
11
49
|
async handle(request: RequestInstance) {
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const source = request.get('source') || 'homepage'
|
|
20
|
-
|
|
21
|
-
if (!email || !email.includes('@')) {
|
|
22
|
-
return { success: false, message: 'A valid email is required' }
|
|
23
|
-
}
|
|
50
|
+
// A plain <form> post goes back to its page with the outcome in the query
|
|
51
|
+
// string (see ./form-return.ts), so a visitor never lands on raw JSON or
|
|
52
|
+
// an error screen. A script keeps getting JSON, and errors, as before.
|
|
53
|
+
const backToForm = wantsFormRedirect(request.headers)
|
|
54
|
+
const answer = (outcome: Outcome, json: Record<string, unknown>) => backToForm
|
|
55
|
+
? response.redirect(formReturnLocation({ target: request.get('return'), referer: request.headers.get('referer'), param: 'subscribe', outcome }), 303)
|
|
56
|
+
: json
|
|
24
57
|
|
|
25
|
-
// Check if subscriber already exists
|
|
26
|
-
const existingSubscriber = await Subscriber.where('email', email).first()
|
|
27
|
-
if (existingSubscriber) {
|
|
28
|
-
return { success: true, message: 'Already subscribed' }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Create subscriber record. The check above is a fast path; two requests
|
|
32
|
-
// for the same email can still both pass it and race into the
|
|
33
|
-
// subscribers.email unique index (#1957). Treat the loser's unique
|
|
34
|
-
// violation as the same "already subscribed" success — the response
|
|
35
|
-
// contract (and its enumeration semantics) is unchanged.
|
|
36
|
-
let subscriber: any
|
|
37
58
|
try {
|
|
38
|
-
|
|
59
|
+
// Per-IP throttle. The endpoint is unauthenticated and skipCsrf'd so
|
|
60
|
+
// bots will find it; without this they can flood the subscribers
|
|
61
|
+
// table within minutes and burn through SES/SendGrid quota. 10/min
|
|
62
|
+
// is generous for a real human filling the same form repeatedly.
|
|
63
|
+
await rateLimit('email-subscribe', 10).per('minute')
|
|
39
64
|
}
|
|
40
65
|
catch (err) {
|
|
41
|
-
if (
|
|
42
|
-
return
|
|
66
|
+
if (backToForm && (err as { status?: number })?.status === 429)
|
|
67
|
+
return answer('busy', {})
|
|
43
68
|
throw err
|
|
44
69
|
}
|
|
45
70
|
|
|
46
|
-
|
|
47
|
-
|
|
71
|
+
const email = emailAddress(request.get('email'))
|
|
72
|
+
const source = String(request.get('source') || 'homepage').slice(0, 100)
|
|
73
|
+
const list = String(request.get('list') || '').trim() || undefined
|
|
74
|
+
|
|
75
|
+
if (!email)
|
|
76
|
+
return answer('invalid', { success: false, message: 'A valid email is required' })
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
// `confirmation: 'list'`: a list with double opt-in on holds the address
|
|
80
|
+
// as pending until it confirms; any other list subscribes it at once.
|
|
81
|
+
const result = await subscribe(email, { list, source, publicOnly: true, confirmation: 'list' })
|
|
82
|
+
|
|
83
|
+
if (result.pending) {
|
|
84
|
+
// New, returning, or asking again while still pending: (re)send the
|
|
85
|
+
// confirmation email. The rate limit above bounds how often.
|
|
86
|
+
if (result.created || result.reactivated)
|
|
87
|
+
await SubscriberEmail.create({ email, source, subscriber_id: result.subscriberId })
|
|
88
|
+
confirmRequest(email, result.token)
|
|
89
|
+
return answer('pending', { success: true, pending: true, message: 'Confirmation required' })
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (!result.created && !result.reactivated)
|
|
93
|
+
return answer('already', { success: true, message: 'Already subscribed' })
|
|
94
|
+
|
|
95
|
+
// The signup log, tied to the person it belongs to.
|
|
96
|
+
await SubscriberEmail.create({ email, source, subscriber_id: result.subscriberId })
|
|
97
|
+
|
|
98
|
+
welcome(email, result.token)
|
|
99
|
+
|
|
100
|
+
return answer('subscribed', {
|
|
101
|
+
success: true,
|
|
102
|
+
message: result.reactivated ? 'Subscribed again' : 'Subscribed',
|
|
103
|
+
subscriber: { email, listId: result.listId },
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
// A form naming a list that is missing or private: say so plainly, as
|
|
108
|
+
// a 422, rather than a 500 carrying the internal error's name.
|
|
109
|
+
if (err instanceof NewsletterListUnavailableError) {
|
|
110
|
+
console.error('[subscribe] signup refused:', err.message)
|
|
111
|
+
return backToForm
|
|
112
|
+
? answer('error', {})
|
|
113
|
+
: response.json({ success: false, error: 'list_unavailable', message: 'That list is not open for signups.' }, 422)
|
|
114
|
+
}
|
|
48
115
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
to: email,
|
|
52
|
-
subscriberUuid: subscriber.uuid,
|
|
53
|
-
}).catch((err: unknown) => {
|
|
54
|
-
const message = err instanceof Error ? err.message : String(err)
|
|
55
|
-
console.error(`Failed to send confirmation email to ${email}:`, message)
|
|
56
|
-
})
|
|
116
|
+
if (!backToForm)
|
|
117
|
+
throw err
|
|
57
118
|
|
|
58
|
-
|
|
119
|
+
console.error('[subscribe] signup failed:', err instanceof Error ? err.message : String(err))
|
|
120
|
+
return answer('error', {})
|
|
121
|
+
}
|
|
59
122
|
},
|
|
60
123
|
})
|
|
124
|
+
|
|
125
|
+
/** Send the double opt-in email without holding up the response; a failure is logged. */
|
|
126
|
+
function confirmRequest(to: string, token: string): void {
|
|
127
|
+
sendSubscriptionConfirmRequest({ to, token }).catch((err: unknown) => {
|
|
128
|
+
const message = err instanceof Error ? err.message : String(err)
|
|
129
|
+
console.error(`Failed to send the confirmation request to ${to}:`, message)
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Send the welcome email without holding up the response; a failure is
|
|
135
|
+
* logged. `token` is the list membership's own, so the unsubscribe link in
|
|
136
|
+
* the email leaves that list and nothing else.
|
|
137
|
+
*/
|
|
138
|
+
function welcome(to: string, token: string): void {
|
|
139
|
+
sendSubscriptionConfirmation({ to, token }).catch((err: unknown) => {
|
|
140
|
+
const message = err instanceof Error ? err.message : String(err)
|
|
141
|
+
console.error(`Failed to send confirmation email to ${to}:`, message)
|
|
142
|
+
})
|
|
143
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { RequestInstance } from '@stacksjs/types'
|
|
2
2
|
import { Action } from '@stacksjs/actions/runtime'
|
|
3
|
+
import { unsubscribeAll } from '@stacksjs/newsletter'
|
|
3
4
|
import { EmailList, EmailListSubscriber, Subscriber } from '@stacksjs/orm'
|
|
5
|
+
import { subscriptionPage as unsubscribePage } from './subscription-page'
|
|
4
6
|
|
|
5
7
|
export default new Action({
|
|
6
8
|
name: 'UnsubscribeAction',
|
|
@@ -68,6 +70,9 @@ export default new Action({
|
|
|
68
70
|
status: 'unsubscribed',
|
|
69
71
|
unsubscribed_at: new Date().toISOString(),
|
|
70
72
|
})
|
|
73
|
+
// The global link means every list. Campaigns send to list memberships,
|
|
74
|
+
// so marking only the subscriber row left them all still receiving.
|
|
75
|
+
await unsubscribeAll(subscriber.email)
|
|
71
76
|
|
|
72
77
|
return new Response(unsubscribePage('Unsubscribed', `${subscriber.email} has been successfully unsubscribed. You will no longer receive emails from us.`, true), {
|
|
73
78
|
headers: { 'Content-Type': 'text/html' },
|
|
@@ -82,27 +87,3 @@ export default new Action({
|
|
|
82
87
|
}
|
|
83
88
|
},
|
|
84
89
|
})
|
|
85
|
-
|
|
86
|
-
function unsubscribePage(title: string, message: string, success: boolean): string {
|
|
87
|
-
const icon = success ? '✓' : '✗'
|
|
88
|
-
const iconColor = success ? '#22c55e' : '#ef4444'
|
|
89
|
-
|
|
90
|
-
return `<!DOCTYPE html>
|
|
91
|
-
<html lang="en">
|
|
92
|
-
<head>
|
|
93
|
-
<meta charset="UTF-8">
|
|
94
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
95
|
-
<title>${title}</title>
|
|
96
|
-
</head>
|
|
97
|
-
<body style="margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; background-color: #0e0e0e; color: #d4d4d4; display: flex; align-items: center; justify-content: center; min-height: 100vh;">
|
|
98
|
-
<div style="max-width: 480px; margin: 0 auto; padding: 40px 20px; text-align: center;">
|
|
99
|
-
<div style="width: 64px; height: 64px; border-radius: 50%; background-color: ${iconColor}20; display: inline-flex; align-items: center; justify-content: center; margin-bottom: 24px;">
|
|
100
|
-
<span style="font-size: 28px; color: ${iconColor};">${icon}</span>
|
|
101
|
-
</div>
|
|
102
|
-
<h1 style="color: #ececec; font-size: 24px; font-weight: 600; margin: 0 0 12px;">${title}</h1>
|
|
103
|
-
<p style="color: #a0a0a0; font-size: 16px; line-height: 1.6; margin: 0 0 32px;">${message}</p>
|
|
104
|
-
<a href="https://stacksjs.com" style="color: #888; font-size: 13px; text-decoration: none;">stacksjs.com</a>
|
|
105
|
-
</div>
|
|
106
|
-
</body>
|
|
107
|
-
</html>`
|
|
108
|
-
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sending a plain HTML form back to the page it came from.
|
|
3
|
+
*
|
|
4
|
+
* The public forms (newsletter signup, contact) post straight to the API with
|
|
5
|
+
* no JavaScript in between. Answered with JSON, a visitor who submits one is
|
|
6
|
+
* left on a page of raw `{"success":true,...}` with no way back but the back
|
|
7
|
+
* button. A browser form post asks for HTML, so it gets a 303 back to the page
|
|
8
|
+
* instead, with the outcome in the query string for that page to render. A
|
|
9
|
+
* `fetch()` asks for anything (`Accept: *\/*`) and keeps getting JSON.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Whether the request is a browser submitting a `<form>`, rather than a script. */
|
|
13
|
+
export function wantsFormRedirect(headers: Headers): boolean {
|
|
14
|
+
const accept = headers.get('accept') || ''
|
|
15
|
+
const type = headers.get('content-type') || ''
|
|
16
|
+
const isForm = type.includes('application/x-www-form-urlencoded') || type.includes('multipart/form-data')
|
|
17
|
+
return isForm && accept.includes('text/html')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A same-site path, or null. Only a path is ever accepted, never a host: a
|
|
22
|
+
* return address that could name another site would make this endpoint an
|
|
23
|
+
* open redirect for anyone who can link to it.
|
|
24
|
+
*/
|
|
25
|
+
function sitePath(value: unknown): string | null {
|
|
26
|
+
if (typeof value !== 'string')
|
|
27
|
+
return null
|
|
28
|
+
|
|
29
|
+
const path = value.trim()
|
|
30
|
+
// `//host` and `/\host` are both read as another origin by browsers.
|
|
31
|
+
if (!path.startsWith('/') || path.startsWith('//') || path.includes('\\'))
|
|
32
|
+
return null
|
|
33
|
+
// eslint-disable-next-line no-control-regex
|
|
34
|
+
if (/[\u0000-\u001F\u007F]/.test(path))
|
|
35
|
+
return null
|
|
36
|
+
|
|
37
|
+
return path
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The path, query and fragment of a Referer, or null when there is none. */
|
|
41
|
+
function refererPath(referer: string | null): string | null {
|
|
42
|
+
if (!referer)
|
|
43
|
+
return null
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const url = new URL(referer)
|
|
47
|
+
return `${url.pathname}${url.search}${url.hash}`
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return null
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Where to send the browser: the form's own `return` field when it names a
|
|
56
|
+
* path on this site, else the page in the Referer, else the home page, with
|
|
57
|
+
* `?<param>=<outcome>` set on it. A fragment in `return` is kept, so a page
|
|
58
|
+
* with two forms can bring the reader back to the one they used.
|
|
59
|
+
*/
|
|
60
|
+
export function formReturnLocation(options: { target?: unknown, referer?: string | null, param: string, outcome: string }): string {
|
|
61
|
+
const path = sitePath(options.target) ?? sitePath(refererPath(options.referer ?? null)) ?? '/'
|
|
62
|
+
const url = new URL(path, 'http://return.invalid')
|
|
63
|
+
url.searchParams.set(options.param, options.outcome)
|
|
64
|
+
return `${url.pathname}${url.search}${url.hash}`
|
|
65
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { config } from '@stacksjs/config'
|
|
2
|
+
|
|
3
|
+
function escapeHtml(value: string): string {
|
|
4
|
+
return value.replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', '\'': ''' }[c]!))
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The page an unsubscribe or confirmation link lands on.
|
|
9
|
+
*
|
|
10
|
+
* It names the app the visitor subscribed to, from config, rather than
|
|
11
|
+
* stacksjs.com, which every app using this default used to print. The
|
|
12
|
+
* message carries the subscriber's stored address, so everything is escaped.
|
|
13
|
+
*/
|
|
14
|
+
export function subscriptionPage(title: string, message: string, success: boolean): string {
|
|
15
|
+
const appName = escapeHtml(config.app?.name || 'Home')
|
|
16
|
+
const rawUrl = String(config.app?.url || '/')
|
|
17
|
+
const appUrl = escapeHtml(/^https?:\/\//.test(rawUrl) || rawUrl.startsWith('/') ? rawUrl : `https://${rawUrl}`)
|
|
18
|
+
const accent = success ? '#1747c9' : '#dc2626'
|
|
19
|
+
const icon = success ? '✓' : '!'
|
|
20
|
+
|
|
21
|
+
return `<!DOCTYPE html>
|
|
22
|
+
<html lang="en">
|
|
23
|
+
<head>
|
|
24
|
+
<meta charset="UTF-8">
|
|
25
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
26
|
+
<meta name="robots" content="noindex">
|
|
27
|
+
<meta name="color-scheme" content="light dark">
|
|
28
|
+
<title>${escapeHtml(title)} - ${appName}</title>
|
|
29
|
+
<style>
|
|
30
|
+
:root { --bg: #f6f7f9; --card: #ffffff; --text: #0b1220; --muted: #4a5468; --line: rgba(11, 18, 32, 0.1); }
|
|
31
|
+
@media (prefers-color-scheme: dark) { :root { --bg: #070b14; --card: #0d1322; --text: #e8edf7; --muted: #9ba7be; --line: rgba(232, 237, 247, 0.1); } }
|
|
32
|
+
* { box-sizing: border-box; }
|
|
33
|
+
body { margin: 0; min-height: 100vh; display: grid; place-items: center; padding: 24px; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; }
|
|
34
|
+
main { width: min(440px, 100%); padding: 40px 32px; border: 1px solid var(--line); border-radius: 16px; background: var(--card); text-align: center; }
|
|
35
|
+
.icon { display: inline-grid; place-items: center; width: 56px; height: 56px; margin-bottom: 20px; border-radius: 50%; color: ${accent}; font-size: 24px; font-weight: 700; background: ${accent}1a; }
|
|
36
|
+
h1 { margin: 0 0 10px; font-size: 22px; letter-spacing: -0.01em; }
|
|
37
|
+
p { margin: 0 0 28px; color: var(--muted); font-size: 15px; line-height: 1.6; overflow-wrap: anywhere; }
|
|
38
|
+
a { display: inline-block; padding: 11px 20px; border-radius: 10px; color: #fff; font-size: 14px; font-weight: 600; text-decoration: none; background: #1747c9; }
|
|
39
|
+
</style>
|
|
40
|
+
</head>
|
|
41
|
+
<body>
|
|
42
|
+
<main>
|
|
43
|
+
<div class="icon" aria-hidden="true">${icon}</div>
|
|
44
|
+
<h1>${escapeHtml(title)}</h1>
|
|
45
|
+
<p>${escapeHtml(message)}</p>
|
|
46
|
+
<a href="${appUrl}">Back to ${appName}</a>
|
|
47
|
+
</main>
|
|
48
|
+
</body>
|
|
49
|
+
</html>`
|
|
50
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { config } from '@stacksjs/config'
|
|
2
|
+
import { mail, template } from '@stacksjs/email'
|
|
3
|
+
import { url } from '@stacksjs/router'
|
|
4
|
+
|
|
5
|
+
export interface SubscriptionConfirmRequestOptions {
|
|
6
|
+
to: string
|
|
7
|
+
/** The pending list membership's token (`subscribe()`'s `token`). */
|
|
8
|
+
token: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The app's own address, with a scheme, for links in the email. */
|
|
12
|
+
function appUrl(): string {
|
|
13
|
+
const raw = String(config.app.url || '').trim().replace(/\/+$/, '')
|
|
14
|
+
if (!raw)
|
|
15
|
+
return ''
|
|
16
|
+
return /^https?:\/\//.test(raw) ? raw : `https://${raw}`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Ask a new subscriber to confirm their address (double opt-in).
|
|
21
|
+
*
|
|
22
|
+
* Sent by the public signup when the list has `double_opt_in` on. The button
|
|
23
|
+
* opens `GET /api/email/confirm?token=…` (ConfirmSubscriptionAction), which
|
|
24
|
+
* confirms the membership and sends the welcome email. Until then campaigns
|
|
25
|
+
* skip the address. Uses `resources/emails/subscription-confirm.stx`, which an
|
|
26
|
+
* app can override like any email template.
|
|
27
|
+
*/
|
|
28
|
+
export async function sendSubscriptionConfirmRequest(options: SubscriptionConfirmRequestOptions): Promise<void> {
|
|
29
|
+
const appName = config.app.name || 'Stacks'
|
|
30
|
+
const confirmUrl = url('email.confirm', { token: options.token })
|
|
31
|
+
const subject = `Confirm your subscription to ${appName}`
|
|
32
|
+
|
|
33
|
+
const { html, text } = await template('subscription-confirm', {
|
|
34
|
+
variables: {
|
|
35
|
+
email: options.to,
|
|
36
|
+
confirmUrl,
|
|
37
|
+
appName,
|
|
38
|
+
appUrl: appUrl(),
|
|
39
|
+
year: new Date().getFullYear(),
|
|
40
|
+
},
|
|
41
|
+
subject,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
await mail.sendOrFail({
|
|
45
|
+
to: [options.to],
|
|
46
|
+
from: {
|
|
47
|
+
name: config.email.from?.name || appName,
|
|
48
|
+
address: config.email.from?.address || 'hello@stacksjs.com',
|
|
49
|
+
},
|
|
50
|
+
subject,
|
|
51
|
+
html,
|
|
52
|
+
text,
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default sendSubscriptionConfirmRequest
|
|
@@ -1,29 +1,68 @@
|
|
|
1
1
|
import { config } from '@stacksjs/config'
|
|
2
2
|
import { mail, template } from '@stacksjs/email'
|
|
3
|
+
import { buildUnsubscribeHeaders } from '@stacksjs/newsletter'
|
|
3
4
|
import { url } from '@stacksjs/router'
|
|
4
5
|
|
|
5
6
|
export interface SubscriptionConfirmationOptions {
|
|
6
7
|
to: string
|
|
7
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The unsubscribe token for the link and the List-Unsubscribe headers: the
|
|
10
|
+
* list membership's uuid (`subscribe()`'s `token`), so unsubscribing leaves
|
|
11
|
+
* that list only.
|
|
12
|
+
*/
|
|
13
|
+
token?: string
|
|
14
|
+
/** The subscriber's own uuid, the older global token. Used when `token` is not given. */
|
|
15
|
+
subscriberUuid?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The app's own address, with a scheme, for links in the email. */
|
|
19
|
+
function appUrl(): string {
|
|
20
|
+
const raw = String(config.app.url || '').trim().replace(/\/+$/, '')
|
|
21
|
+
if (!raw)
|
|
22
|
+
return ''
|
|
23
|
+
return /^https?:\/\//.test(raw) ? raw : `https://${raw}`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The RFC 8058 `List-Unsubscribe` headers, so the inbox's own unsubscribe
|
|
28
|
+
* button works (POST `/api/email/unsubscribe`). They need an absolute URL;
|
|
29
|
+
* without one the email still goes, with only the link in its body.
|
|
30
|
+
*/
|
|
31
|
+
function unsubscribeHeaders(unsubscribeUrl: string): Record<string, string> | undefined {
|
|
32
|
+
try {
|
|
33
|
+
return buildUnsubscribeHeaders({ url: unsubscribeUrl })
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return undefined
|
|
37
|
+
}
|
|
8
38
|
}
|
|
9
39
|
|
|
10
40
|
/**
|
|
11
41
|
* Send a subscription confirmation email with an unsubscribe link.
|
|
12
42
|
*
|
|
13
|
-
* Uses the `resources/emails/subscription-confirmation.stx` template
|
|
43
|
+
* Uses the `resources/emails/subscription-confirmation.stx` template, and
|
|
44
|
+
* passes it the app's name and address so a template never has to fall back
|
|
45
|
+
* to the framework's.
|
|
14
46
|
*/
|
|
15
47
|
export async function sendSubscriptionConfirmation(options: SubscriptionConfirmationOptions): Promise<void> {
|
|
16
|
-
const { to
|
|
48
|
+
const { to } = options
|
|
49
|
+
const unsubscribeToken = options.token || options.subscriberUuid
|
|
50
|
+
if (!unsubscribeToken)
|
|
51
|
+
throw new Error('[mail] sendSubscriptionConfirmation needs a token for the unsubscribe link')
|
|
17
52
|
|
|
18
53
|
const appName = config.app.name || 'Stacks'
|
|
19
|
-
const unsubscribeUrl = url('email.unsubscribe', { token:
|
|
54
|
+
const unsubscribeUrl = url('email.unsubscribe', { token: unsubscribeToken })
|
|
55
|
+
const subject = `You are on the ${appName} list`
|
|
20
56
|
|
|
21
57
|
const { html, text } = await template('subscription-confirmation', {
|
|
22
58
|
variables: {
|
|
23
59
|
email: to,
|
|
24
60
|
unsubscribeUrl,
|
|
61
|
+
appName,
|
|
62
|
+
appUrl: appUrl(),
|
|
63
|
+
year: new Date().getFullYear(),
|
|
25
64
|
},
|
|
26
|
-
subject
|
|
65
|
+
subject,
|
|
27
66
|
})
|
|
28
67
|
|
|
29
68
|
await mail.sendOrFail({
|
|
@@ -32,9 +71,10 @@ export async function sendSubscriptionConfirmation(options: SubscriptionConfirma
|
|
|
32
71
|
name: config.email.from?.name || appName,
|
|
33
72
|
address: config.email.from?.address || 'hello@stacksjs.com',
|
|
34
73
|
},
|
|
35
|
-
subject
|
|
74
|
+
subject,
|
|
36
75
|
html,
|
|
37
76
|
text,
|
|
77
|
+
headers: unsubscribeHeaders(unsubscribeUrl),
|
|
38
78
|
})
|
|
39
79
|
}
|
|
40
80
|
|
package/app/Models/FailedJob.ts
CHANGED
package/app/Models/Referral.ts
CHANGED
|
@@ -14,7 +14,7 @@ export default defineModel({
|
|
|
14
14
|
referredUserId: { required: true, unique: true, validation: { rule: schema.number().integer().min(1) } },
|
|
15
15
|
code: { required: true, validation: { rule: schema.string().max(24) } },
|
|
16
16
|
status: { required: true, default: 'registered', validation: { rule: schema.enum(['registered', 'qualified']) } },
|
|
17
|
-
qualifiedAt: { nullable: true, validation: { rule: schema.
|
|
17
|
+
qualifiedAt: { nullable: true, validation: { rule: schema.timestamp() } },
|
|
18
18
|
},
|
|
19
19
|
|
|
20
20
|
dashboard: {
|
|
@@ -122,7 +122,7 @@ export default defineModel({
|
|
|
122
122
|
order: 7,
|
|
123
123
|
fillable: true,
|
|
124
124
|
validation: {
|
|
125
|
-
rule: schema.
|
|
125
|
+
rule: schema.timestamp().required(),
|
|
126
126
|
},
|
|
127
127
|
factory: faker => faker.date.soon().toISOString(),
|
|
128
128
|
},
|
|
@@ -131,7 +131,7 @@ export default defineModel({
|
|
|
131
131
|
order: 8,
|
|
132
132
|
fillable: true,
|
|
133
133
|
validation: {
|
|
134
|
-
rule: schema.
|
|
134
|
+
rule: schema.timestamp().required(),
|
|
135
135
|
},
|
|
136
136
|
factory: faker => faker.date.future().toISOString(),
|
|
137
137
|
},
|
|
@@ -133,7 +133,7 @@ export default defineModel({
|
|
|
133
133
|
order: 9,
|
|
134
134
|
fillable: true,
|
|
135
135
|
validation: {
|
|
136
|
-
rule: schema.
|
|
136
|
+
rule: schema.timestamp(),
|
|
137
137
|
},
|
|
138
138
|
factory: faker => faker.date.future().toISOString().slice(0, 19).replace('T', ' '),
|
|
139
139
|
},
|
|
@@ -141,7 +141,7 @@ export default defineModel({
|
|
|
141
141
|
order: 10,
|
|
142
142
|
fillable: true,
|
|
143
143
|
validation: {
|
|
144
|
-
rule: schema.
|
|
144
|
+
rule: schema.timestamp(),
|
|
145
145
|
},
|
|
146
146
|
factory: faker => faker.date.future().toISOString().slice(0, 19).replace('T', ' '),
|
|
147
147
|
},
|
|
@@ -149,7 +149,7 @@ export default defineModel({
|
|
|
149
149
|
order: 11,
|
|
150
150
|
fillable: true,
|
|
151
151
|
validation: {
|
|
152
|
-
rule: schema.
|
|
152
|
+
rule: schema.timestamp(),
|
|
153
153
|
},
|
|
154
154
|
factory: faker => faker.date.future().toISOString().slice(0, 19).replace('T', ' '),
|
|
155
155
|
},
|
package/ide/vscode/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"publisher": "Stacks",
|
|
3
3
|
"name": "vscode-stacks",
|
|
4
4
|
"displayName": "Stacks",
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.667",
|
|
6
6
|
"description": "Stacks, built into VS Code: stx templates, pickier linting and formatting, encrypted env files, the dev server preview and every buddy command. No other extensions required.",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/defaults",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.667",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
@@ -45,14 +45,14 @@
|
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@iconify-json/f7": "^1.2.2",
|
|
47
47
|
"@iconify-json/hugeicons": "^1.2.35",
|
|
48
|
-
"@stacksjs/mobile": "0.75.
|
|
49
|
-
"@stacksjs/sanitizer": "^0.2.
|
|
50
|
-
"@stacksjs/stx": "^0.2.
|
|
51
|
-
"@stacksjs/strings": "0.75.
|
|
48
|
+
"@stacksjs/mobile": "0.75.667",
|
|
49
|
+
"@stacksjs/sanitizer": "^0.2.930",
|
|
50
|
+
"@stacksjs/stx": "^0.2.930",
|
|
51
|
+
"@stacksjs/strings": "0.75.667",
|
|
52
52
|
"@xterm/addon-fit": "^0.11.0",
|
|
53
53
|
"@xterm/xterm": "^6.0.0",
|
|
54
54
|
"ts-qr-codes": "^0.1.8",
|
|
55
|
-
"@stacksjs/support": "0.75.
|
|
56
|
-
"@stacksjs/browser": "0.75.
|
|
55
|
+
"@stacksjs/support": "0.75.667",
|
|
56
|
+
"@stacksjs/browser": "0.75.667"
|
|
57
57
|
}
|
|
58
58
|
}
|
|
@@ -19,8 +19,10 @@ export interface AuthConfig {
|
|
|
19
19
|
username: string
|
|
20
20
|
password: string
|
|
21
21
|
tokenExpiry: number
|
|
22
|
-
/** @deprecated Use explicit bearer rotation or a refresh-token exchange. */
|
|
22
|
+
/** @deprecated Read by nothing. Use explicit bearer rotation or a refresh-token exchange. */
|
|
23
23
|
tokenRotation?: number
|
|
24
|
+
/** Whether the scheduler prunes expired and old revoked tokens daily. Default `true`. */
|
|
25
|
+
pruneTokens?: boolean
|
|
24
26
|
defaultAbilities: string[]
|
|
25
27
|
defaultTokenName: string
|
|
26
28
|
}
|
|
@@ -1197,13 +1197,12 @@ async function subscribeEmail(email: string): Promise<{ success: boolean, messag
|
|
|
1197
1197
|
body: JSON.stringify({ email }),
|
|
1198
1198
|
})
|
|
1199
1199
|
|
|
1200
|
-
|
|
1200
|
+
// The endpoint answers a bad address with 200 and `success: false`, so the
|
|
1201
|
+
// status alone is not the answer: read the body.
|
|
1202
|
+
const data = await response.json().catch(() => ({})) as { success?: boolean, message?: string }
|
|
1203
|
+
if (response.ok && data.success === true)
|
|
1201
1204
|
return { success: true }
|
|
1202
|
-
}
|
|
1203
|
-
else {
|
|
1204
|
-
const data = await response.json().catch(() => ({}))
|
|
1205
|
-
return { success: false, message: data.message || 'Failed to subscribe. Please try again later.' }
|
|
1206
|
-
}
|
|
1205
|
+
return { success: false, message: data.message || 'Failed to subscribe. Please try again later.' }
|
|
1207
1206
|
}
|
|
1208
1207
|
|
|
1209
1208
|
// Email signup (notification popup)
|