@lowdefy/plugin-better-auth 0.0.0-experimental-20260714122106

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.
@@ -0,0 +1,98 @@
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
+ import createActingMemberAdapter from './createActingMemberAdapter.js';
17
+ import getPluginEndpoint from './getPluginEndpoint.js';
18
+ import invokeEndpoint from './invokeEndpoint.js';
19
+ // Admin steps carry server authority: the hosting Lowdefy endpoint's auth.api.roles
20
+ // gate is the only authorization, so BetterAuth's own caller access control is
21
+ // satisfied mechanically by injecting an acting session with role "admin" (admin
22
+ // plugin) and a virtual owner member row (org plugin). Nothing here is written to
23
+ // the database - the acting identity exists only for the duration of the call.
24
+ async function callPluginEndpoint({ acting, auth, body, endpointKey, pluginId, query }) {
25
+ const authContext = await auth.$context;
26
+ const endpoint = getPluginEndpoint({
27
+ authContext,
28
+ endpointKey,
29
+ pluginId
30
+ });
31
+ let actingUser;
32
+ if (type.isNone(acting.user)) {
33
+ actingUser = {
34
+ id: 'lowdefy:system',
35
+ email: 'system@lowdefy.internal',
36
+ name: 'Lowdefy System',
37
+ emailVerified: true,
38
+ image: null,
39
+ role: 'admin'
40
+ };
41
+ } else {
42
+ actingUser = {
43
+ id: acting.user.id,
44
+ email: acting.user.email,
45
+ name: acting.user.name,
46
+ image: acting.user.image,
47
+ emailVerified: acting.user.emailVerified,
48
+ role: 'admin'
49
+ };
50
+ }
51
+ const actingSession = {
52
+ session: {
53
+ id: 'lowdefy:system-session',
54
+ token: 'lowdefy:system-session',
55
+ userId: actingUser.id,
56
+ activeOrganizationId: acting.user?.activeOrganizationId ?? null,
57
+ expiresAt: new Date(Date.now() + 60000),
58
+ createdAt: new Date(),
59
+ updatedAt: new Date()
60
+ },
61
+ user: actingUser
62
+ };
63
+ const context = {
64
+ ...authContext,
65
+ adapter: createActingMemberAdapter({
66
+ actingUser,
67
+ adapter: authContext.adapter
68
+ }),
69
+ // The admin plugin re-fetches the session from headers when a server-side
70
+ // session store exists (options.database or secondaryStorage). Forcing both
71
+ // undefined on the per-call options makes it honor the injected session.
72
+ options: {
73
+ ...authContext.options,
74
+ database: undefined,
75
+ secondaryStorage: undefined
76
+ },
77
+ session: actingSession
78
+ };
79
+ if (!type.isUndefined(query)) {
80
+ return invokeEndpoint({
81
+ endpoint,
82
+ input: {
83
+ query,
84
+ headers: new Headers(),
85
+ context
86
+ }
87
+ });
88
+ }
89
+ return invokeEndpoint({
90
+ endpoint,
91
+ input: {
92
+ body,
93
+ headers: new Headers(),
94
+ context
95
+ }
96
+ });
97
+ }
98
+ export default callPluginEndpoint;
@@ -0,0 +1,51 @@
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
+ // Org mutation endpoints authorize the CALLER by looking up their member row
17
+ // (findMemberByOrgId -> adapter.findOne on "member" by userId + organizationId,
18
+ // joined to the user). Admin steps act with server authority, so the acting user
19
+ // has no real member row. This wrapper intercepts exactly that lookup and returns
20
+ // a virtual owner member (in-memory only; never written), which passes the org
21
+ // plugin's hasPermission check via the creator role. Every other adapter call
22
+ // delegates to the real adapter unchanged.
23
+ function createActingMemberAdapter({ actingUser, adapter }) {
24
+ async function findOne(args) {
25
+ if (args.model === 'member' && type.isArray(args.where)) {
26
+ const userIdClause = args.where.find((clause)=>clause.field === 'userId');
27
+ const organizationIdClause = args.where.find((clause)=>clause.field === 'organizationId');
28
+ if (!type.isNone(userIdClause) && userIdClause.value === actingUser.id && !type.isNone(organizationIdClause)) {
29
+ return {
30
+ id: 'lowdefy:system-member',
31
+ userId: actingUser.id,
32
+ organizationId: organizationIdClause.value,
33
+ role: 'owner',
34
+ createdAt: new Date(),
35
+ user: {
36
+ id: actingUser.id,
37
+ name: actingUser.name,
38
+ email: actingUser.email,
39
+ image: actingUser.image
40
+ }
41
+ };
42
+ }
43
+ }
44
+ return adapter.findOne(args);
45
+ }
46
+ return {
47
+ ...adapter,
48
+ findOne
49
+ };
50
+ }
51
+ export default createActingMemberAdapter;
@@ -0,0 +1,27 @@
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
+ function getPluginEndpoint({ authContext, endpointKey, pluginId }) {
17
+ const plugin = (authContext.options.plugins ?? []).find((p)=>p.id === pluginId);
18
+ if (type.isNone(plugin)) {
19
+ throw new Error(`BetterAuth plugin "${pluginId}" is not configured on the auth instance.`);
20
+ }
21
+ const endpoint = (plugin.endpoints ?? {})[endpointKey];
22
+ if (type.isNone(endpoint)) {
23
+ throw new Error(`BetterAuth plugin "${pluginId}" has no endpoint "${endpointKey}".`);
24
+ }
25
+ return endpoint;
26
+ }
27
+ export default getPluginEndpoint;
@@ -0,0 +1,30 @@
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
+ async function invokeEndpoint({ endpoint, input }) {
17
+ try {
18
+ return await endpoint(input);
19
+ } catch (error) {
20
+ // Surface BetterAuth APIError rail messages (validation, ROLE_NOT_FOUND, etc.)
21
+ // verbatim - the step interface layer wraps further.
22
+ if (!type.isNone(error.status) && !type.isNone(error.body)) {
23
+ throw new Error(error.body?.message ?? error.body?.code ?? error.message, {
24
+ cause: error
25
+ });
26
+ }
27
+ throw error;
28
+ }
29
+ }
30
+ export default invokeEndpoint;
@@ -0,0 +1,33 @@
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
+ // Org-scoped steps default an omitted organizationId to the deployment's
17
+ // pinned organization - the org the engine ensures by slug at startup. An
18
+ // explicit organizationId always wins (what a multi-tenant admin module
19
+ // passes). Under the "tenant" policy there is no single implied org, so an
20
+ // omitted organizationId is a runtime error naming the fix.
21
+ function resolveOrganizationId({ organization, organizationId, step }) {
22
+ if (!type.isNone(organizationId)) {
23
+ return organizationId;
24
+ }
25
+ if (organization?.policy === 'tenant') {
26
+ throw new Error(`${step} requires an "organizationId" property under the "tenant" organizations policy - there is no pinned organization to default to. Set organizationId on the step properties.`);
27
+ }
28
+ if (type.isNone(organization?.pinned?.id)) {
29
+ throw new Error(`${step} could not default "organizationId" - the pinned organization is not resolved. Set organizationId on the step properties, or check that auth organizations are configured and the database is reachable.`);
30
+ }
31
+ return organization.pinned.id;
32
+ }
33
+ export default resolveOrganizationId;
@@ -0,0 +1,25 @@
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
+ // member.role stores multiple roles as one comma-separated string; step
17
+ // properties may also pass an array. Normalize either shape to a trimmed
18
+ // array of role names.
19
+ function splitRoles(role) {
20
+ const roles = type.isArray(role) ? role : [
21
+ role ?? ''
22
+ ];
23
+ return roles.flatMap((entry)=>String(entry).split(',')).map((entry)=>entry.trim()).filter(Boolean);
24
+ }
25
+ export default splitRoles;
@@ -0,0 +1,98 @@
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
+ import splitRoles from './splitRoles.js';
17
+ // The engine maintains user.role as an internal denormalization: BetterAuth's
18
+ // admin plugin resolves impersonation permissions from the user-level role
19
+ // field and never reads member.role. When the user's member roles in the
20
+ // pinned organization include the configured auth.userAdminRole, the engine
21
+ // writes that role name to user.role; when they lose it, the engine clears
22
+ // the value it wrote (a foreign value is left alone). Apps never read or
23
+ // write it; it exists so BetterAuth's own check passes. Pinned policy only -
24
+ // tenant semantics wait for a multi-tenant admin design. The write is
25
+ // adapter-direct so no user.update database hooks fire for the internal
26
+ // bookkeeping. The sync always recomputes from the pinned organization's
27
+ // member row - the source of truth - regardless of which organization the
28
+ // calling step wrote. The twin helper for the invitation-accept hook lives in
29
+ // @lowdefy/api (routes/auth/organizations/syncUserAdminRole.js).
30
+ async function syncUserAdminRole({ auth, organization, userAdminRole, userId }) {
31
+ if (type.isNone(userAdminRole) || type.isNone(userId)) {
32
+ return;
33
+ }
34
+ if (organization?.policy !== 'pinned') {
35
+ return;
36
+ }
37
+ if (type.isNone(organization.pinned?.id)) {
38
+ throw new Error('Could not sync the user-admin role - the pinned organization is not resolved.');
39
+ }
40
+ const { adapter } = await auth.$context;
41
+ const member = await adapter.findOne({
42
+ model: 'member',
43
+ where: [
44
+ {
45
+ field: 'userId',
46
+ value: userId
47
+ },
48
+ {
49
+ field: 'organizationId',
50
+ value: organization.pinned.id
51
+ }
52
+ ]
53
+ });
54
+ const holdsRole = !type.isNone(member) && splitRoles(member.role).includes(userAdminRole);
55
+ const user = await adapter.findOne({
56
+ model: 'user',
57
+ where: [
58
+ {
59
+ field: 'id',
60
+ value: userId
61
+ }
62
+ ]
63
+ });
64
+ // No user row means nothing to denormalize (the user was deleted between
65
+ // the member write and this sync) - not a failure.
66
+ if (type.isNone(user)) {
67
+ return;
68
+ }
69
+ if (holdsRole && user.role !== userAdminRole) {
70
+ await adapter.update({
71
+ model: 'user',
72
+ where: [
73
+ {
74
+ field: 'id',
75
+ value: userId
76
+ }
77
+ ],
78
+ update: {
79
+ role: userAdminRole
80
+ }
81
+ });
82
+ }
83
+ if (!holdsRole && user.role === userAdminRole) {
84
+ await adapter.update({
85
+ model: 'user',
86
+ where: [
87
+ {
88
+ field: 'id',
89
+ value: userId
90
+ }
91
+ ],
92
+ update: {
93
+ role: null
94
+ }
95
+ });
96
+ }
97
+ }
98
+ export default syncUserAdminRole;
package/dist/steps.js ADDED
@@ -0,0 +1,28 @@
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
+ */ export { default as BanUser } from './steps/BanUser.js';
16
+ export { default as CancelInvitation } from './steps/CancelInvitation.js';
17
+ export { default as CreateOrganization } from './steps/CreateOrganization.js';
18
+ export { default as DeleteUser } from './steps/DeleteUser.js';
19
+ export { default as InviteMember } from './steps/InviteMember.js';
20
+ export { default as ListMembers } from './steps/ListMembers.js';
21
+ export { default as ListUsers } from './steps/ListUsers.js';
22
+ export { default as RemoveMember } from './steps/RemoveMember.js';
23
+ export { default as RevokeUserSessions } from './steps/RevokeUserSessions.js';
24
+ export { default as UnbanUser } from './steps/UnbanUser.js';
25
+ export { default as UpdateMemberAttributes } from './steps/UpdateMemberAttributes.js';
26
+ export { default as UpdateMemberRoles } from './steps/UpdateMemberRoles.js';
27
+ export { default as UpdateUserAttributes } from './steps/UpdateUserAttributes.js';
28
+ export { default as UpdateUserProfile } from './steps/UpdateUserProfile.js';
package/dist/types.js ADDED
@@ -0,0 +1,76 @@
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
+ */ export default {
16
+ auth: {
17
+ providers: [
18
+ 'GenericOAuth',
19
+ 'Apple',
20
+ 'Atlassian',
21
+ 'Cognito',
22
+ 'Discord',
23
+ 'Dropbox',
24
+ 'Facebook',
25
+ 'Figma',
26
+ 'GitHub',
27
+ 'GitLab',
28
+ 'Google',
29
+ 'HuggingFace',
30
+ 'Kakao',
31
+ 'Kick',
32
+ 'Line',
33
+ 'Linear',
34
+ 'LinkedIn',
35
+ 'Microsoft',
36
+ 'Naver',
37
+ 'Notion',
38
+ 'PayPal',
39
+ 'Paybin',
40
+ 'Polar',
41
+ 'Railway',
42
+ 'Reddit',
43
+ 'Roblox',
44
+ 'Salesforce',
45
+ 'Slack',
46
+ 'Spotify',
47
+ 'TikTok',
48
+ 'Twitch',
49
+ 'Twitter',
50
+ 'VK',
51
+ 'Vercel',
52
+ 'WeChat',
53
+ 'Zoom'
54
+ ],
55
+ strategies: [
56
+ 'apiKey',
57
+ 'jwt'
58
+ ]
59
+ },
60
+ steps: [
61
+ 'BanUser',
62
+ 'CancelInvitation',
63
+ 'CreateOrganization',
64
+ 'DeleteUser',
65
+ 'InviteMember',
66
+ 'ListMembers',
67
+ 'ListUsers',
68
+ 'RemoveMember',
69
+ 'RevokeUserSessions',
70
+ 'UnbanUser',
71
+ 'UpdateMemberAttributes',
72
+ 'UpdateMemberRoles',
73
+ 'UpdateUserAttributes',
74
+ 'UpdateUserProfile'
75
+ ]
76
+ };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@lowdefy/plugin-better-auth",
3
+ "version": "0.0.0-experimental-20260714122106",
4
+ "license": "Apache-2.0",
5
+ "description": "Lowdefy providers, API auth strategies, and admin steps for the BetterAuth engine.",
6
+ "homepage": "https://lowdefy.com",
7
+ "keywords": [
8
+ "lowdefy",
9
+ "lowdefy plugin"
10
+ ],
11
+ "bugs": {
12
+ "url": "https://github.com/lowdefy/lowdefy/issues"
13
+ },
14
+ "contributors": [
15
+ {
16
+ "name": "Sam Tolmay",
17
+ "url": "https://github.com/SamTolmay"
18
+ },
19
+ {
20
+ "name": "Gerrie van Wyk",
21
+ "url": "https://github.com/Gervwyk"
22
+ }
23
+ ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/lowdefy/lowdefy.git"
27
+ },
28
+ "type": "module",
29
+ "exports": {
30
+ "./auth/providers": "./dist/auth/providers.js",
31
+ "./auth/strategies": "./dist/auth/strategies.js",
32
+ "./steps": "./dist/steps.js",
33
+ "./types": "./dist/types.js"
34
+ },
35
+ "files": [
36
+ "dist/*"
37
+ ],
38
+ "dependencies": {
39
+ "@lowdefy/helpers": "0.0.0-experimental-20260714122106",
40
+ "jose": "^6.2.3"
41
+ },
42
+ "devDependencies": {
43
+ "@jest/globals": "28.1.3",
44
+ "@swc/cli": "0.8.1",
45
+ "@swc/core": "1.15.32",
46
+ "@swc/jest": "0.2.39",
47
+ "jest": "28.1.3",
48
+ "jest-environment-node": "28.1.3"
49
+ },
50
+ "publishConfig": {
51
+ "access": "public"
52
+ },
53
+ "scripts": {
54
+ "build": "swc src --out-dir dist --config-file ../../../../.swcrc --cli-config-file ../../../../.swc-cli.json --copy-files",
55
+ "clean": "rm -rf dist",
56
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
57
+ }
58
+ }