@unifiedcommerce/core 0.3.2 → 0.3.4
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/auth/middleware.d.ts.map +1 -1
- package/dist/auth/middleware.js +1 -0
- package/dist/auth/setup.d.ts +25 -65
- package/dist/auth/setup.d.ts.map +1 -1
- package/dist/auth/setup.js +7 -20
- package/package.json +1 -1
- package/src/auth/middleware.ts +1 -0
- package/src/auth/setup.ts +30 -79
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/auth/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9C,OAAO,KAAK,EAAmB,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE1E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AA0B/C,wBAAgB,cAAc,CAC5B,IAAI,EAAE,YAAY,EAClB,MAAM,EAAE,cAAc,GACrB,iBAAiB,
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/auth/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9C,OAAO,KAAK,EAAmB,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE1E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AA0B/C,wBAAgB,cAAc,CAC5B,IAAI,EAAE,YAAY,EAClB,MAAM,EAAE,cAAc,GACrB,iBAAiB,CAsInB"}
|
package/dist/auth/middleware.js
CHANGED
|
@@ -34,6 +34,7 @@ export function authMiddleware(auth, config) {
|
|
|
34
34
|
try {
|
|
35
35
|
const org = await auth.api.getFullOrganization({
|
|
36
36
|
query: { organizationId: orgId ?? DEFAULT_ORG_ID },
|
|
37
|
+
headers: c.req.raw.headers,
|
|
37
38
|
});
|
|
38
39
|
if (org?.members) {
|
|
39
40
|
const membership = org.members.find((m) => m.userId === session.user.id);
|
package/dist/auth/setup.d.ts
CHANGED
|
@@ -1,69 +1,29 @@
|
|
|
1
|
+
import { betterAuth } from "better-auth";
|
|
2
|
+
import { apiKey } from "@better-auth/api-key";
|
|
3
|
+
import { organization, twoFactor, phoneNumber, jwt, bearer } from "better-auth/plugins";
|
|
1
4
|
import type { CommerceConfig } from "../config/types.js";
|
|
2
5
|
import type { DatabaseAdapter } from "../kernel/database/adapter.js";
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
organizationId: string;
|
|
25
|
-
};
|
|
26
|
-
}) => Promise<{
|
|
27
|
-
id: string;
|
|
28
|
-
name: string;
|
|
29
|
-
members: OrgMember[];
|
|
30
|
-
} | null>;
|
|
31
|
-
listMembers?: (input: {
|
|
32
|
-
query: {
|
|
33
|
-
organizationId: string;
|
|
34
|
-
};
|
|
35
|
-
}) => Promise<OrgMember[]>;
|
|
36
|
-
verifyApiKey?: (input: {
|
|
37
|
-
body: {
|
|
38
|
-
key: string;
|
|
39
|
-
permissions?: Record<string, string[]>;
|
|
40
|
-
};
|
|
41
|
-
}) => Promise<{
|
|
42
|
-
valid: boolean;
|
|
43
|
-
error: {
|
|
44
|
-
message: string;
|
|
45
|
-
code: string;
|
|
46
|
-
} | null;
|
|
47
|
-
key: Record<string, unknown> | null;
|
|
48
|
-
}>;
|
|
49
|
-
createApiKey?: (input: {
|
|
50
|
-
body: {
|
|
51
|
-
configId?: string;
|
|
52
|
-
name?: string;
|
|
53
|
-
permissions?: Record<string, string[]>;
|
|
54
|
-
userId?: string;
|
|
55
|
-
organizationId?: string;
|
|
56
|
-
};
|
|
57
|
-
headers?: Headers;
|
|
58
|
-
}) => Promise<{
|
|
59
|
-
key: string;
|
|
60
|
-
id: string;
|
|
61
|
-
}>;
|
|
62
|
-
/** Allow access to other Better Auth API methods added by plugins */
|
|
63
|
-
[key: string]: unknown;
|
|
64
|
-
};
|
|
65
|
-
options?: Record<string, unknown>;
|
|
66
|
-
$context?: Promise<unknown>;
|
|
67
|
-
}
|
|
6
|
+
/**
|
|
7
|
+
* The auth type is inferred from `typeof betterAuth(...)` with all plugins enabled.
|
|
8
|
+
* This gives us the full API surface without manual interface maintenance.
|
|
9
|
+
*
|
|
10
|
+
* We use a type-level-only reference (never executed) to capture the complete type.
|
|
11
|
+
*/
|
|
12
|
+
type FullBetterAuth = ReturnType<typeof betterAuth<{
|
|
13
|
+
plugins: [
|
|
14
|
+
ReturnType<typeof organization>,
|
|
15
|
+
ReturnType<typeof bearer>,
|
|
16
|
+
ReturnType<typeof jwt>,
|
|
17
|
+
ReturnType<typeof twoFactor>,
|
|
18
|
+
ReturnType<typeof apiKey>,
|
|
19
|
+
ReturnType<typeof phoneNumber>
|
|
20
|
+
];
|
|
21
|
+
}>>;
|
|
22
|
+
/**
|
|
23
|
+
* AuthInstance — inferred from Better Auth with all plugins.
|
|
24
|
+
* No manual type maintenance needed.
|
|
25
|
+
*/
|
|
26
|
+
export type AuthInstance = FullBetterAuth;
|
|
68
27
|
export declare function createAuth(db: DatabaseAdapter, config: CommerceConfig): AuthInstance;
|
|
28
|
+
export {};
|
|
69
29
|
//# sourceMappingURL=setup.d.ts.map
|
package/dist/auth/setup.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/auth/setup.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/auth/setup.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AACxF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAiBrE;;;;;GAKG;AACH,KAAK,cAAc,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC;IACjD,OAAO,EAAE;QACP,UAAU,CAAC,OAAO,YAAY,CAAC;QAC/B,UAAU,CAAC,OAAO,MAAM,CAAC;QACzB,UAAU,CAAC,OAAO,GAAG,CAAC;QACtB,UAAU,CAAC,OAAO,SAAS,CAAC;QAC5B,UAAU,CAAC,OAAO,MAAM,CAAC;QACzB,UAAU,CAAC,OAAO,WAAW,CAAC;KAC/B,CAAC;CACH,CAAC,CAAC,CAAC;AAEJ;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,cAAc,CAAC;AAE1C,wBAAgB,UAAU,CACxB,EAAE,EAAE,eAAe,EACnB,MAAM,EAAE,cAAc,GACrB,YAAY,CA+Gd"}
|
package/dist/auth/setup.js
CHANGED
|
@@ -4,24 +4,17 @@ import { apiKey } from "@better-auth/api-key";
|
|
|
4
4
|
import { organization, twoFactor, phoneNumber, jwt, bearer } from "better-auth/plugins";
|
|
5
5
|
import * as authSchema from "./auth-schema.js";
|
|
6
6
|
function resolveAuthDbProvider(provider) {
|
|
7
|
-
if (provider === "postgres" ||
|
|
8
|
-
provider === "postgresql" ||
|
|
9
|
-
provider === "pg") {
|
|
7
|
+
if (provider === "postgres" || provider === "postgresql" || provider === "pg")
|
|
10
8
|
return "pg";
|
|
11
|
-
|
|
12
|
-
if (provider === "mysql") {
|
|
9
|
+
if (provider === "mysql")
|
|
13
10
|
return "mysql";
|
|
14
|
-
|
|
15
|
-
if (provider === "sqlite") {
|
|
11
|
+
if (provider === "sqlite")
|
|
16
12
|
return "sqlite";
|
|
17
|
-
}
|
|
18
|
-
throw new Error(`Unsupported auth database provider "${provider}". Expected one of: postgres, mysql, sqlite.`);
|
|
13
|
+
throw new Error(`Unsupported auth database provider "${provider}".`);
|
|
19
14
|
}
|
|
20
15
|
export function createAuth(db, config) {
|
|
21
16
|
const plugins = [
|
|
22
17
|
organization({
|
|
23
|
-
// Better Auth's Role includes `authorize` and `statements` fields that
|
|
24
|
-
// our RoleDefinition doesn't have. Double-cast is required — upstream type gap.
|
|
25
18
|
roles: (config.auth?.roles ?? {}),
|
|
26
19
|
}),
|
|
27
20
|
bearer(),
|
|
@@ -30,7 +23,6 @@ export function createAuth(db, config) {
|
|
|
30
23
|
if (config.auth?.twoFactor?.enabled) {
|
|
31
24
|
plugins.push(twoFactor({ issuer: config.storeName ?? "UnifiedCommerce" }));
|
|
32
25
|
}
|
|
33
|
-
// Configure API key plugin — one config per defined scope, or a single default config.
|
|
34
26
|
const scopes = config.auth?.apiKeyScopes;
|
|
35
27
|
if (scopes && Object.keys(scopes).length > 0) {
|
|
36
28
|
const apiKeyConfigs = Object.entries(scopes).map(([scopeId, scope]) => ({
|
|
@@ -62,11 +54,8 @@ export function createAuth(db, config) {
|
|
|
62
54
|
},
|
|
63
55
|
}));
|
|
64
56
|
}
|
|
65
|
-
// API key support can be attached via external plugin package in newer better-auth versions.
|
|
66
57
|
try {
|
|
67
58
|
const auth = betterAuth({
|
|
68
|
-
// Better Auth's drizzle adapter expects a plain object, not PgDatabase.
|
|
69
|
-
// Double-cast required — PgDatabase has no index signature.
|
|
70
59
|
database: drizzleAdapter(db.db, {
|
|
71
60
|
provider: resolveAuthDbProvider(db.provider),
|
|
72
61
|
schema: authSchema,
|
|
@@ -100,7 +89,7 @@ export function createAuth(db, config) {
|
|
|
100
89
|
updateAge: 60 * 60 * 24,
|
|
101
90
|
cookieCache: {
|
|
102
91
|
enabled: true,
|
|
103
|
-
maxAge: 60 * 5,
|
|
92
|
+
maxAge: 60 * 5,
|
|
104
93
|
},
|
|
105
94
|
},
|
|
106
95
|
advanced: {
|
|
@@ -115,10 +104,8 @@ export function createAuth(db, config) {
|
|
|
115
104
|
},
|
|
116
105
|
},
|
|
117
106
|
});
|
|
118
|
-
//
|
|
119
|
-
//
|
|
120
|
-
// Double-cast is the accepted pattern — same approach used by PayloadCMS.
|
|
121
|
-
// @see https://github.com/better-auth/better-auth/discussions
|
|
107
|
+
// The runtime return matches FullBetterAuth structurally — plugins may differ
|
|
108
|
+
// at runtime (conditional) but the API surface is a superset.
|
|
122
109
|
return auth;
|
|
123
110
|
}
|
|
124
111
|
catch (error) {
|
package/package.json
CHANGED
package/src/auth/middleware.ts
CHANGED
package/src/auth/setup.ts
CHANGED
|
@@ -10,80 +10,39 @@ import * as authSchema from "./auth-schema.js";
|
|
|
10
10
|
type BetterAuthDbProvider = "pg" | "mysql" | "sqlite";
|
|
11
11
|
|
|
12
12
|
function resolveAuthDbProvider(provider: string): BetterAuthDbProvider {
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
) {
|
|
18
|
-
return "pg";
|
|
19
|
-
}
|
|
20
|
-
if (provider === "mysql") {
|
|
21
|
-
return "mysql";
|
|
22
|
-
}
|
|
23
|
-
if (provider === "sqlite") {
|
|
24
|
-
return "sqlite";
|
|
25
|
-
}
|
|
26
|
-
throw new Error(
|
|
27
|
-
`Unsupported auth database provider "${provider}". Expected one of: postgres, mysql, sqlite.`,
|
|
28
|
-
);
|
|
13
|
+
if (provider === "postgres" || provider === "postgresql" || provider === "pg") return "pg";
|
|
14
|
+
if (provider === "mysql") return "mysql";
|
|
15
|
+
if (provider === "sqlite") return "sqlite";
|
|
16
|
+
throw new Error(`Unsupported auth database provider "${provider}".`);
|
|
29
17
|
}
|
|
30
18
|
|
|
31
19
|
interface AuthEmailPayload {
|
|
32
|
-
user: {
|
|
33
|
-
email: string;
|
|
34
|
-
name: string | null;
|
|
35
|
-
};
|
|
20
|
+
user: { email: string; name: string | null };
|
|
36
21
|
url: string;
|
|
37
22
|
}
|
|
38
23
|
|
|
39
|
-
/**
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
getFullOrganization?: (input: { query: { organizationId: string } }) => Promise<{
|
|
56
|
-
id: string;
|
|
57
|
-
name: string;
|
|
58
|
-
members: OrgMember[];
|
|
59
|
-
} | null>;
|
|
60
|
-
listMembers?: (input: { query: { organizationId: string } }) => Promise<OrgMember[]>;
|
|
61
|
-
|
|
62
|
-
// API key plugin methods
|
|
63
|
-
verifyApiKey?: (input: {
|
|
64
|
-
body: { key: string; permissions?: Record<string, string[]> };
|
|
65
|
-
}) => Promise<{
|
|
66
|
-
valid: boolean;
|
|
67
|
-
error: { message: string; code: string } | null;
|
|
68
|
-
key: Record<string, unknown> | null;
|
|
69
|
-
}>;
|
|
70
|
-
createApiKey?: (input: {
|
|
71
|
-
body: {
|
|
72
|
-
configId?: string;
|
|
73
|
-
name?: string;
|
|
74
|
-
permissions?: Record<string, string[]>;
|
|
75
|
-
userId?: string;
|
|
76
|
-
organizationId?: string;
|
|
77
|
-
};
|
|
78
|
-
headers?: Headers;
|
|
79
|
-
}) => Promise<{ key: string; id: string }>;
|
|
24
|
+
/**
|
|
25
|
+
* The auth type is inferred from `typeof betterAuth(...)` with all plugins enabled.
|
|
26
|
+
* This gives us the full API surface without manual interface maintenance.
|
|
27
|
+
*
|
|
28
|
+
* We use a type-level-only reference (never executed) to capture the complete type.
|
|
29
|
+
*/
|
|
30
|
+
type FullBetterAuth = ReturnType<typeof betterAuth<{
|
|
31
|
+
plugins: [
|
|
32
|
+
ReturnType<typeof organization>,
|
|
33
|
+
ReturnType<typeof bearer>,
|
|
34
|
+
ReturnType<typeof jwt>,
|
|
35
|
+
ReturnType<typeof twoFactor>,
|
|
36
|
+
ReturnType<typeof apiKey>,
|
|
37
|
+
ReturnType<typeof phoneNumber>,
|
|
38
|
+
];
|
|
39
|
+
}>>;
|
|
80
40
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
41
|
+
/**
|
|
42
|
+
* AuthInstance — inferred from Better Auth with all plugins.
|
|
43
|
+
* No manual type maintenance needed.
|
|
44
|
+
*/
|
|
45
|
+
export type AuthInstance = FullBetterAuth;
|
|
87
46
|
|
|
88
47
|
export function createAuth(
|
|
89
48
|
db: DatabaseAdapter,
|
|
@@ -98,8 +57,6 @@ export function createAuth(
|
|
|
98
57
|
| ReturnType<typeof bearer>
|
|
99
58
|
> = [
|
|
100
59
|
organization({
|
|
101
|
-
// Better Auth's Role includes `authorize` and `statements` fields that
|
|
102
|
-
// our RoleDefinition doesn't have. Double-cast is required — upstream type gap.
|
|
103
60
|
roles: (config.auth?.roles ?? {}) as unknown as Record<string, Role | undefined>,
|
|
104
61
|
}),
|
|
105
62
|
bearer(),
|
|
@@ -110,7 +67,6 @@ export function createAuth(
|
|
|
110
67
|
plugins.push(twoFactor({ issuer: config.storeName ?? "UnifiedCommerce" }));
|
|
111
68
|
}
|
|
112
69
|
|
|
113
|
-
// Configure API key plugin — one config per defined scope, or a single default config.
|
|
114
70
|
const scopes = config.auth?.apiKeyScopes;
|
|
115
71
|
if (scopes && Object.keys(scopes).length > 0) {
|
|
116
72
|
const apiKeyConfigs = Object.entries(scopes).map(([scopeId, scope]) => ({
|
|
@@ -143,12 +99,8 @@ export function createAuth(
|
|
|
143
99
|
}));
|
|
144
100
|
}
|
|
145
101
|
|
|
146
|
-
// API key support can be attached via external plugin package in newer better-auth versions.
|
|
147
|
-
|
|
148
102
|
try {
|
|
149
103
|
const auth = betterAuth({
|
|
150
|
-
// Better Auth's drizzle adapter expects a plain object, not PgDatabase.
|
|
151
|
-
// Double-cast required — PgDatabase has no index signature.
|
|
152
104
|
database: drizzleAdapter(db.db as unknown as Record<string, unknown>, {
|
|
153
105
|
provider: resolveAuthDbProvider(db.provider),
|
|
154
106
|
schema: authSchema,
|
|
@@ -180,7 +132,7 @@ export function createAuth(
|
|
|
180
132
|
updateAge: 60 * 60 * 24,
|
|
181
133
|
cookieCache: {
|
|
182
134
|
enabled: true,
|
|
183
|
-
maxAge: 60 * 5,
|
|
135
|
+
maxAge: 60 * 5,
|
|
184
136
|
},
|
|
185
137
|
},
|
|
186
138
|
advanced: {
|
|
@@ -195,10 +147,9 @@ export function createAuth(
|
|
|
195
147
|
},
|
|
196
148
|
},
|
|
197
149
|
});
|
|
198
|
-
|
|
199
|
-
//
|
|
200
|
-
//
|
|
201
|
-
// @see https://github.com/better-auth/better-auth/discussions
|
|
150
|
+
|
|
151
|
+
// The runtime return matches FullBetterAuth structurally — plugins may differ
|
|
152
|
+
// at runtime (conditional) but the API surface is a superset.
|
|
202
153
|
return auth as unknown as AuthInstance;
|
|
203
154
|
} catch (error) {
|
|
204
155
|
const message =
|