@haatlabs-core/templates 0.15.1 → 0.17.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haatlabs-core/templates",
3
- "version": "0.15.1",
3
+ "version": "0.17.0",
4
4
  "description": "The starter files and shared project files that @haatlabs-core/cli writes.",
5
5
  "keywords": [
6
6
  "haatlabs"
@@ -36,8 +36,8 @@
36
36
  },
37
37
  "devDependencies": {
38
38
  "playwright-core": "1.63.0",
39
- "@haatlabs-core/tsconfig": "0.15.1",
40
- "@haatlabs-core/vitest-config": "0.15.1"
39
+ "@haatlabs-core/tsconfig": "0.17.0",
40
+ "@haatlabs-core/vitest-config": "0.17.0"
41
41
  },
42
42
  "scripts": {
43
43
  "build": "rm -rf dist && tsc -p tsconfig.build.json",
@@ -8,6 +8,8 @@ import {
8
8
  ApiKeyRepository,
9
9
  BillingRepository,
10
10
  ExtensionKeyRepository,
11
+ UserIdentityRepository,
12
+ DisposableEmailRepository,
11
13
  FeatureRepository,
12
14
  IdentityRepository,
13
15
  #@if kindShopify
@@ -114,6 +116,16 @@ export class AppModule {
114
116
  useFactory: (keys: ExtensionKeyRepository) => keys,
115
117
  inject: [ExtensionKeyRepository],
116
118
  },
119
+ // Signing in with Google or GitHub, where their keys are set.
120
+ identityLinks: {
121
+ useFactory: (links: UserIdentityRepository) => links,
122
+ inject: [UserIdentityRepository],
123
+ },
124
+ // New accounts on throwaway mailbox domains are refused where the check is on.
125
+ disposableEmails: {
126
+ useFactory: (list: DisposableEmailRepository) => list,
127
+ inject: [DisposableEmailRepository],
128
+ },
117
129
  },
118
130
  connectableExtensions: config.extension.ids,
119
131
  #@if kindShopify
@@ -7,6 +7,8 @@ import {
7
7
  ApiKeyRepository,
8
8
  BillingRepository,
9
9
  ExtensionKeyRepository,
10
+ UserIdentityRepository,
11
+ DisposableEmailRepository,
10
12
  FeatureRepository,
11
13
  IdentityRepository,
12
14
  WebhookEventRepository,
@@ -237,6 +239,16 @@ export class InfraModule {
237
239
  new ExtensionKeyRepository(s.db, a.db),
238
240
  inject: [SYSTEM_DB, APP_DB],
239
241
  },
242
+ {
243
+ provide: UserIdentityRepository,
244
+ useFactory: (s: DatabaseHandle) => new UserIdentityRepository(s.db),
245
+ inject: [SYSTEM_DB],
246
+ },
247
+ {
248
+ provide: DisposableEmailRepository,
249
+ useFactory: (s: DatabaseHandle) => new DisposableEmailRepository(s.db),
250
+ inject: [SYSTEM_DB],
251
+ },
240
252
  #@if kindShopify
241
253
  {
242
254
  provide: StoreRepository,
@@ -17,6 +17,7 @@
17
17
  "@@@name@@/config": "workspace:*",
18
18
  "@@@name@@/database": "workspace:*",
19
19
  "@haatlabs-core/logger": "^@@platformVersion@@",
20
+ "@haatlabs-core/nest-auth": "^@@platformVersion@@",
20
21
  "@@@name@@/observability": "workspace:*",
21
22
  "@@@name@@/queue": "workspace:*",
22
23
  "drizzle-orm": "^0.45.3",
@@ -1,5 +1,6 @@
1
1
  import type { AppConfig } from '@@@name@@/config';
2
2
  import {
3
+ DisposableEmailRepository,
3
4
  OutboxRepository,
4
5
  #@if kindShopify
5
6
  SystemBillingRepository,
@@ -14,6 +15,7 @@ import {
14
15
  import { ShopifyAdminClient, StoreAccessTokens, TokenCipher } from '@@@name@@/shopify';
15
16
  #@end
16
17
  import { createLogger, watchRedis } from '@haatlabs-core/logger';
18
+ import { startDisposableDomainRefresh } from '@haatlabs-core/nest-auth/disposable';
17
19
  import { createMetrics } from '@@@name@@/observability';
18
20
  import type { Metrics } from '@@@name@@/observability';
19
21
  import {
@@ -159,6 +161,18 @@ export async function startWorker(
159
161
  { metrics },
160
162
  );
161
163
 
164
+ // The disposable-domain list: filled and refreshed daily, by one process at a time.
165
+ const disposable = config.auth.disposableEmails?.check
166
+ ? startDisposableDomainRefresh({
167
+ store: new DisposableEmailRepository(system.db),
168
+ url: config.auth.disposableEmails.url,
169
+ fetch: fetchImpl,
170
+ logger,
171
+ redis,
172
+ appName: '@@name@@',
173
+ })
174
+ : null;
175
+
162
176
  logger.info('Worker started');
163
177
 
164
178
  const stop = async (): Promise<void> => {
@@ -166,6 +180,7 @@ export async function startWorker(
166
180
  // Takes no more work and finishes what it has in hand.
167
181
  await consumer?.stop();
168
182
  #@end
183
+ disposable?.stop();
169
184
  await relay.stop();
170
185
  if (health) {
171
186
  await new Promise<void>((resolve) => {
@@ -112,6 +112,21 @@ ALLOW_FIXED_SIGN_IN_CODE=false
112
112
  # as <email>:<eight digits>, e.g. review@example.com:12345678. Only that email takes the
113
113
  # code, so production may set it: it opens that one account. No email is sent to it.
114
114
  SIGN_IN_REVIEW_ACCOUNT=
115
+ # false: sign-in codes are not emailed, and people sign in with SIGN_IN_FIXED_CODE (which it
116
+ # needs). For dev or staging, to send no code emails there. Other emails still go out.
117
+ SIGN_IN_EMAIL=true
118
+ # Signing in with Google and GitHub: the shared Haatlabs Google OAuth client and GitHub App
119
+ # (see social-sign-in-setup.md). Both of a pair set turns that button on; leave a pair empty
120
+ # and it is not offered. The callback is <API_BASE_URL>/api/v1/auth/oauth/<google|github>/callback.
121
+ # New accounts on throwaway mailbox domains (mailinator and ~98,000 others) are refused:
122
+ # on|off, default on in production only. The list is refreshed daily from
123
+ # DISPOSABLE_EMAIL_DOMAINS_URL (https; the community list when empty).
124
+ DISPOSABLE_EMAIL_CHECK=
125
+ DISPOSABLE_EMAIL_DOMAINS_URL=
126
+ GOOGLE_OAUTH_CLIENT_ID=
127
+ GOOGLE_OAUTH_CLIENT_SECRET=
128
+ GITHUB_OAUTH_CLIENT_ID=
129
+ GITHUB_OAUTH_CLIENT_SECRET=
115
130
 
116
131
  # The browser extensions (their ids, comma-separated) a person may connect to this app from
117
132
  # /connect/extension: the app's own extension (`haatlabs create --kind extension`). Empty: none.
@@ -0,0 +1,13 @@
1
+ CREATE TABLE "user_identities" (
2
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
3
+ "user_id" uuid NOT NULL,
4
+ "provider" text NOT NULL,
5
+ "provider_user_id" text NOT NULL,
6
+ "email_at_link" text NOT NULL,
7
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
8
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
9
+ CONSTRAINT "user_identities_provider_user_unique" UNIQUE("provider","provider_user_id")
10
+ );
11
+ --> statement-breakpoint
12
+ ALTER TABLE "user_identities" ADD CONSTRAINT "user_identities_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
13
+ CREATE INDEX "user_identities_user_idx" ON "user_identities" USING btree ("user_id");
@@ -0,0 +1,46 @@
1
+ CREATE TABLE "disposable_email_domains" (
2
+ "domain" text PRIMARY KEY NOT NULL
3
+ );
4
+ --> statement-breakpoint
5
+ ALTER TABLE "subscriptions" DROP CONSTRAINT "subscriptions_organization_id_unique";--> statement-breakpoint
6
+ DROP INDEX "plans_one_default_idx";--> statement-breakpoint
7
+ ALTER TABLE "plans" ADD COLUMN "product" text DEFAULT 'app' NOT NULL;--> statement-breakpoint
8
+ ALTER TABLE "subscriptions" ADD COLUMN "product" text DEFAULT 'app' NOT NULL;--> statement-breakpoint
9
+ CREATE UNIQUE INDEX "plans_one_default_idx" ON "plans" USING btree ("product") WHERE "plans"."is_default";--> statement-breakpoint
10
+ ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_org_product_unique" UNIQUE("organization_id","product");--> statement-breakpoint
11
+ -- The app's own plan decides priority: a plan of another product never does.
12
+ CREATE OR REPLACE FUNCTION organization_goes_first() RETURNS boolean
13
+ LANGUAGE sql STABLE
14
+ AS $$
15
+ WITH org AS (
16
+ SELECT NULLIF(current_setting('app.organization_id', true), '')::uuid AS id
17
+ ),
18
+ held AS (
19
+ SELECT s.plan_id
20
+ FROM subscriptions s
21
+ JOIN org ON s.organization_id = org.id
22
+ WHERE s.status = 'ACTIVE'
23
+ AND s.product = 'app'
24
+ AND s.period_start <= now()
25
+ AND (now() < s.period_end OR (s.provider = 'shopify' AND s.provider_ended_at IS NULL))
26
+ LIMIT 1
27
+ ),
28
+ plan AS (
29
+ SELECT COALESCE(
30
+ (SELECT plan_id FROM held),
31
+ (SELECT id FROM plans WHERE is_default AND product = 'app' LIMIT 1)
32
+ ) AS id
33
+ )
34
+ SELECT CASE
35
+ WHEN (SELECT id FROM org) IS NULL THEN false
36
+ ELSE COALESCE(
37
+ (
38
+ SELECT f.limit_value = 1
39
+ FROM plan_features f
40
+ JOIN plan ON f.plan_id = plan.id
41
+ WHERE f.feature_key = 'PRIORITY'
42
+ ),
43
+ false
44
+ )
45
+ END
46
+ $$;