@lowdefy/api 0.0.0-experimental-20260720072521 → 0.0.0-experimental-20260720135014
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/context/createApiContext.js +1 -9
- package/dist/context/createAuthorize.js +8 -19
- package/dist/context/createEvaluateOperators.js +1 -2
- package/dist/index.js +3 -13
- package/dist/routes/agent/callAgent.js +180 -7
- package/dist/routes/{websocket/authorizeWebsocket.js → auth/callbacks/addUserFieldsToSession.js} +11 -10
- package/dist/routes/auth/callbacks/addUserFieldsToToken.js +30 -0
- package/dist/routes/auth/{hooks/composeAfterSlot.js → callbacks/createCallbackPlugins.js} +6 -11
- package/dist/routes/auth/callbacks/createCallbacks.js +46 -0
- package/dist/routes/auth/callbacks/createJWTCallback.js +77 -0
- package/dist/routes/auth/callbacks/createRedirectCallback.js +41 -0
- package/dist/routes/auth/callbacks/createSessionCallback.js +81 -0
- package/dist/routes/auth/callbacks/createSignInCallback.js +40 -0
- package/dist/routes/auth/callbacks/validateSessionRoles.js +33 -0
- package/dist/routes/{websocket/getWebsocketConfig.js → auth/createAdapter.js} +8 -14
- package/dist/routes/auth/createLogger.js +38 -0
- package/dist/routes/auth/createProviders.js +25 -0
- package/dist/routes/auth/events/createCreateUserEvent.js +41 -0
- package/dist/routes/auth/{organizations/getHookRequestHeaders.js → events/createEventPlugins.js} +6 -9
- package/dist/routes/auth/events/createEvents.js +56 -0
- package/dist/routes/auth/events/createLinkAccountEvent.js +43 -0
- package/dist/routes/auth/{strategies/getAuthStrategies.js → events/createSessionEvent.js} +17 -14
- package/dist/routes/auth/events/createSignInEvent.js +46 -0
- package/dist/routes/auth/events/createSignOutEvent.js +43 -0
- package/dist/routes/auth/events/createUpdateUserEvent.js +41 -0
- package/dist/routes/auth/getNextAuthConfig.js +75 -0
- package/dist/routes/endpoints/authorizeApiEndpoint.js +2 -8
- package/dist/routes/endpoints/callEndpoint.js +0 -20
- package/dist/routes/endpoints/control/controlReject.js +1 -3
- package/dist/routes/endpoints/control/controlThrow.js +1 -4
- package/dist/routes/endpoints/handleEndpointCall.js +1 -62
- package/dist/routes/endpoints/handleRequest.js +1 -8
- package/dist/routes/endpoints/handleValidateSchema.js +1 -3
- package/dist/routes/endpoints/runRoutine.js +0 -18
- package/dist/routes/endpoints/test/runTest.js +4 -3
- package/dist/routes/page/getPageConfig.js +8 -42
- package/dist/routes/request/authorizeRequest.js +2 -8
- package/dist/routes/request/callRequest.js +1 -8
- package/dist/routes/request/callRequestResolver.js +2 -6
- package/dist/test/testContext.js +6 -6
- package/package.json +10 -16
- package/dist/context/applySystemTrust.js +0 -33
- package/dist/context/createSystemContext.js +0 -72
- package/dist/context/normalizeInjectedCaller.js +0 -41
- package/dist/context/resolveAuthentication.js +0 -107
- package/dist/context/resolveStrategyCaller.js +0 -56
- package/dist/email/renderAuthEmail.js +0 -98
- package/dist/email/resolveThemeLogo.js +0 -35
- package/dist/routes/agent/prepareAgent.js +0 -169
- package/dist/routes/auth/buildAdminPlugin.js +0 -55
- package/dist/routes/auth/buildCaptchaPlugin.js +0 -61
- package/dist/routes/auth/buildPhoneNumberPlugin.js +0 -57
- package/dist/routes/auth/buildProviders.js +0 -51
- package/dist/routes/auth/createAuthLogger.js +0 -31
- package/dist/routes/auth/createSendEmail.js +0 -49
- package/dist/routes/auth/getBetterAuth.js +0 -72
- package/dist/routes/auth/getBetterAuthConfig.js +0 -439
- package/dist/routes/auth/getUserAdminRole.js +0 -32
- package/dist/routes/auth/hooks/authHookPoints.js +0 -192
- package/dist/routes/auth/hooks/buildEngineHooks.js +0 -43
- package/dist/routes/auth/hooks/buildHooks.js +0 -111
- package/dist/routes/auth/hooks/composeBeforeSlot.js +0 -41
- package/dist/routes/auth/hooks/createHookDispatch.js +0 -59
- package/dist/routes/auth/hooks/createUserAfterHook.js +0 -33
- package/dist/routes/auth/hooks/createUserBeforeHook.js +0 -47
- package/dist/routes/auth/hooks/findHookUser.js +0 -27
- package/dist/routes/auth/modelNames.js +0 -29
- package/dist/routes/auth/organizations/buildOrganizationPlugin.js +0 -136
- package/dist/routes/auth/organizations/createActiveOrgPolicyHook.js +0 -211
- package/dist/routes/auth/organizations/createAfterAcceptInvitationHook.js +0 -75
- package/dist/routes/auth/organizations/createAutoJoinHook.js +0 -63
- package/dist/routes/auth/organizations/ensureOrganization.js +0 -95
- package/dist/routes/auth/organizations/findPendingInvitation.js +0 -43
- package/dist/routes/auth/organizations/getOrganizationBinding.js +0 -65
- package/dist/routes/auth/organizations/resolvePinnedOrganization.js +0 -48
- package/dist/routes/auth/organizations/syncUserAdminRole.js +0 -102
- package/dist/routes/auth/resolveCookiePrefix.js +0 -30
- package/dist/routes/auth/strategies/createAuthStrategies.js +0 -77
- package/dist/routes/endpoints/handleAgentCall.js +0 -83
- package/dist/routes/endpoints/handleAuthStep.js +0 -124
- package/dist/routes/endpoints/handleRenderNotification.js +0 -189
- package/dist/routes/endpoints/runDetachedEndpoint.js +0 -85
- package/dist/routes/endpoints/runScheduledEndpoint.js +0 -102
- package/dist/routes/endpoints/runWebhookEndpoint.js +0 -118
- package/dist/routes/endpoints/runWebhookVerify.js +0 -94
- package/dist/routes/endpoints/scheduleBackground.js +0 -48
- package/dist/routes/notifications/derivePreview.js +0 -30
- package/dist/routes/notifications/getNotificationConfig.js +0 -32
- package/dist/routes/notifications/resolveNotificationLinks.js +0 -72
- package/dist/routes/page/dynamic/resolveDynamicContent.js +0 -172
- package/dist/routes/page/dynamic/unescapeOperators.js +0 -36
- package/dist/routes/page/dynamic/validateFragment.js +0 -104
- package/dist/routes/request/resolveTenant.js +0 -57
- package/dist/routes/websocket/createChannelRegistry.js +0 -278
- package/dist/routes/websocket/createWebSocketConnection.js +0 -131
- package/dist/routes/websocket/getWebsocketResolver.js +0 -33
- package/dist/routes/websocket/prepareChannel.js +0 -84
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import findHookUser from './findHookUser.js';
|
|
16
|
-
// The bindable auth hook points and what each hands the hook routine as
|
|
17
|
-
// _payload - the contract between the hook mechanism and the hook author.
|
|
18
|
-
// Build validation guarantees every configured point is in this catalog.
|
|
19
|
-
//
|
|
20
|
-
// The dispatch layer (createHookDispatch) injects a reserved "point" field
|
|
21
|
-
// onto every payload - the firing point name - so a routine bound to multiple
|
|
22
|
-
// points can branch on _payload.point. buildPayload definitions own the domain
|
|
23
|
-
// data only and must never produce a field named "point".
|
|
24
|
-
// Twin catalog: packages/build/src/build/buildAuth/authHookPoints.js lists
|
|
25
|
-
// the point names build accepts - a point accepted there without a
|
|
26
|
-
// definition here crashes buildHooks at startup; keep the two in sync.
|
|
27
|
-
//
|
|
28
|
-
// Database points receive BetterAuth's databaseHooks record; session and
|
|
29
|
-
// account writes carry only a userId, so the subject user is read through
|
|
30
|
-
// BetterAuth's internal adapter. user.update deviates from the catalog by
|
|
31
|
-
// what BetterAuth provides: the before hook receives only the changed fields
|
|
32
|
-
// (no way to identify the subject), the after hook only the updated record.
|
|
33
|
-
//
|
|
34
|
-
// Synthetic points are backed by BetterAuth config callbacks, not database
|
|
35
|
-
// hooks: "email.verified" by emailVerification.afterEmailVerification, and the
|
|
36
|
-
// "phone.*" points by the phoneNumber plugin's sendOTP /
|
|
37
|
-
// sendPasswordResetOTP / callbackOnVerification.
|
|
38
|
-
const authHookPoints = {
|
|
39
|
-
'user.create.before': {
|
|
40
|
-
kind: 'database',
|
|
41
|
-
model: 'user',
|
|
42
|
-
operation: 'create',
|
|
43
|
-
timing: 'before',
|
|
44
|
-
buildPayload: (data)=>({
|
|
45
|
-
user: data
|
|
46
|
-
})
|
|
47
|
-
},
|
|
48
|
-
'user.create.after': {
|
|
49
|
-
kind: 'database',
|
|
50
|
-
model: 'user',
|
|
51
|
-
operation: 'create',
|
|
52
|
-
timing: 'after',
|
|
53
|
-
buildPayload: (data)=>({
|
|
54
|
-
user: data
|
|
55
|
-
})
|
|
56
|
-
},
|
|
57
|
-
'user.update.before': {
|
|
58
|
-
kind: 'database',
|
|
59
|
-
model: 'user',
|
|
60
|
-
operation: 'update',
|
|
61
|
-
timing: 'before',
|
|
62
|
-
buildPayload: (data)=>({
|
|
63
|
-
user: null,
|
|
64
|
-
changes: data
|
|
65
|
-
})
|
|
66
|
-
},
|
|
67
|
-
'user.update.after': {
|
|
68
|
-
kind: 'database',
|
|
69
|
-
model: 'user',
|
|
70
|
-
operation: 'update',
|
|
71
|
-
timing: 'after',
|
|
72
|
-
buildPayload: (data)=>({
|
|
73
|
-
user: data,
|
|
74
|
-
changes: null
|
|
75
|
-
})
|
|
76
|
-
},
|
|
77
|
-
'session.create.before': {
|
|
78
|
-
kind: 'database',
|
|
79
|
-
model: 'session',
|
|
80
|
-
operation: 'create',
|
|
81
|
-
timing: 'before',
|
|
82
|
-
buildPayload: async (data, ctx)=>({
|
|
83
|
-
session: data,
|
|
84
|
-
user: await findHookUser({
|
|
85
|
-
ctx,
|
|
86
|
-
userId: data.userId
|
|
87
|
-
})
|
|
88
|
-
})
|
|
89
|
-
},
|
|
90
|
-
'session.create.after': {
|
|
91
|
-
kind: 'database',
|
|
92
|
-
model: 'session',
|
|
93
|
-
operation: 'create',
|
|
94
|
-
timing: 'after',
|
|
95
|
-
buildPayload: async (data, ctx)=>({
|
|
96
|
-
session: data,
|
|
97
|
-
user: await findHookUser({
|
|
98
|
-
ctx,
|
|
99
|
-
userId: data.userId
|
|
100
|
-
})
|
|
101
|
-
})
|
|
102
|
-
},
|
|
103
|
-
'session.delete.after': {
|
|
104
|
-
kind: 'database',
|
|
105
|
-
model: 'session',
|
|
106
|
-
operation: 'delete',
|
|
107
|
-
timing: 'after',
|
|
108
|
-
buildPayload: async (data, ctx)=>({
|
|
109
|
-
session: data,
|
|
110
|
-
user: await findHookUser({
|
|
111
|
-
ctx,
|
|
112
|
-
userId: data.userId
|
|
113
|
-
})
|
|
114
|
-
})
|
|
115
|
-
},
|
|
116
|
-
'account.create.before': {
|
|
117
|
-
kind: 'database',
|
|
118
|
-
model: 'account',
|
|
119
|
-
operation: 'create',
|
|
120
|
-
timing: 'before',
|
|
121
|
-
buildPayload: async (data, ctx)=>({
|
|
122
|
-
account: data,
|
|
123
|
-
user: await findHookUser({
|
|
124
|
-
ctx,
|
|
125
|
-
userId: data.userId
|
|
126
|
-
})
|
|
127
|
-
})
|
|
128
|
-
},
|
|
129
|
-
'account.create.after': {
|
|
130
|
-
kind: 'database',
|
|
131
|
-
model: 'account',
|
|
132
|
-
operation: 'create',
|
|
133
|
-
timing: 'after',
|
|
134
|
-
buildPayload: async (data, ctx)=>({
|
|
135
|
-
account: data,
|
|
136
|
-
user: await findHookUser({
|
|
137
|
-
ctx,
|
|
138
|
-
userId: data.userId
|
|
139
|
-
})
|
|
140
|
-
})
|
|
141
|
-
},
|
|
142
|
-
'verification.create.before': {
|
|
143
|
-
kind: 'database',
|
|
144
|
-
model: 'verification',
|
|
145
|
-
operation: 'create',
|
|
146
|
-
timing: 'before',
|
|
147
|
-
buildPayload: (data)=>({
|
|
148
|
-
verification: data
|
|
149
|
-
})
|
|
150
|
-
},
|
|
151
|
-
'verification.create.after': {
|
|
152
|
-
kind: 'database',
|
|
153
|
-
model: 'verification',
|
|
154
|
-
operation: 'create',
|
|
155
|
-
timing: 'after',
|
|
156
|
-
buildPayload: (data)=>({
|
|
157
|
-
verification: data
|
|
158
|
-
})
|
|
159
|
-
},
|
|
160
|
-
'email.verified': {
|
|
161
|
-
kind: 'synthetic',
|
|
162
|
-
timing: 'after',
|
|
163
|
-
buildPayload: (user)=>({
|
|
164
|
-
user
|
|
165
|
-
})
|
|
166
|
-
},
|
|
167
|
-
'phone.otp.send': {
|
|
168
|
-
kind: 'synthetic',
|
|
169
|
-
timing: 'after',
|
|
170
|
-
buildPayload: (data)=>({
|
|
171
|
-
phoneNumber: data.phoneNumber,
|
|
172
|
-
code: data.code
|
|
173
|
-
})
|
|
174
|
-
},
|
|
175
|
-
'phone.passwordReset.send': {
|
|
176
|
-
kind: 'synthetic',
|
|
177
|
-
timing: 'after',
|
|
178
|
-
buildPayload: (data)=>({
|
|
179
|
-
phoneNumber: data.phoneNumber,
|
|
180
|
-
code: data.code
|
|
181
|
-
})
|
|
182
|
-
},
|
|
183
|
-
'phone.verified': {
|
|
184
|
-
kind: 'synthetic',
|
|
185
|
-
timing: 'after',
|
|
186
|
-
buildPayload: (data)=>({
|
|
187
|
-
user: data.user,
|
|
188
|
-
phoneNumber: data.phoneNumber
|
|
189
|
-
})
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
export default authHookPoints;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import createActiveOrgPolicyHook from '../organizations/createActiveOrgPolicyHook.js';
|
|
16
|
-
import createAutoJoinHook from '../organizations/createAutoJoinHook.js';
|
|
17
|
-
// Engine-tier hook bindings - hard-coded framework behavior, not entries in
|
|
18
|
-
// auth.hooks. Keyed by point; each point lists BetterAuth-native callbacks
|
|
19
|
-
// (before: (data, ctx) => false | { data } | undefined, after: (data, ctx)).
|
|
20
|
-
// Engine hooks run first in each composed slot, so the user hook sees the
|
|
21
|
-
// engine-normalized record. Config parameterizes the membership behaviors
|
|
22
|
-
// (policy, signup) but they are not user-pluggable.
|
|
23
|
-
function buildEngineHooks({ authConfig, getAuth }) {
|
|
24
|
-
const organizations = authConfig.organizations;
|
|
25
|
-
const engineHooks = {
|
|
26
|
-
'session.create.before': [
|
|
27
|
-
createActiveOrgPolicyHook({
|
|
28
|
-
getAuth,
|
|
29
|
-
organizations
|
|
30
|
-
})
|
|
31
|
-
]
|
|
32
|
-
};
|
|
33
|
-
if (organizations.policy === 'pinned' && organizations.signup === 'open') {
|
|
34
|
-
engineHooks['user.create.after'] = [
|
|
35
|
-
createAutoJoinHook({
|
|
36
|
-
getAuth,
|
|
37
|
-
organizations
|
|
38
|
-
})
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
return engineHooks;
|
|
42
|
-
}
|
|
43
|
-
export default buildEngineHooks;
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import { set, type } from '@lowdefy/helpers';
|
|
16
|
-
import { LowdefyInternalError } from '@lowdefy/errors';
|
|
17
|
-
import authHookPoints from './authHookPoints.js';
|
|
18
|
-
import buildEngineHooks from './buildEngineHooks.js';
|
|
19
|
-
import composeAfterSlot from './composeAfterSlot.js';
|
|
20
|
-
import composeBeforeSlot from './composeBeforeSlot.js';
|
|
21
|
-
import createHookDispatch from './createHookDispatch.js';
|
|
22
|
-
import createUserAfterHook from './createUserAfterHook.js';
|
|
23
|
-
import createUserBeforeHook from './createUserBeforeHook.js';
|
|
24
|
-
// Assembles BetterAuth's databaseHooks and the synthetic points' backing
|
|
25
|
-
// callbacks from the engine-tier bindings and the validated auth.hooks
|
|
26
|
-
// entries. Each point resolves to one composed slot with engine hooks first,
|
|
27
|
-
// then every user hook bound to the point in array order - the build orders
|
|
28
|
-
// the array as module contributions first, then app entries, so array order
|
|
29
|
-
// IS the tier order. Build validation guarantees every entry binds a known
|
|
30
|
-
// point to an existing InternalApi endpoint; one endpoint may bind several
|
|
31
|
-
// points.
|
|
32
|
-
function buildHooks({ authConfig, createSystemContext, getAuth }) {
|
|
33
|
-
const userHooks = {};
|
|
34
|
-
(authConfig.hooks ?? []).forEach((hook)=>{
|
|
35
|
-
if (type.isNone(userHooks[hook.point])) {
|
|
36
|
-
userHooks[hook.point] = [];
|
|
37
|
-
}
|
|
38
|
-
userHooks[hook.point].push(hook);
|
|
39
|
-
});
|
|
40
|
-
if (Object.keys(userHooks).length > 0 && type.isNone(createSystemContext)) {
|
|
41
|
-
throw new LowdefyInternalError('Auth hooks are configured but no createSystemContext factory was provided to getBetterAuthConfig.');
|
|
42
|
-
}
|
|
43
|
-
const engineHooks = buildEngineHooks({
|
|
44
|
-
authConfig,
|
|
45
|
-
getAuth
|
|
46
|
-
});
|
|
47
|
-
const databaseHooks = {};
|
|
48
|
-
let afterEmailVerification;
|
|
49
|
-
let phoneVerified;
|
|
50
|
-
let sendPhoneOtp;
|
|
51
|
-
let sendPhonePasswordResetOtp;
|
|
52
|
-
const points = new Set([
|
|
53
|
-
...Object.keys(engineHooks),
|
|
54
|
-
...Object.keys(userHooks)
|
|
55
|
-
]);
|
|
56
|
-
points.forEach((point)=>{
|
|
57
|
-
const pointDef = authHookPoints[point];
|
|
58
|
-
const hooks = [
|
|
59
|
-
...engineHooks[point] ?? []
|
|
60
|
-
];
|
|
61
|
-
(userHooks[point] ?? []).forEach((userHook)=>{
|
|
62
|
-
const dispatch = createHookDispatch({
|
|
63
|
-
createSystemContext,
|
|
64
|
-
getAuth,
|
|
65
|
-
hook: userHook
|
|
66
|
-
});
|
|
67
|
-
hooks.push(pointDef.timing === 'before' ? createUserBeforeHook({
|
|
68
|
-
dispatch,
|
|
69
|
-
hook: userHook
|
|
70
|
-
}) : createUserAfterHook({
|
|
71
|
-
dispatch
|
|
72
|
-
}));
|
|
73
|
-
});
|
|
74
|
-
if (pointDef.kind === 'database') {
|
|
75
|
-
const slot = pointDef.timing === 'before' ? composeBeforeSlot({
|
|
76
|
-
hooks
|
|
77
|
-
}) : composeAfterSlot({
|
|
78
|
-
hooks
|
|
79
|
-
});
|
|
80
|
-
set(databaseHooks, `${pointDef.model}.${pointDef.operation}.${pointDef.timing}`, slot);
|
|
81
|
-
}
|
|
82
|
-
if (point === 'email.verified') {
|
|
83
|
-
afterEmailVerification = composeAfterSlot({
|
|
84
|
-
hooks
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
if (point === 'phone.otp.send') {
|
|
88
|
-
sendPhoneOtp = composeAfterSlot({
|
|
89
|
-
hooks
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
if (point === 'phone.passwordReset.send') {
|
|
93
|
-
sendPhonePasswordResetOtp = composeAfterSlot({
|
|
94
|
-
hooks
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
if (point === 'phone.verified') {
|
|
98
|
-
phoneVerified = composeAfterSlot({
|
|
99
|
-
hooks
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
return {
|
|
104
|
-
afterEmailVerification,
|
|
105
|
-
databaseHooks,
|
|
106
|
-
phoneVerified,
|
|
107
|
-
sendPhoneOtp,
|
|
108
|
-
sendPhonePasswordResetOtp
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
export default buildHooks;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
-
// BetterAuth exposes one callback per database point, so each point resolves
|
|
17
|
-
// to a single composed slot - engine hooks first, then the user hooks in
|
|
18
|
-
// array order. The slot threads the record: what one hook returns is passed
|
|
19
|
-
// to the next, and the final record is handed back to BetterAuth as
|
|
20
|
-
// { data }. The first hook
|
|
21
|
-
// to veto short-circuits - a returned false aborts the write, and a thrown
|
|
22
|
-
// error propagates; there is no cross-hook transaction, so external
|
|
23
|
-
// side-effects of earlier hooks are not rolled back.
|
|
24
|
-
function composeBeforeSlot({ hooks }) {
|
|
25
|
-
return async function beforeSlot(data, ctx) {
|
|
26
|
-
let record = data;
|
|
27
|
-
for (const hook of hooks){
|
|
28
|
-
const result = await hook(record, ctx);
|
|
29
|
-
if (result === false) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
if (type.isObject(result) && 'data' in result) {
|
|
33
|
-
record = result.data;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
data: record
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
export default composeBeforeSlot;
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import invokeEndpoint from '../../endpoints/invokeEndpoint.js';
|
|
16
|
-
import authHookPoints from './authHookPoints.js';
|
|
17
|
-
// Runs a user hook: builds the point's payload from the hook data, builds a
|
|
18
|
-
// fresh system context, and invokes the bound InternalApi endpoint. Returns
|
|
19
|
-
// the routine's terminal envelope ({ status, response?, error? }); the
|
|
20
|
-
// translation to BetterAuth's hook contract lives in createUserBeforeHook /
|
|
21
|
-
// createUserAfterHook.
|
|
22
|
-
function createHookDispatch({ createSystemContext, getAuth, hook }) {
|
|
23
|
-
const { buildPayload } = authHookPoints[hook.point];
|
|
24
|
-
return async function dispatchHook(data, ctx) {
|
|
25
|
-
const start = performance.now();
|
|
26
|
-
// point is a reserved envelope key injected onto every payload so a
|
|
27
|
-
// routine bound to multiple points (which may hand identical domain data,
|
|
28
|
-
// e.g. user.create.before and email.verified both give { user }) can tell
|
|
29
|
-
// which point fired. No point's buildPayload produces a "point" field, so
|
|
30
|
-
// the spread never collides.
|
|
31
|
-
const payload = {
|
|
32
|
-
...await buildPayload(data, ctx),
|
|
33
|
-
point: hook.point
|
|
34
|
-
};
|
|
35
|
-
// getAuth resolves the BetterAuth instance lazily at fire time - hooks
|
|
36
|
-
// only fire after construction completes, so the accessor is always
|
|
37
|
-
// resolved by now. This is the same chicken-and-egg fix used by the
|
|
38
|
-
// engine hooks (e.g. createActiveOrgPolicyHook): the instance cannot be
|
|
39
|
-
// passed in directly because building it is what registers this hook.
|
|
40
|
-
const context = createSystemContext({
|
|
41
|
-
auth: getAuth()
|
|
42
|
-
});
|
|
43
|
-
const result = await invokeEndpoint(context, {
|
|
44
|
-
endpointId: hook.endpointId,
|
|
45
|
-
payload,
|
|
46
|
-
endpointDepth: 0
|
|
47
|
-
});
|
|
48
|
-
context.logger.debug({
|
|
49
|
-
event: 'debug_auth_hook',
|
|
50
|
-
hookId: hook.id,
|
|
51
|
-
point: hook.point,
|
|
52
|
-
endpointId: hook.endpointId,
|
|
53
|
-
status: result.status,
|
|
54
|
-
elapsed_ms: Math.round((performance.now() - start) * 100) / 100
|
|
55
|
-
});
|
|
56
|
-
return result;
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
export default createHookDispatch;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import { APIError } from 'better-auth/api';
|
|
16
|
-
// Translates the routine's terminal control for after and synthetic points.
|
|
17
|
-
// These fire after the write committed, so `:return` data is ignored and a
|
|
18
|
-
// `:reject`/throw surfaces as an operational error on the underlying
|
|
19
|
-
// operation - hook routines wrap fallible fire-and-forget work in `:try`.
|
|
20
|
-
function createUserAfterHook({ dispatch }) {
|
|
21
|
-
return async function userAfterHook(data, ctx) {
|
|
22
|
-
const result = await dispatch(data, ctx);
|
|
23
|
-
if (result.status === 'reject') {
|
|
24
|
-
throw new APIError('BAD_REQUEST', {
|
|
25
|
-
message: result.error.message
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
if (result.status === 'error') {
|
|
29
|
-
throw result.error;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
export default createUserAfterHook;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import { APIError } from 'better-auth/api';
|
|
16
|
-
import { type } from '@lowdefy/helpers';
|
|
17
|
-
import { ConfigError } from '@lowdefy/errors';
|
|
18
|
-
// Translates the routine's terminal control into BetterAuth's before-hook
|
|
19
|
-
// contract. The routine `:return`s plain data - the ceremony lives here:
|
|
20
|
-
// returned data replaces the record ({ data } wrap), a fall-through leaves
|
|
21
|
-
// the record unchanged, and `:reject`/throw aborts the write.
|
|
22
|
-
function createUserBeforeHook({ dispatch, hook }) {
|
|
23
|
-
return async function userBeforeHook(data, ctx) {
|
|
24
|
-
const result = await dispatch(data, ctx);
|
|
25
|
-
if (result.status === 'return') {
|
|
26
|
-
if (!type.isObject(result.response)) {
|
|
27
|
-
throw new ConfigError(`Auth hook "${hook.id}" at point "${hook.point}" returned a ${type.typeOf(result.response)}. A before hook ":return" value replaces the record being written, so it should be an object.`, {
|
|
28
|
-
received: result.response
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
return {
|
|
32
|
-
data: result.response
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
if (result.status === 'reject') {
|
|
36
|
-
throw new APIError('BAD_REQUEST', {
|
|
37
|
-
message: result.error.message
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
if (result.status === 'error') {
|
|
41
|
-
throw result.error;
|
|
42
|
-
}
|
|
43
|
-
// The routine fell through without a terminal control - record unchanged.
|
|
44
|
-
return undefined;
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
export default createUserBeforeHook;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
-
// Fetches the subject user for points whose payload catalog includes a user
|
|
17
|
-
// the database hook does not receive directly (session and account writes
|
|
18
|
-
// carry only a userId). Reads through BetterAuth's own internal adapter from
|
|
19
|
-
// the hook's endpoint context; null when the write runs outside a request
|
|
20
|
-
// lifecycle and no context is available.
|
|
21
|
-
async function findHookUser({ ctx, userId }) {
|
|
22
|
-
if (type.isNone(userId) || type.isNone(ctx?.context?.internalAdapter?.findUserById)) {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
return await ctx.context.internalAdapter.findUserById(userId) ?? null;
|
|
26
|
-
}
|
|
27
|
-
export default findHookUser;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ // Fixed mapping from BetterAuth models to physical collection names - the
|
|
16
|
-
// user-* convention. Not configurable: modules that read auth collections
|
|
17
|
-
// natively are portable across apps only if these names are stable.
|
|
18
|
-
const modelNames = {
|
|
19
|
-
user: 'users',
|
|
20
|
-
session: 'user-sessions',
|
|
21
|
-
account: 'user-accounts',
|
|
22
|
-
verification: 'user-verifications',
|
|
23
|
-
organization: 'user-organizations',
|
|
24
|
-
member: 'user-members',
|
|
25
|
-
invitation: 'user-invitations',
|
|
26
|
-
twoFactor: 'user-two-factors',
|
|
27
|
-
passkey: 'user-passkeys'
|
|
28
|
-
};
|
|
29
|
-
export default modelNames;
|