create-protobase 0.47.1 → 0.49.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/dist/env-file.js +3 -3
- package/dist/templates/erp/.env.example +3 -3
- package/dist/templates/erp/auth/hooks.ts +26 -0
- package/dist/templates/erp/db/migrations/022_auth.sql +297 -0
- package/dist/templates/erp/{config/jobs.test.ts → hooks/schedule-overdue-check.test.ts} +1 -1
- package/dist/templates/erp/hooks/schedule-overdue-check.ts +16 -0
- package/dist/templates/erp/jobs/mark-invoice-overdue.ts +7 -0
- package/dist/templates/erp/jobs/mark-overdue-invoices.ts +8 -0
- package/dist/templates/erp/package.json +6 -5
- package/dist/templates/erp/protobase.config.ts +33 -18
- package/dist/templates/real-estate/.env.example +3 -3
- package/dist/templates/real-estate/auth/hooks.ts +9 -0
- package/dist/templates/real-estate/db/migrations/015_auth.sql +319 -0
- package/dist/templates/real-estate/package.json +6 -5
- package/dist/templates/real-estate/protobase.config.ts +13 -4
- package/dist/templates/scratch/.env.example +3 -3
- package/dist/templates/scratch/db/migrations/006_auth.sql +319 -0
- package/dist/templates/scratch/package.json +6 -5
- package/dist/templates/scratch/protobase.config.ts +11 -3
- package/package.json +1 -1
- package/dist/templates/erp/auth/auth.ts +0 -65
- package/dist/templates/erp/config/jobs.ts +0 -29
- package/dist/templates/real-estate/auth/auth.ts +0 -33
- package/dist/templates/scratch/auth/auth.ts +0 -25
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
-- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
|
|
2
|
+
-- Applied with the project's other migrations; edit or remove statements the project handles differently.
|
|
3
|
+
|
|
4
|
+
-- The record of the auth store's migrations.
|
|
5
|
+
create schema if not exists "auth";
|
|
6
|
+
|
|
7
|
+
create table "auth"."auth_migrations" (
|
|
8
|
+
version integer primary key,
|
|
9
|
+
name text not null,
|
|
10
|
+
applied_at timestamptz default current_timestamp not null
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
-- The auth store an earlier Protobase made, without a record of its migrations, adopted: 1 core, 2 two_factor, 3 passkeys, 4 sign_in_policy, 5 staff_sign_in
|
|
14
|
+
-- recorded as applied, once every table and column they make is there.
|
|
15
|
+
do $adopt$
|
|
16
|
+
declare
|
|
17
|
+
missing text;
|
|
18
|
+
begin
|
|
19
|
+
select string_agg(expected.table_name || '.' || expected.column_name, ', ' order by expected.table_name, expected.column_name) into missing
|
|
20
|
+
from (values ('account', 'id', 'text'),
|
|
21
|
+
('account', 'account_id', 'text'),
|
|
22
|
+
('account', 'provider_id', 'text'),
|
|
23
|
+
('account', 'user_id', 'text'),
|
|
24
|
+
('account', 'access_token', 'text'),
|
|
25
|
+
('account', 'refresh_token', 'text'),
|
|
26
|
+
('account', 'id_token', 'text'),
|
|
27
|
+
('account', 'access_token_expires_at', 'timestamptz'),
|
|
28
|
+
('account', 'refresh_token_expires_at', 'timestamptz'),
|
|
29
|
+
('account', 'scope', 'text'),
|
|
30
|
+
('account', 'password', 'text'),
|
|
31
|
+
('account', 'created_at', 'timestamptz'),
|
|
32
|
+
('account', 'updated_at', 'timestamptz'),
|
|
33
|
+
('jwks', 'id', 'text'),
|
|
34
|
+
('jwks', 'public_key', 'text'),
|
|
35
|
+
('jwks', 'private_key', 'text'),
|
|
36
|
+
('jwks', 'created_at', 'timestamptz'),
|
|
37
|
+
('jwks', 'expires_at', 'timestamptz'),
|
|
38
|
+
('jwks', 'alg', 'text'),
|
|
39
|
+
('jwks', 'crv', 'text'),
|
|
40
|
+
('session', 'id', 'text'),
|
|
41
|
+
('session', 'expires_at', 'timestamptz'),
|
|
42
|
+
('session', 'token', 'text'),
|
|
43
|
+
('session', 'created_at', 'timestamptz'),
|
|
44
|
+
('session', 'updated_at', 'timestamptz'),
|
|
45
|
+
('session', 'ip_address', 'text'),
|
|
46
|
+
('session', 'user_agent', 'text'),
|
|
47
|
+
('session', 'user_id', 'text'),
|
|
48
|
+
('session', 'impersonated_by', 'text'),
|
|
49
|
+
('session', 'active_role', 'text'),
|
|
50
|
+
('user', 'id', 'text'),
|
|
51
|
+
('user', 'name', 'text'),
|
|
52
|
+
('user', 'email', 'text'),
|
|
53
|
+
('user', 'email_verified', 'bool'),
|
|
54
|
+
('user', 'image', 'text'),
|
|
55
|
+
('user', 'created_at', 'timestamptz'),
|
|
56
|
+
('user', 'updated_at', 'timestamptz'),
|
|
57
|
+
('user', 'role', 'text'),
|
|
58
|
+
('user', 'banned', 'bool'),
|
|
59
|
+
('user', 'ban_reason', 'text'),
|
|
60
|
+
('user', 'ban_expires', 'timestamptz'),
|
|
61
|
+
('user', 'last_role', 'text'),
|
|
62
|
+
('user', 'email_confirmed_at', 'timestamptz'),
|
|
63
|
+
('verification', 'id', 'text'),
|
|
64
|
+
('verification', 'identifier', 'text'),
|
|
65
|
+
('verification', 'value', 'text'),
|
|
66
|
+
('verification', 'expires_at', 'timestamptz'),
|
|
67
|
+
('verification', 'created_at', 'timestamptz'),
|
|
68
|
+
('verification', 'updated_at', 'timestamptz'),
|
|
69
|
+
('two_factor', 'id', 'text'),
|
|
70
|
+
('two_factor', 'secret', 'text'),
|
|
71
|
+
('two_factor', 'backup_codes', 'text'),
|
|
72
|
+
('two_factor', 'user_id', 'text'),
|
|
73
|
+
('two_factor', 'verified', 'bool'),
|
|
74
|
+
('two_factor', 'failed_verification_count', 'int4'),
|
|
75
|
+
('two_factor', 'locked_until', 'timestamptz'),
|
|
76
|
+
('user', 'two_factor_enabled', 'bool'),
|
|
77
|
+
('passkey', 'id', 'text'),
|
|
78
|
+
('passkey', 'name', 'text'),
|
|
79
|
+
('passkey', 'public_key', 'text'),
|
|
80
|
+
('passkey', 'user_id', 'text'),
|
|
81
|
+
('passkey', 'credential_id', 'text'),
|
|
82
|
+
('passkey', 'counter', 'int4'),
|
|
83
|
+
('passkey', 'device_type', 'text'),
|
|
84
|
+
('passkey', 'backed_up', 'bool'),
|
|
85
|
+
('passkey', 'transports', 'text'),
|
|
86
|
+
('passkey', 'created_at', 'timestamptz'),
|
|
87
|
+
('passkey', 'aaguid', 'text'),
|
|
88
|
+
('passkey', 'rp_id', 'text'),
|
|
89
|
+
('sign_in_policy', 'id', 'text'),
|
|
90
|
+
('sign_in_policy', 'password', 'text'),
|
|
91
|
+
('sign_in_policy', 'email_code', 'text'),
|
|
92
|
+
('sign_in_policy', 'passkey', 'text'),
|
|
93
|
+
('sign_in_policy', 'two_factor', 'text'),
|
|
94
|
+
('sign_in_policy', 'staff_access', 'text'),
|
|
95
|
+
('sign_in_policy', 'platform_sign_in', 'text'),
|
|
96
|
+
('sign_in_policy', 'created_at', 'timestamptz'),
|
|
97
|
+
('sign_in_policy', 'created_by', 'text'),
|
|
98
|
+
('staff_sign_in', 'id', 'text'),
|
|
99
|
+
('staff_sign_in', 'user_id', 'text'),
|
|
100
|
+
('staff_sign_in', 'user_email', 'text'),
|
|
101
|
+
('staff_sign_in', 'staff', 'text'),
|
|
102
|
+
('staff_sign_in', 'staff_name', 'text'),
|
|
103
|
+
('staff_sign_in', 'staff_subject', 'text'),
|
|
104
|
+
('staff_sign_in', 'issuer', 'text'),
|
|
105
|
+
('staff_sign_in', 'reason', 'text'),
|
|
106
|
+
('staff_sign_in', 'session_id', 'text'),
|
|
107
|
+
('staff_sign_in', 'started_at', 'timestamptz'),
|
|
108
|
+
('staff_sign_in', 'expires_at', 'timestamptz'),
|
|
109
|
+
('staff_sign_in', 'ended_at', 'timestamptz')) as expected (table_name, column_name, udt_name)
|
|
110
|
+
where not exists (
|
|
111
|
+
select from information_schema.columns found
|
|
112
|
+
where found.table_schema = 'auth' and found.table_name = expected.table_name and found.column_name = expected.column_name and found.udt_name = expected.udt_name
|
|
113
|
+
);
|
|
114
|
+
if missing is not null then
|
|
115
|
+
raise exception 'The auth store does not match the migrations this file adopts: it lacks %, or has them as other types', missing;
|
|
116
|
+
end if;
|
|
117
|
+
end
|
|
118
|
+
$adopt$;
|
|
119
|
+
|
|
120
|
+
insert into "auth"."auth_migrations" (version, name) values (1, 'core'), (2, 'two_factor'), (3, 'passkeys'), (4, 'sign_in_policy'), (5, 'staff_sign_in');
|
|
121
|
+
|
|
122
|
+
-- 6 organizations: Organizations, their members and their invitations.
|
|
123
|
+
alter table "auth"."user" add column if not exists "last_organization_id" text;
|
|
124
|
+
|
|
125
|
+
alter table "auth"."session" add column active_organization_id text;
|
|
126
|
+
|
|
127
|
+
create table "auth"."organization" (
|
|
128
|
+
id text primary key,
|
|
129
|
+
name text not null,
|
|
130
|
+
slug text not null unique,
|
|
131
|
+
logo text,
|
|
132
|
+
created_at timestamptz not null,
|
|
133
|
+
metadata text
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
create table "auth"."member" (
|
|
137
|
+
id text primary key,
|
|
138
|
+
organization_id text not null references "auth"."organization" (id) on delete cascade,
|
|
139
|
+
user_id text not null references "auth"."user" (id) on delete cascade,
|
|
140
|
+
role text not null,
|
|
141
|
+
created_at timestamptz not null,
|
|
142
|
+
app_roles jsonb
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
create index member_organization_id_idx on "auth"."member" (organization_id);
|
|
146
|
+
|
|
147
|
+
create index member_user_id_idx on "auth"."member" (user_id);
|
|
148
|
+
|
|
149
|
+
create table "auth"."invitation" (
|
|
150
|
+
id text primary key,
|
|
151
|
+
organization_id text not null references "auth"."organization" (id) on delete cascade,
|
|
152
|
+
email text not null,
|
|
153
|
+
role text,
|
|
154
|
+
status text not null,
|
|
155
|
+
expires_at timestamptz not null,
|
|
156
|
+
created_at timestamptz default current_timestamp not null,
|
|
157
|
+
inviter_id text not null references "auth"."user" (id) on delete cascade,
|
|
158
|
+
app_roles jsonb
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
create index invitation_organization_id_idx on "auth"."invitation" (organization_id);
|
|
162
|
+
|
|
163
|
+
create index invitation_email_idx on "auth"."invitation" (email);
|
|
164
|
+
|
|
165
|
+
insert into "auth"."auth_migrations" (version, name) values (6, 'organizations');
|
|
166
|
+
|
|
167
|
+
-- 7 mcp: The clients, resources, tokens and consents of the OAuth provider for MCP.
|
|
168
|
+
create table "auth"."oauth_client" (
|
|
169
|
+
id text primary key,
|
|
170
|
+
client_id text not null unique,
|
|
171
|
+
client_secret text,
|
|
172
|
+
client_discovery_id text,
|
|
173
|
+
disabled boolean,
|
|
174
|
+
skip_consent boolean,
|
|
175
|
+
enable_end_session boolean,
|
|
176
|
+
subject_type text,
|
|
177
|
+
scopes jsonb,
|
|
178
|
+
client_credentials_scopes jsonb,
|
|
179
|
+
user_id text references "auth"."user" (id) on delete cascade,
|
|
180
|
+
created_at timestamptz,
|
|
181
|
+
updated_at timestamptz,
|
|
182
|
+
name text,
|
|
183
|
+
uri text,
|
|
184
|
+
icon text,
|
|
185
|
+
contacts jsonb,
|
|
186
|
+
tos text,
|
|
187
|
+
policy text,
|
|
188
|
+
software_id text,
|
|
189
|
+
software_version text,
|
|
190
|
+
software_statement text,
|
|
191
|
+
redirect_uris jsonb not null,
|
|
192
|
+
post_logout_redirect_uris jsonb,
|
|
193
|
+
backchannel_logout_uri text,
|
|
194
|
+
backchannel_logout_session_required boolean,
|
|
195
|
+
token_endpoint_auth_method text,
|
|
196
|
+
application_type text,
|
|
197
|
+
jwks text,
|
|
198
|
+
jwks_uri text,
|
|
199
|
+
grant_types jsonb,
|
|
200
|
+
response_types jsonb,
|
|
201
|
+
require_pkce boolean,
|
|
202
|
+
dpop_bound_access_tokens boolean,
|
|
203
|
+
reference_id text,
|
|
204
|
+
metadata jsonb
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
create index oauth_client_user_id_idx on "auth"."oauth_client" (user_id);
|
|
208
|
+
|
|
209
|
+
create table "auth"."oauth_resource" (
|
|
210
|
+
id text primary key,
|
|
211
|
+
identifier text not null unique,
|
|
212
|
+
name text not null,
|
|
213
|
+
access_token_ttl integer,
|
|
214
|
+
refresh_token_ttl integer,
|
|
215
|
+
signing_algorithm text,
|
|
216
|
+
signing_key_id text,
|
|
217
|
+
allowed_scopes jsonb,
|
|
218
|
+
custom_claims jsonb,
|
|
219
|
+
dpop_bound_access_tokens_required boolean,
|
|
220
|
+
disabled boolean,
|
|
221
|
+
created_at timestamptz,
|
|
222
|
+
updated_at timestamptz,
|
|
223
|
+
policy_version integer,
|
|
224
|
+
metadata jsonb
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
create table "auth"."oauth_client_resource" (
|
|
228
|
+
id text primary key,
|
|
229
|
+
client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
|
|
230
|
+
resource_id text not null references "auth"."oauth_resource" (identifier) on delete cascade,
|
|
231
|
+
metadata jsonb,
|
|
232
|
+
created_at timestamptz
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
create index oauth_client_resource_client_id_idx on "auth"."oauth_client_resource" (client_id);
|
|
236
|
+
|
|
237
|
+
create index oauth_client_resource_resource_id_idx on "auth"."oauth_client_resource" (resource_id);
|
|
238
|
+
|
|
239
|
+
create unique index oauth_client_resource_client_id_resource_id_uidx on "auth"."oauth_client_resource" (client_id, resource_id);
|
|
240
|
+
|
|
241
|
+
create table "auth"."oauth_refresh_token" (
|
|
242
|
+
id text primary key,
|
|
243
|
+
token text not null unique,
|
|
244
|
+
client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
|
|
245
|
+
session_id text references "auth"."session" (id) on delete set null,
|
|
246
|
+
user_id text not null references "auth"."user" (id) on delete cascade,
|
|
247
|
+
reference_id text,
|
|
248
|
+
authorization_code_id text,
|
|
249
|
+
resources jsonb,
|
|
250
|
+
requested_user_info_claims jsonb,
|
|
251
|
+
expires_at timestamptz not null,
|
|
252
|
+
created_at timestamptz not null,
|
|
253
|
+
revoked timestamptz,
|
|
254
|
+
rotated_at timestamptz,
|
|
255
|
+
rotation_replay_response text,
|
|
256
|
+
rotation_replay_expires_at timestamptz,
|
|
257
|
+
auth_time timestamptz,
|
|
258
|
+
confirmation jsonb,
|
|
259
|
+
scopes jsonb not null
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
create index oauth_refresh_token_client_id_idx on "auth"."oauth_refresh_token" (client_id);
|
|
263
|
+
|
|
264
|
+
create index oauth_refresh_token_session_id_idx on "auth"."oauth_refresh_token" (session_id);
|
|
265
|
+
|
|
266
|
+
create index oauth_refresh_token_user_id_idx on "auth"."oauth_refresh_token" (user_id);
|
|
267
|
+
|
|
268
|
+
create index oauth_refresh_token_authorization_code_id_idx on "auth"."oauth_refresh_token" (authorization_code_id);
|
|
269
|
+
|
|
270
|
+
create table "auth"."oauth_access_token" (
|
|
271
|
+
id text primary key,
|
|
272
|
+
token text not null unique,
|
|
273
|
+
client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
|
|
274
|
+
session_id text references "auth"."session" (id) on delete set null,
|
|
275
|
+
user_id text references "auth"."user" (id) on delete cascade,
|
|
276
|
+
reference_id text,
|
|
277
|
+
authorization_code_id text,
|
|
278
|
+
resources jsonb,
|
|
279
|
+
requested_user_info_claims jsonb,
|
|
280
|
+
refresh_id text references "auth"."oauth_refresh_token" (id) on delete cascade,
|
|
281
|
+
expires_at timestamptz not null,
|
|
282
|
+
created_at timestamptz not null,
|
|
283
|
+
revoked timestamptz,
|
|
284
|
+
confirmation jsonb,
|
|
285
|
+
scopes jsonb not null
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
create index oauth_access_token_client_id_idx on "auth"."oauth_access_token" (client_id);
|
|
289
|
+
|
|
290
|
+
create index oauth_access_token_session_id_idx on "auth"."oauth_access_token" (session_id);
|
|
291
|
+
|
|
292
|
+
create index oauth_access_token_user_id_idx on "auth"."oauth_access_token" (user_id);
|
|
293
|
+
|
|
294
|
+
create index oauth_access_token_authorization_code_id_idx on "auth"."oauth_access_token" (authorization_code_id);
|
|
295
|
+
|
|
296
|
+
create index oauth_access_token_refresh_id_idx on "auth"."oauth_access_token" (refresh_id);
|
|
297
|
+
|
|
298
|
+
create table "auth"."oauth_consent" (
|
|
299
|
+
id text primary key,
|
|
300
|
+
client_id text not null references "auth"."oauth_client" (client_id) on delete cascade,
|
|
301
|
+
user_id text references "auth"."user" (id) on delete cascade,
|
|
302
|
+
reference_id text,
|
|
303
|
+
resources jsonb,
|
|
304
|
+
requested_user_info_claims jsonb,
|
|
305
|
+
scopes jsonb not null,
|
|
306
|
+
created_at timestamptz not null,
|
|
307
|
+
updated_at timestamptz not null
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
create index oauth_consent_client_id_idx on "auth"."oauth_consent" (client_id);
|
|
311
|
+
|
|
312
|
+
create index oauth_consent_user_id_idx on "auth"."oauth_consent" (user_id);
|
|
313
|
+
|
|
314
|
+
create table "auth"."oauth_client_assertion" (
|
|
315
|
+
id text primary key,
|
|
316
|
+
expires_at timestamptz not null
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
insert into "auth"."auth_migrations" (version, name) values (7, 'mcp');
|
|
@@ -13,11 +13,12 @@
|
|
|
13
13
|
"dev": "protobase dev"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@protobase/cli": "^0.
|
|
17
|
-
"@protobase/
|
|
18
|
-
"@protobase/
|
|
19
|
-
"@protobase/
|
|
20
|
-
"@protobase/
|
|
16
|
+
"@protobase/cli": "^0.49.0",
|
|
17
|
+
"@protobase/config": "^0.49.0",
|
|
18
|
+
"@protobase/layout": "^0.49.0",
|
|
19
|
+
"@protobase/schema": "^0.49.0",
|
|
20
|
+
"@protobase/server": "^0.49.0",
|
|
21
|
+
"@protobase/ui": "^0.49.0",
|
|
21
22
|
"better-auth": "^1.7.12",
|
|
22
23
|
"jose": "^6.2.12",
|
|
23
24
|
"kysely": "^0.29.6",
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { auth, authenticate } from './auth/auth'
|
|
1
|
+
import { defineConfig } from '@protobase/config'
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
// The whole app, as one object; it grows as the app does. Its resources are found in config/. Its address and secret
|
|
4
|
+
// are Protobase's defaults, PROTOBASE_URL and PROTOBASE_SECRET (see .env.example).
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
auth: {
|
|
7
|
+
// No defaultRole on purpose: every account is created with an explicit role (`protobase users create --role`).
|
|
8
|
+
roles: ['admin'],
|
|
9
|
+
},
|
|
10
|
+
// Every user works in tenant 1, which access rules can compare with an organization_id column.
|
|
11
|
+
authenticate: { tenant: 1 },
|
|
12
|
+
})
|
package/package.json
CHANGED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { PostgresDialect } from 'kysely'
|
|
2
|
-
import pg from 'pg'
|
|
3
|
-
import { appPool, betterAuthAuthenticator, createAuth } from '@protobase/server'
|
|
4
|
-
import { databaseUrl } from '../db/connection'
|
|
5
|
-
|
|
6
|
-
const secret = process.env.BETTER_AUTH_SECRET
|
|
7
|
-
if (!secret) throw new Error('BETTER_AUTH_SECRET is not set; generate one with `openssl rand -base64 32` and put it in .env')
|
|
8
|
-
|
|
9
|
-
// The app's one pool, shared with the runtime, so the store opens no connections beyond the app's
|
|
10
|
-
const pool = appPool(pg, databaseUrl)
|
|
11
|
-
|
|
12
|
-
/** The admin store (Better Auth's users, sessions and signing keys) lives in this schema, next to the ERP's own schemas. */
|
|
13
|
-
export const authSchema = 'auth'
|
|
14
|
-
|
|
15
|
-
// The ERP's records name their organization by core.organizations' integer id, so a new organization takes the next
|
|
16
|
-
// value of that table's sequence, and gets its row there with the ERP's own settings once it is made.
|
|
17
|
-
const nextOrganizationId = async () => {
|
|
18
|
-
const { rows } = await pool.query<{ id: string }>("select nextval(pg_get_serial_sequence('core.organizations', 'id'))::text as id")
|
|
19
|
-
return rows[0]!.id
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const addOrganizationRow = async ({ organization }: { organization: { id: string; name: string; slug: string } }) => {
|
|
23
|
-
await pool.query(
|
|
24
|
-
"insert into core.organizations (id, name, slug, country_code, currency_code) values ($1, $2, $3, 'NL', 'EUR') on conflict (id) do nothing",
|
|
25
|
-
[Number(organization.id), organization.name, organization.slug],
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Orders name their owner by core.users' integer id, so a new user takes the next value of that table's sequence; their
|
|
30
|
-
// row there comes with their first membership (db/migrations/019_auth_users.sql).
|
|
31
|
-
const nextUserId = async () => {
|
|
32
|
-
const { rows } = await pool.query<{ id: string }>("select nextval(pg_get_serial_sequence('core.users', 'id'))::text as id")
|
|
33
|
-
return rows[0]!.id
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const auth = createAuth({
|
|
37
|
-
database: { dialect: new PostgresDialect({ pool }), type: 'postgres', schemaName: authSchema, transaction: true },
|
|
38
|
-
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:5173',
|
|
39
|
-
secret,
|
|
40
|
-
// `admin` is the superuser, in every organization; the others are held per organization (or globally, by a few).
|
|
41
|
-
roles: {
|
|
42
|
-
admin: 'Superuser',
|
|
43
|
-
auditor: 'Auditor',
|
|
44
|
-
manager: { label: 'Manager', grants: ['sales', 'accountant', 'warehouse', 'editor'] },
|
|
45
|
-
sales: 'Sales',
|
|
46
|
-
accountant: 'Accountant',
|
|
47
|
-
warehouse: 'Warehouse',
|
|
48
|
-
editor: 'Editor',
|
|
49
|
-
integration: 'Integration',
|
|
50
|
-
},
|
|
51
|
-
generateUserId: nextUserId,
|
|
52
|
-
organizations: {
|
|
53
|
-
generateId: nextOrganizationId,
|
|
54
|
-
onCreated: addOrganizationRow,
|
|
55
|
-
hooks: {
|
|
56
|
-
afterUpdateOrganization: async ({ organization }) => {
|
|
57
|
-
if (organization) await pool.query('update core.organizations set name = $2, slug = $3 where id = $1', [Number(organization.id), organization.name, organization.slug])
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
trustedOrigins: process.env.TRUSTED_ORIGINS?.split(',').filter(Boolean),
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
/** The organization comes from the signed-in person's session: the one they work in. */
|
|
65
|
-
export const authenticate = betterAuthAuthenticator({ auth })
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { sql } from 'kysely'
|
|
2
|
-
import { defineJob, type WriteEvent } from '@protobase/server'
|
|
3
|
-
|
|
4
|
-
/** An invoice still sent and unpaid once its due date has passed becomes overdue. */
|
|
5
|
-
export const markInvoiceOverdue = defineJob<{ invoiceId: string }>({
|
|
6
|
-
name: 'mark-invoice-overdue',
|
|
7
|
-
run: async ({ invoiceId }, { db }) => {
|
|
8
|
-
await sql`update sales.invoices set status = 'overdue' where id = ${invoiceId} and status = 'sent' and due_at < current_date`.execute(db)
|
|
9
|
-
},
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
/** Every night, the sent invoices that passed their due date, such as imported ones that never had a check enqueued. */
|
|
13
|
-
export const markOverdueInvoices = defineJob({
|
|
14
|
-
name: 'mark-overdue-invoices',
|
|
15
|
-
schedule: '0 2 * * *',
|
|
16
|
-
timezone: 'Europe/Amsterdam',
|
|
17
|
-
run: async (_, { db }) => {
|
|
18
|
-
await sql`update sales.invoices set status = 'overdue' where status = 'sent' and due_at < current_date`.execute(db)
|
|
19
|
-
},
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
const day = 86_400_000
|
|
23
|
-
|
|
24
|
-
/** For a sent invoice, the check that it is paid: the day after it is due, one waiting check per invoice. */
|
|
25
|
-
export const overdueCheck = (event: WriteEvent) => {
|
|
26
|
-
if (event.resource.name !== 'invoices' || event.operation === 'delete' || event.after?.status !== 'sent') return undefined
|
|
27
|
-
const invoiceId = String(event.after.id)
|
|
28
|
-
return { invoiceId, runAt: new Date(Date.parse(String(event.after.due_at)) + day), key: `invoice:${invoiceId}` }
|
|
29
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { PostgresDialect } from 'kysely'
|
|
2
|
-
import pg from 'pg'
|
|
3
|
-
import { appPool, betterAuthAuthenticator, createAuth } from '@protobase/server'
|
|
4
|
-
import { databaseUrl } from '../db/connection'
|
|
5
|
-
|
|
6
|
-
const secret = process.env.BETTER_AUTH_SECRET
|
|
7
|
-
if (!secret) throw new Error('BETTER_AUTH_SECRET is not set; generate one with `openssl rand -base64 32` and put it in .env')
|
|
8
|
-
|
|
9
|
-
// The app's one pool, shared with the runtime, so the store opens no connections beyond the app's
|
|
10
|
-
const pool = appPool(pg, databaseUrl)
|
|
11
|
-
|
|
12
|
-
/** The admin store (Better Auth's users, sessions and signing keys) lives in this schema, next to the example's own schemas. */
|
|
13
|
-
export const authSchema = 'auth'
|
|
14
|
-
|
|
15
|
-
// Tickets name their assignee by core.users' integer id, so a new user takes the next value of that table's sequence;
|
|
16
|
-
// their row there comes with the account (db/migrations/013_auth_users.sql).
|
|
17
|
-
const nextUserId = async () => {
|
|
18
|
-
const { rows } = await pool.query<{ id: string }>("select nextval(pg_get_serial_sequence('core.users', 'id'))::text as id")
|
|
19
|
-
return rows[0]!.id
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const auth = createAuth({
|
|
23
|
-
database: { dialect: new PostgresDialect({ pool }), type: 'postgres', schemaName: authSchema, transaction: true },
|
|
24
|
-
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:5173',
|
|
25
|
-
secret,
|
|
26
|
-
// No defaultRole on purpose: every account is created with an explicit role (`protobase users create --role`).
|
|
27
|
-
roles: ['admin', 'manager', 'finance', 'maintenance'],
|
|
28
|
-
generateUserId: nextUserId,
|
|
29
|
-
trustedOrigins: process.env.TRUSTED_ORIGINS?.split(',').filter(Boolean),
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
/** Organizations are not used yet: everyone works in organization 1. */
|
|
33
|
-
export const authenticate = betterAuthAuthenticator({ auth, tenant: 1 })
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { PostgresDialect } from 'kysely'
|
|
2
|
-
import pg from 'pg'
|
|
3
|
-
import { appPool, betterAuthAuthenticator, createAuth } from '@protobase/server'
|
|
4
|
-
import { databaseUrl } from '../db/connection'
|
|
5
|
-
|
|
6
|
-
const secret = process.env.BETTER_AUTH_SECRET
|
|
7
|
-
if (!secret) throw new Error('BETTER_AUTH_SECRET is not set; generate one with `openssl rand -base64 32` and put it in .env')
|
|
8
|
-
|
|
9
|
-
// The app's one pool, shared with the runtime, so the store opens no connections beyond the app's
|
|
10
|
-
const pool = appPool(pg, databaseUrl)
|
|
11
|
-
|
|
12
|
-
/** The admin store (Better Auth's users, sessions and signing keys) lives in this schema, next to the app's own schemas. */
|
|
13
|
-
export const authSchema = 'auth'
|
|
14
|
-
|
|
15
|
-
export const auth = createAuth({
|
|
16
|
-
database: { dialect: new PostgresDialect({ pool }), type: 'postgres', schemaName: authSchema, transaction: true },
|
|
17
|
-
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:5173',
|
|
18
|
-
secret,
|
|
19
|
-
// No defaultRole on purpose: every account is created with an explicit role (`protobase users create --role`).
|
|
20
|
-
roles: ['admin'],
|
|
21
|
-
trustedOrigins: process.env.TRUSTED_ORIGINS?.split(',').filter(Boolean),
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
/** Every user works in tenant 1, which access rules can compare with an organization_id column. */
|
|
25
|
-
export const authenticate = betterAuthAuthenticator({ auth, tenant: 1 })
|