jazz-tools 0.18.5 → 0.18.6
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/.turbo/turbo-build.log +53 -53
- package/CHANGELOG.md +16 -0
- package/dist/better-auth/auth/client.d.ts.map +1 -1
- package/dist/better-auth/auth/client.js +7 -1
- package/dist/better-auth/auth/client.js.map +1 -1
- package/dist/better-auth/auth/react.d.ts +0 -2145
- package/dist/better-auth/auth/react.d.ts.map +1 -1
- package/dist/better-auth/auth/react.js +2 -14
- package/dist/better-auth/auth/react.js.map +1 -1
- package/dist/better-auth/auth/server.d.ts.map +1 -1
- package/dist/better-auth/auth/server.js +73 -22
- package/dist/better-auth/auth/server.js.map +1 -1
- package/dist/better-auth/auth/tests/react.test.d.ts +2 -0
- package/dist/better-auth/auth/tests/react.test.d.ts.map +1 -0
- package/dist/{chunk-3LE7N6TH.js → chunk-45VKEOXG.js} +123 -81
- package/dist/chunk-45VKEOXG.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/inspector/{custom-element-WCY6D3QJ.js → custom-element-IBHKHN27.js} +19 -69
- package/dist/inspector/custom-element-IBHKHN27.js.map +1 -0
- package/dist/inspector/index.d.ts +5 -1
- package/dist/inspector/index.d.ts.map +1 -1
- package/dist/inspector/index.js +18 -17
- package/dist/inspector/index.js.map +1 -1
- package/dist/inspector/register-custom-element.js +1 -1
- package/dist/inspector/viewer/new-app.d.ts +0 -3
- package/dist/inspector/viewer/new-app.d.ts.map +1 -1
- package/dist/react-core/index.js +3 -1
- package/dist/react-core/index.js.map +1 -1
- package/dist/testing.js +2 -2
- package/dist/testing.js.map +1 -1
- package/dist/tools/coValues/inbox.d.ts +5 -5
- package/dist/tools/coValues/inbox.d.ts.map +1 -1
- package/dist/worker/index.d.ts +8 -2
- package/dist/worker/index.d.ts.map +1 -1
- package/dist/worker/index.js +7 -3
- package/dist/worker/index.js.map +1 -1
- package/package.json +4 -4
- package/src/better-auth/auth/client.ts +8 -2
- package/src/better-auth/auth/react.tsx +2 -51
- package/src/better-auth/auth/server.ts +94 -24
- package/src/better-auth/auth/tests/client.test.ts +92 -4
- package/src/better-auth/auth/tests/react.test.tsx +43 -0
- package/src/better-auth/auth/tests/server.test.ts +276 -98
- package/src/inspector/custom-element.tsx +1 -1
- package/src/inspector/index.tsx +44 -0
- package/src/inspector/viewer/new-app.tsx +0 -18
- package/src/tools/coValues/inbox.ts +190 -108
- package/src/tools/testing.ts +1 -1
- package/src/tools/tests/coFeed.test.ts +33 -22
- package/src/tools/tests/coList.test.ts +6 -4
- package/src/tools/tests/coMap.test.ts +13 -5
- package/src/tools/tests/exportImport.test.ts +3 -1
- package/src/tools/tests/groupsAndAccounts.test.ts +56 -44
- package/src/tools/tests/inbox.test.ts +293 -31
- package/src/worker/index.ts +15 -5
- package/tsup.config.ts +1 -1
- package/dist/chunk-3LE7N6TH.js.map +0 -1
- package/dist/inspector/custom-element-WCY6D3QJ.js.map +0 -1
- package/src/inspector/index.ts +0 -23
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../../src/better-auth/auth/react.tsx"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../../src/better-auth/auth/react.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAGtD,OAAO,EAAE,KAAK,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C,KAAK,UAAU,GAAG,UAAU,CAC1B,OAAO,gBAAgB,CAAC;IACtB,OAAO,EAAE,CAAC,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC,CAAC;CAChD,CAAC,CACH,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAAC,EAC3B,QAAQ,EACR,gBAAgB,GACjB,EAAE,iBAAiB,CAAC;IACnB,gBAAgB,EAAE,UAAU,CAAC;CAC9B,CAAC,6BAmBD"}
|
@@ -1,15 +1,8 @@
|
|
1
1
|
"use client";
|
2
2
|
|
3
3
|
// src/better-auth/auth/react.tsx
|
4
|
-
import {
|
5
|
-
JazzReactProvider,
|
6
|
-
useAuthSecretStorage,
|
7
|
-
useJazzContext
|
8
|
-
} from "jazz-tools/react";
|
4
|
+
import { useAuthSecretStorage, useJazzContext } from "jazz-tools/react-core";
|
9
5
|
import { useEffect } from "react";
|
10
|
-
import { createContext } from "react";
|
11
|
-
import { jsx } from "react/jsx-runtime";
|
12
|
-
var AuthContext = createContext(null);
|
13
6
|
function AuthProvider({
|
14
7
|
children,
|
15
8
|
betterAuthClient
|
@@ -29,12 +22,7 @@ function AuthProvider({
|
|
29
22
|
}, [betterAuthClient, context, authSecretStorage]);
|
30
23
|
return children;
|
31
24
|
}
|
32
|
-
var JazzReactProviderWithBetterAuth = (props) => {
|
33
|
-
return /* @__PURE__ */ jsx(JazzReactProvider, { ...props, children: /* @__PURE__ */ jsx(AuthProvider, { betterAuthClient: props.betterAuthClient, children: props.children }) });
|
34
|
-
};
|
35
25
|
export {
|
36
|
-
|
37
|
-
AuthProvider,
|
38
|
-
JazzReactProviderWithBetterAuth
|
26
|
+
AuthProvider
|
39
27
|
};
|
40
28
|
//# sourceMappingURL=react.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/better-auth/auth/react.tsx"],"sourcesContent":["\"use client\";\n\nimport
|
1
|
+
{"version":3,"sources":["../../../src/better-auth/auth/react.tsx"],"sourcesContent":["\"use client\";\n\nimport { createAuthClient } from \"better-auth/client\";\nimport { useAuthSecretStorage, useJazzContext } from \"jazz-tools/react-core\";\nimport { useEffect } from \"react\";\nimport { type PropsWithChildren } from \"react\";\nimport { jazzPluginClient } from \"./client.js\";\n\ntype AuthClient = ReturnType<\n typeof createAuthClient<{\n plugins: [ReturnType<typeof jazzPluginClient>];\n }>\n>;\n\n/**\n * @param props.children - The children to render.\n * @param props.betterAuthClient - The BetterAuth client with the Jazz plugin.\n *\n * @example\n * ```ts\n * const betterAuthClient = createAuthClient({\n * plugins: [\n * jazzPluginClient(),\n * ],\n * });\n *\n * <AuthProvider betterAuthClient={betterAuthClient}>\n * <App />\n * </AuthProvider>\n * ```\n */\nexport function AuthProvider({\n children,\n betterAuthClient,\n}: PropsWithChildren<{\n betterAuthClient: AuthClient;\n}>) {\n const context = useJazzContext();\n const authSecretStorage = useAuthSecretStorage();\n\n if (betterAuthClient.jazz === undefined) {\n throw new Error(\n \"Better Auth client has been initialized without the jazzPluginClient\",\n );\n }\n\n useEffect(() => {\n betterAuthClient.jazz.setJazzContext(context);\n betterAuthClient.jazz.setAuthSecretStorage(authSecretStorage);\n\n // We need to subscribe to the session to let the plugin keep sync Jazz's and BetterAuth's session\n return betterAuthClient.useSession.subscribe(() => {});\n }, [betterAuthClient, context, authSecretStorage]);\n\n return children;\n}\n"],"mappings":";;;AAGA,SAAS,sBAAsB,sBAAsB;AACrD,SAAS,iBAAiB;AA2BnB,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA;AACF,GAEI;AACF,QAAM,UAAU,eAAe;AAC/B,QAAM,oBAAoB,qBAAqB;AAE/C,MAAI,iBAAiB,SAAS,QAAW;AACvC,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,YAAU,MAAM;AACd,qBAAiB,KAAK,eAAe,OAAO;AAC5C,qBAAiB,KAAK,qBAAqB,iBAAiB;AAG5D,WAAO,iBAAiB,WAAW,UAAU,MAAM;AAAA,IAAC,CAAC;AAAA,EACvD,GAAG,CAAC,kBAAkB,SAAS,iBAAiB,CAAC;AAEjD,SAAO;AACT;","names":[]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../src/better-auth/auth/server.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAwB,MAAM,qBAAqB,CAAC;AAM7E,KAAK,UAAU,GAAG,gBAAgB,GAAG;IACnC,MAAM,EAAE;QACN,IAAI,EAAE;YACJ,MAAM,EAAE;gBACN,SAAS,EAAE;oBACT,IAAI,EAAE,QAAQ,CAAC;oBACf,QAAQ,EAAE,KAAK,CAAC;oBAChB,KAAK,EAAE,KAAK,CAAC;iBACd,CAAC;gBACF,oBAAoB,EAAE;oBACpB,IAAI,EAAE,QAAQ,CAAC;oBACf,QAAQ,EAAE,KAAK,CAAC;oBAChB,KAAK,EAAE,KAAK,CAAC;oBACb,QAAQ,EAAE,KAAK,CAAC;iBACjB,CAAC;aACH,CAAC;SACH,CAAC;KACH,CAAC;CACH,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../src/better-auth/auth/server.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAwB,MAAM,qBAAqB,CAAC;AAM7E,KAAK,UAAU,GAAG,gBAAgB,GAAG;IACnC,MAAM,EAAE;QACN,IAAI,EAAE;YACJ,MAAM,EAAE;gBACN,SAAS,EAAE;oBACT,IAAI,EAAE,QAAQ,CAAC;oBACf,QAAQ,EAAE,KAAK,CAAC;oBAChB,KAAK,EAAE,KAAK,CAAC;iBACd,CAAC;gBACF,oBAAoB,EAAE;oBACpB,IAAI,EAAE,QAAQ,CAAC;oBACf,QAAQ,EAAE,KAAK,CAAC;oBAChB,KAAK,EAAE,KAAK,CAAC;oBACb,QAAQ,EAAE,KAAK,CAAC;iBACjB,CAAC;aACH,CAAC;SACH,CAAC;KACH,CAAC;CACH,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU,EAAE,MAAM,UAsQ9B,CAAC"}
|
@@ -45,18 +45,15 @@ var jazzPlugin = () => {
|
|
45
45
|
},
|
46
46
|
verification: {
|
47
47
|
create: {
|
48
|
-
|
49
|
-
if (contextContainsJazzAuth(context)) {
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
return {
|
56
|
-
data: {
|
57
|
-
value: newValue
|
48
|
+
after: async (verification, context) => {
|
49
|
+
if (contextContainsJazzAuth(context) && verification.identifier.startsWith("sign-in-otp-")) {
|
50
|
+
await context.context.internalAdapter.createVerificationValue(
|
51
|
+
{
|
52
|
+
value: JSON.stringify({ jazzAuth: context.jazzAuth }),
|
53
|
+
identifier: `${verification.identifier}-jazz-auth`,
|
54
|
+
expiresAt: verification.expiresAt
|
58
55
|
}
|
59
|
-
|
56
|
+
);
|
60
57
|
}
|
61
58
|
}
|
62
59
|
}
|
@@ -100,6 +97,7 @@ var jazzPlugin = () => {
|
|
100
97
|
})
|
101
98
|
},
|
102
99
|
/**
|
100
|
+
* For: Social / OAuth2 plugin
|
103
101
|
* /callback is the endpoint that BetterAuth uses to authenticate the user coming from a social provider.
|
104
102
|
* 1. Catch the state
|
105
103
|
* 2. Find the verification value
|
@@ -111,17 +109,10 @@ var jazzPlugin = () => {
|
|
111
109
|
},
|
112
110
|
handler: createAuthMiddleware(async (ctx) => {
|
113
111
|
const state = ctx.query?.state || ctx.body?.state;
|
114
|
-
const
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
field: "identifier",
|
119
|
-
operator: "eq",
|
120
|
-
value: state
|
121
|
-
}
|
122
|
-
],
|
123
|
-
select: ["value"]
|
124
|
-
});
|
112
|
+
const identifier = `${state}-jazz-auth`;
|
113
|
+
const data = await ctx.context.internalAdapter.findVerificationValue(
|
114
|
+
identifier
|
115
|
+
);
|
125
116
|
if (!data) {
|
126
117
|
throw new APIError(404, {
|
127
118
|
message: "Verification not found"
|
@@ -141,6 +132,39 @@ var jazzPlugin = () => {
|
|
141
132
|
});
|
142
133
|
}
|
143
134
|
})
|
135
|
+
},
|
136
|
+
/**
|
137
|
+
* For: Email OTP plugin
|
138
|
+
* When the user sends an OTP, we try to find the jazzAuth.
|
139
|
+
* If it isn't a sign-up, we expect to not find a verification value.
|
140
|
+
*/
|
141
|
+
{
|
142
|
+
matcher: (context) => {
|
143
|
+
return context.path.startsWith("/sign-in/email-otp");
|
144
|
+
},
|
145
|
+
handler: createAuthMiddleware(async (ctx) => {
|
146
|
+
const email = ctx.body.email;
|
147
|
+
const identifier = `sign-in-otp-${email}-jazz-auth`;
|
148
|
+
const data = await ctx.context.internalAdapter.findVerificationValue(
|
149
|
+
identifier
|
150
|
+
);
|
151
|
+
if (!data || data.expiresAt < /* @__PURE__ */ new Date()) {
|
152
|
+
return;
|
153
|
+
}
|
154
|
+
const parsed = JSON.parse(data.value);
|
155
|
+
if (parsed && "jazzAuth" in parsed) {
|
156
|
+
return {
|
157
|
+
context: {
|
158
|
+
...ctx,
|
159
|
+
jazzAuth: parsed.jazzAuth
|
160
|
+
}
|
161
|
+
};
|
162
|
+
} else {
|
163
|
+
throw new APIError(500, {
|
164
|
+
message: "JazzAuth not found in verification value"
|
165
|
+
});
|
166
|
+
}
|
167
|
+
})
|
144
168
|
}
|
145
169
|
],
|
146
170
|
after: [
|
@@ -166,6 +190,33 @@ var jazzPlugin = () => {
|
|
166
190
|
jazzAuth
|
167
191
|
});
|
168
192
|
})
|
193
|
+
},
|
194
|
+
/**
|
195
|
+
* For: Social / OAuth2 plugin
|
196
|
+
* When the user sign-in via social, we create a verification value with the jazzAuth.
|
197
|
+
*/
|
198
|
+
{
|
199
|
+
matcher: (context) => {
|
200
|
+
return context.path.startsWith("/sign-in/social");
|
201
|
+
},
|
202
|
+
handler: createAuthMiddleware(async (ctx) => {
|
203
|
+
if (!contextContainsJazzAuth(ctx)) {
|
204
|
+
throw new APIError(500, {
|
205
|
+
message: "JazzAuth not found in context"
|
206
|
+
});
|
207
|
+
}
|
208
|
+
const returned = ctx.context.returned;
|
209
|
+
const url = new URL(returned.url);
|
210
|
+
const state = url.searchParams.get("state");
|
211
|
+
const value = JSON.stringify({ jazzAuth: ctx.jazzAuth });
|
212
|
+
const expiresAt = /* @__PURE__ */ new Date();
|
213
|
+
expiresAt.setMinutes(expiresAt.getMinutes() + 10);
|
214
|
+
await ctx.context.internalAdapter.createVerificationValue({
|
215
|
+
value,
|
216
|
+
identifier: `${state}-jazz-auth`,
|
217
|
+
expiresAt
|
218
|
+
});
|
219
|
+
})
|
169
220
|
}
|
170
221
|
]
|
171
222
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/better-auth/auth/server.ts"],"sourcesContent":["import { AuthContext, MiddlewareContext, MiddlewareOptions } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport { symmetricDecrypt, symmetricEncrypt } from \"better-auth/crypto\";\nimport { BetterAuthPlugin, createAuthMiddleware } from \"better-auth/plugins\";\nimport type { Account, AuthCredentials, ID } from \"jazz-tools\";\n\n// Define a type to have user fields mapped in the better-auth instance\n// It should be automatic, but it needs an hard reference to BetterAuthPlugin type\n// in order to be exported as library.\ntype JazzPlugin = BetterAuthPlugin & {\n schema: {\n user: {\n fields: {\n accountID: {\n type: \"string\";\n required: false;\n input: false;\n };\n encryptedCredentials: {\n type: \"string\";\n required: false;\n input: false;\n returned: false;\n };\n };\n };\n };\n};\n\n/**\n * @returns The BetterAuth server plugin.\n *\n * @example\n * ```ts\n * const auth = betterAuth({\n * plugins: [jazzPlugin()],\n * // ... other BetterAuth options\n * });\n * ```\n */\nexport const jazzPlugin: () => JazzPlugin = () => {\n return {\n id: \"jazz-plugin\",\n schema: {\n user: {\n fields: {\n accountID: {\n type: \"string\",\n required: false,\n input: false,\n },\n encryptedCredentials: {\n type: \"string\",\n required: false,\n input: false,\n returned: false,\n },\n },\n },\n },\n\n init() {\n return {\n options: {\n databaseHooks: {\n user: {\n create: {\n before: async (user, context) => {\n // If the user is created without a jazzAuth, it will throw an error.\n if (!contextContainsJazzAuth(context)) {\n throw new APIError(422, {\n message: \"JazzAuth is required on user creation\",\n });\n }\n // Decorate the user with the jazz's credentials.\n return {\n data: {\n accountID: context.jazzAuth.accountID,\n encryptedCredentials:\n context.jazzAuth.encryptedCredentials,\n },\n };\n },\n },\n },\n verification: {\n create: {\n before: async (verification, context) => {\n // If a jazzAuth is provided, save it for later usage.\n if (contextContainsJazzAuth(context)) {\n const parsed = JSON.parse(verification.value);\n const newValue = JSON.stringify({\n ...parsed,\n jazzAuth: context.jazzAuth,\n });\n\n return {\n data: {\n value: newValue,\n },\n };\n }\n },\n },\n },\n },\n },\n };\n },\n\n hooks: {\n before: [\n /**\n * If the client sends a x-jazz-auth header,\n * we encrypt the credentials and inject them into the context.\n */\n {\n matcher: (context) => {\n return !!context.headers?.get(\"x-jazz-auth\");\n },\n handler: createAuthMiddleware(async (ctx) => {\n const jazzAuth = JSON.parse(ctx.headers?.get(\"x-jazz-auth\")!);\n\n const credentials: AuthCredentials = {\n accountID: jazzAuth.accountID as ID<Account>,\n secretSeed: jazzAuth.secretSeed,\n accountSecret: jazzAuth.accountSecret as any,\n // If the provider remains 'anonymous', Jazz will not consider us authenticated later.\n provider: \"better-auth\",\n };\n\n const encryptedCredentials = await symmetricEncrypt({\n key: ctx.context.secret,\n data: JSON.stringify(credentials),\n });\n\n return {\n context: {\n ...ctx,\n jazzAuth: {\n accountID: jazzAuth.accountID,\n encryptedCredentials: encryptedCredentials,\n },\n },\n };\n }),\n },\n\n /**\n * /callback is the endpoint that BetterAuth uses to authenticate the user coming from a social provider.\n * 1. Catch the state\n * 2. Find the verification value\n * 3. If the verification value contains a jazzAuth, inject into the context to have it in case of registration.\n */\n {\n matcher: (context) => {\n return (\n context.path.startsWith(\"/callback\") ||\n context.path.startsWith(\"/oauth2/callback\")\n );\n },\n handler: createAuthMiddleware(async (ctx) => {\n const state = ctx.query?.state || ctx.body?.state;\n\n const data = await ctx.context.adapter.findOne<{ value: string }>({\n model: ctx.context.tables.verification!.modelName,\n where: [\n {\n field: \"identifier\",\n operator: \"eq\",\n value: state,\n },\n ],\n select: [\"value\"],\n });\n\n // if not found, the social plugin will throw later anyway\n if (!data) {\n throw new APIError(404, {\n message: \"Verification not found\",\n });\n }\n\n const parsed = JSON.parse(data.value);\n\n if (parsed && \"jazzAuth\" in parsed) {\n return {\n context: {\n ...ctx,\n jazzAuth: parsed.jazzAuth,\n },\n };\n } else {\n throw new APIError(404, {\n message: \"JazzAuth not found in verification value\",\n });\n }\n }),\n },\n ],\n after: [\n /**\n * This middleware is used to extract the jazzAuth from the user and return it in the response.\n * It is used in the following endpoints that return the user:\n * - /sign-up/email\n * - /sign-in/email\n * - /get-session\n */\n {\n matcher: (context) => {\n return (\n context.path.startsWith(\"/sign-up\") ||\n context.path.startsWith(\"/sign-in\") ||\n context.path.startsWith(\"/get-session\")\n );\n },\n handler: createAuthMiddleware({}, async (ctx) => {\n const returned = ctx.context.returned as any;\n if (!returned?.user?.id) {\n return;\n }\n const jazzAuth = await extractJazzAuth(returned.user.id, ctx);\n\n return ctx.json({\n ...returned,\n jazzAuth: jazzAuth,\n });\n }),\n },\n ],\n },\n } satisfies JazzPlugin;\n};\n\nfunction contextContainsJazzAuth(ctx: unknown): ctx is {\n jazzAuth: {\n accountID: string;\n encryptedCredentials: string;\n };\n} {\n return !!ctx && typeof ctx === \"object\" && \"jazzAuth\" in ctx;\n}\n\nasync function extractJazzAuth(\n userId: string,\n ctx: MiddlewareContext<\n MiddlewareOptions,\n AuthContext & {\n returned?: unknown;\n responseHeaders?: Headers;\n }\n >,\n) {\n const user = await ctx.context.adapter.findOne<{\n accountID: string;\n encryptedCredentials: string;\n }>({\n model: ctx.context.tables.user!.modelName,\n where: [\n {\n field: \"id\",\n operator: \"eq\",\n value: userId,\n },\n ],\n select: [\"accountID\", \"encryptedCredentials\"],\n });\n\n if (!user) {\n return;\n }\n\n const jazzAuth = JSON.parse(\n await symmetricDecrypt({\n key: ctx.context.secret,\n data: user.encryptedCredentials,\n }),\n );\n\n return jazzAuth;\n}\n"],"mappings":";AACA,SAAS,gBAAgB;AACzB,SAAS,kBAAkB,wBAAwB;AACnD,SAA2B,4BAA4B;AAqChD,IAAM,aAA+B,MAAM;AAChD,S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MAAM,iBAAiB;AAAA,MACrB,KAAK,IAAI,QAAQ;AAAA,MACjB,MAAM,KAAK;AAAA,IACb,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
1
|
+
{"version":3,"sources":["../../../src/better-auth/auth/server.ts"],"sourcesContent":["import { AuthContext, MiddlewareContext, MiddlewareOptions } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport { symmetricDecrypt, symmetricEncrypt } from \"better-auth/crypto\";\nimport { BetterAuthPlugin, createAuthMiddleware } from \"better-auth/plugins\";\nimport type { Account, AuthCredentials, ID } from \"jazz-tools\";\n\n// Define a type to have user fields mapped in the better-auth instance\n// It should be automatic, but it needs an hard reference to BetterAuthPlugin type\n// in order to be exported as library.\ntype JazzPlugin = BetterAuthPlugin & {\n schema: {\n user: {\n fields: {\n accountID: {\n type: \"string\";\n required: false;\n input: false;\n };\n encryptedCredentials: {\n type: \"string\";\n required: false;\n input: false;\n returned: false;\n };\n };\n };\n };\n};\n\n/**\n * @returns The BetterAuth server plugin.\n *\n * @example\n * ```ts\n * const auth = betterAuth({\n * plugins: [jazzPlugin()],\n * // ... other BetterAuth options\n * });\n * ```\n */\nexport const jazzPlugin: () => JazzPlugin = () => {\n return {\n id: \"jazz-plugin\",\n schema: {\n user: {\n fields: {\n accountID: {\n type: \"string\",\n required: false,\n input: false,\n },\n encryptedCredentials: {\n type: \"string\",\n required: false,\n input: false,\n returned: false,\n },\n },\n },\n },\n\n init() {\n return {\n options: {\n databaseHooks: {\n user: {\n create: {\n before: async (user, context) => {\n // If the user is created without a jazzAuth, it will throw an error.\n if (!contextContainsJazzAuth(context)) {\n throw new APIError(422, {\n message: \"JazzAuth is required on user creation\",\n });\n }\n // Decorate the user with the jazz's credentials.\n return {\n data: {\n accountID: context.jazzAuth.accountID,\n encryptedCredentials:\n context.jazzAuth.encryptedCredentials,\n },\n };\n },\n },\n },\n verification: {\n create: {\n after: async (verification, context) => {\n /**\n * For: Email OTP plugin\n * After a verification is created, if it is from the EmailOTP plugin,\n * create a new verification value with the jazzAuth with the same expiration.\n */\n if (\n contextContainsJazzAuth(context) &&\n verification.identifier.startsWith(\"sign-in-otp-\")\n ) {\n await context.context.internalAdapter.createVerificationValue(\n {\n value: JSON.stringify({ jazzAuth: context.jazzAuth }),\n identifier: `${verification.identifier}-jazz-auth`,\n expiresAt: verification.expiresAt,\n },\n );\n }\n },\n },\n },\n },\n },\n };\n },\n\n hooks: {\n before: [\n /**\n * If the client sends a x-jazz-auth header,\n * we encrypt the credentials and inject them into the context.\n */\n {\n matcher: (context) => {\n return !!context.headers?.get(\"x-jazz-auth\");\n },\n handler: createAuthMiddleware(async (ctx) => {\n const jazzAuth = JSON.parse(ctx.headers?.get(\"x-jazz-auth\")!);\n\n const credentials: AuthCredentials = {\n accountID: jazzAuth.accountID as ID<Account>,\n secretSeed: jazzAuth.secretSeed,\n accountSecret: jazzAuth.accountSecret as any,\n // If the provider remains 'anonymous', Jazz will not consider us authenticated later.\n provider: \"better-auth\",\n };\n\n const encryptedCredentials = await symmetricEncrypt({\n key: ctx.context.secret,\n data: JSON.stringify(credentials),\n });\n\n return {\n context: {\n ...ctx,\n jazzAuth: {\n accountID: jazzAuth.accountID,\n encryptedCredentials: encryptedCredentials,\n },\n },\n };\n }),\n },\n\n /**\n * For: Social / OAuth2 plugin\n * /callback is the endpoint that BetterAuth uses to authenticate the user coming from a social provider.\n * 1. Catch the state\n * 2. Find the verification value\n * 3. If the verification value contains a jazzAuth, inject into the context to have it in case of registration.\n */\n {\n matcher: (context) => {\n return (\n context.path.startsWith(\"/callback\") ||\n context.path.startsWith(\"/oauth2/callback\")\n );\n },\n handler: createAuthMiddleware(async (ctx) => {\n const state = ctx.query?.state || ctx.body?.state;\n\n const identifier = `${state}-jazz-auth`;\n\n const data =\n await ctx.context.internalAdapter.findVerificationValue(\n identifier,\n );\n\n // if not found, the social plugin will throw later anyway\n if (!data) {\n throw new APIError(404, {\n message: \"Verification not found\",\n });\n }\n\n const parsed = JSON.parse(data.value);\n\n if (parsed && \"jazzAuth\" in parsed) {\n return {\n context: {\n ...ctx,\n jazzAuth: parsed.jazzAuth,\n },\n };\n } else {\n throw new APIError(404, {\n message: \"JazzAuth not found in verification value\",\n });\n }\n }),\n },\n /**\n * For: Email OTP plugin\n * When the user sends an OTP, we try to find the jazzAuth.\n * If it isn't a sign-up, we expect to not find a verification value.\n */\n {\n matcher: (context) => {\n return context.path.startsWith(\"/sign-in/email-otp\");\n },\n handler: createAuthMiddleware(async (ctx) => {\n const email = ctx.body.email;\n const identifier = `sign-in-otp-${email}-jazz-auth`;\n\n const data =\n await ctx.context.internalAdapter.findVerificationValue(\n identifier,\n );\n\n // if not found, it isn't a sign-up\n if (!data || data.expiresAt < new Date()) {\n return;\n }\n\n const parsed = JSON.parse(data.value);\n\n if (parsed && \"jazzAuth\" in parsed) {\n return {\n context: {\n ...ctx,\n jazzAuth: parsed.jazzAuth,\n },\n };\n } else {\n throw new APIError(500, {\n message: \"JazzAuth not found in verification value\",\n });\n }\n }),\n },\n ],\n after: [\n /**\n * This middleware is used to extract the jazzAuth from the user and return it in the response.\n * It is used in the following endpoints that return the user:\n * - /sign-up/email\n * - /sign-in/email\n * - /get-session\n */\n {\n matcher: (context) => {\n return (\n context.path.startsWith(\"/sign-up\") ||\n context.path.startsWith(\"/sign-in\") ||\n context.path.startsWith(\"/get-session\")\n );\n },\n handler: createAuthMiddleware({}, async (ctx) => {\n const returned = ctx.context.returned as any;\n if (!returned?.user?.id) {\n return;\n }\n const jazzAuth = await extractJazzAuth(returned.user.id, ctx);\n\n return ctx.json({\n ...returned,\n jazzAuth: jazzAuth,\n });\n }),\n },\n\n /**\n * For: Social / OAuth2 plugin\n * When the user sign-in via social, we create a verification value with the jazzAuth.\n */\n {\n matcher: (context) => {\n return context.path.startsWith(\"/sign-in/social\");\n },\n handler: createAuthMiddleware(async (ctx) => {\n if (!contextContainsJazzAuth(ctx)) {\n throw new APIError(500, {\n message: \"JazzAuth not found in context\",\n });\n }\n\n const returned = ctx.context.returned as { url: string };\n\n const url = new URL(returned.url);\n const state = url.searchParams.get(\"state\");\n\n const value = JSON.stringify({ jazzAuth: ctx.jazzAuth });\n const expiresAt = new Date();\n expiresAt.setMinutes(expiresAt.getMinutes() + 10);\n\n await ctx.context.internalAdapter.createVerificationValue({\n value,\n identifier: `${state}-jazz-auth`,\n expiresAt,\n });\n }),\n },\n ],\n },\n } satisfies JazzPlugin;\n};\n\nfunction contextContainsJazzAuth(ctx: unknown): ctx is {\n jazzAuth: {\n accountID: string;\n encryptedCredentials: string;\n };\n} {\n return !!ctx && typeof ctx === \"object\" && \"jazzAuth\" in ctx;\n}\n\nasync function extractJazzAuth(\n userId: string,\n ctx: MiddlewareContext<\n MiddlewareOptions,\n AuthContext & {\n returned?: unknown;\n responseHeaders?: Headers;\n }\n >,\n) {\n const user = await ctx.context.adapter.findOne<{\n accountID: string;\n encryptedCredentials: string;\n }>({\n model: ctx.context.tables.user!.modelName,\n where: [\n {\n field: \"id\",\n operator: \"eq\",\n value: userId,\n },\n ],\n select: [\"accountID\", \"encryptedCredentials\"],\n });\n\n if (!user) {\n return;\n }\n\n const jazzAuth = JSON.parse(\n await symmetricDecrypt({\n key: ctx.context.secret,\n data: user.encryptedCredentials,\n }),\n );\n\n return jazzAuth;\n}\n"],"mappings":";AACA,SAAS,gBAAgB;AACzB,SAAS,kBAAkB,wBAAwB;AACnD,SAA2B,4BAA4B;AAqChD,IAAM,aAA+B,MAAM;AAChD,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,QAAQ;AAAA,MACN,MAAM;AAAA,QACJ,QAAQ;AAAA,UACN,WAAW;AAAA,YACT,MAAM;AAAA,YACN,UAAU;AAAA,YACV,OAAO;AAAA,UACT;AAAA,UACA,sBAAsB;AAAA,YACpB,MAAM;AAAA,YACN,UAAU;AAAA,YACV,OAAO;AAAA,YACP,UAAU;AAAA,UACZ;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IAEA,OAAO;AACL,aAA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,CAAC;AAAA,YACH;AAEA,kBAAM,WAAW,IAAI,QAAQ;AAE7B,kBAAM,MAAM,IAAI,IAAI,SAAS,GAAG;AAChC,kBAAM,QAAQ,IAAI,aAAa,IAAI,OAAO;AAE1C,kBAAM,QAAQ,KAAK,UAAU,EAAE,UAAU,IAAI,SAAS,CAAC;AACvD,kBAAM,YAAY,oBAAI,KAAK;AAC3B,sBAAU,WAAW,UAAU,WAAW,IAAI,EAAE;AAEhD,kBAAM,IAAI,QAAQ,gBAAgB,wBAAwB;AAAA,cACxD;AAAA,cACA,YAAY,GAAG,KAAK;AAAA,cACpB;AAAA,YACF,CAAC;AAAA,UACH,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,wBAAwB,KAK/B;AACA,SAAO,CAAC,CAAC,OAAO,OAAO,QAAQ,YAAY,cAAc;AAC3D;AAEA,eAAe,gBACb,QACA,KAOA;AACA,QAAM,OAAO,MAAM,IAAI,QAAQ,QAAQ,QAGpC;AAAA,IACD,OAAO,IAAI,QAAQ,OAAO,KAAM;AAAA,IAChC,OAAO;AAAA,MACL;AAAA,QACE,OAAO;AAAA,QACP,UAAU;AAAA,QACV,OAAO;AAAA,MACT;AAAA,IACF;AAAA,IACA,QAAQ,CAAC,aAAa,sBAAsB;AAAA,EAC9C,CAAC;AAED,MAAI,CAAC,MAAM;AACT;AAAA,EACF;AAEA,QAAM,WAAW,KAAK;AAAA,IACpB,MAAM,iBAAiB;AAAA,MACrB,KAAK,IAAI,QAAQ;AAAA,MACjB,MAAM,KAAK;AAAA,IACb,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"react.test.d.ts","sourceRoot":"","sources":["../../../../src/better-auth/auth/tests/react.test.tsx"],"names":[],"mappings":""}
|
@@ -2677,7 +2677,9 @@ var Profile = class extends CoMap {
|
|
2677
2677
|
};
|
2678
2678
|
|
2679
2679
|
// src/tools/coValues/inbox.ts
|
2680
|
-
import {
|
2680
|
+
import {
|
2681
|
+
RawAccount as RawAccount2
|
2682
|
+
} from "cojson";
|
2681
2683
|
|
2682
2684
|
// src/tools/lib/id.ts
|
2683
2685
|
function isCoValueId(id) {
|
@@ -2704,6 +2706,20 @@ function createInboxRoot(account) {
|
|
2704
2706
|
inviteLink
|
2705
2707
|
};
|
2706
2708
|
}
|
2709
|
+
var IncrementalFeed = class {
|
2710
|
+
constructor(feed) {
|
2711
|
+
this.feed = feed;
|
2712
|
+
this.sessions = {};
|
2713
|
+
}
|
2714
|
+
getNewItems() {
|
2715
|
+
const items = this.feed.getValidTransactions({
|
2716
|
+
ignorePrivateTransactions: false,
|
2717
|
+
from: this.sessions
|
2718
|
+
});
|
2719
|
+
this.sessions = this.feed.knownState().sessions;
|
2720
|
+
return items;
|
2721
|
+
}
|
2722
|
+
};
|
2707
2723
|
async function createInboxMessage(payload, inboxOwner) {
|
2708
2724
|
const group = payload.$jazz.raw.group;
|
2709
2725
|
if (group instanceof RawAccount2) {
|
@@ -2720,108 +2736,133 @@ async function createInboxMessage(payload, inboxOwner) {
|
|
2720
2736
|
await message.core.waitForSync();
|
2721
2737
|
return message;
|
2722
2738
|
}
|
2739
|
+
var MessageQueue = class {
|
2740
|
+
constructor(concurrencyLimit = 10, processMessage, handleError) {
|
2741
|
+
this.processMessage = processMessage;
|
2742
|
+
this.handleError = handleError;
|
2743
|
+
this.queue = [];
|
2744
|
+
this.processing = /* @__PURE__ */ new Set();
|
2745
|
+
this.activeCount = 0;
|
2746
|
+
this.concurrencyLimit = concurrencyLimit;
|
2747
|
+
}
|
2748
|
+
enqueue(txKey, messageId) {
|
2749
|
+
this.queue.push({ txKey, messageId });
|
2750
|
+
this.processNext();
|
2751
|
+
}
|
2752
|
+
async processNext() {
|
2753
|
+
if (this.activeCount >= this.concurrencyLimit || this.queue.length === 0) {
|
2754
|
+
return;
|
2755
|
+
}
|
2756
|
+
const { txKey, messageId } = this.queue.shift();
|
2757
|
+
if (this.processing.has(txKey)) {
|
2758
|
+
this.processNext();
|
2759
|
+
return;
|
2760
|
+
}
|
2761
|
+
this.processing.add(txKey);
|
2762
|
+
this.activeCount++;
|
2763
|
+
try {
|
2764
|
+
await this.processMessage(txKey, messageId);
|
2765
|
+
} catch (error) {
|
2766
|
+
this.handleError(txKey, messageId, error);
|
2767
|
+
} finally {
|
2768
|
+
this.processing.delete(txKey);
|
2769
|
+
this.activeCount--;
|
2770
|
+
this.processNext();
|
2771
|
+
}
|
2772
|
+
}
|
2773
|
+
};
|
2723
2774
|
var Inbox = class _Inbox {
|
2724
2775
|
constructor(account, root, messages, processed, failed) {
|
2725
|
-
this.processing = /* @__PURE__ */ new Set();
|
2726
2776
|
this.account = account;
|
2727
2777
|
this.root = root;
|
2728
2778
|
this.messages = messages;
|
2729
2779
|
this.processed = processed;
|
2730
2780
|
this.failed = failed;
|
2731
2781
|
}
|
2732
|
-
subscribe(Schema4, callback, options
|
2782
|
+
subscribe(Schema4, callback, options) {
|
2733
2783
|
const processed = /* @__PURE__ */ new Set();
|
2734
|
-
const failed = /* @__PURE__ */ new Map();
|
2735
2784
|
const node = this.account.$jazz.localNode;
|
2736
|
-
|
2737
|
-
|
2738
|
-
|
2739
|
-
|
2740
|
-
|
2785
|
+
const concurrencyLimit = options?.concurrencyLimit ?? 10;
|
2786
|
+
const processedFeed = new IncrementalFeed(this.processed.core);
|
2787
|
+
this.processed.subscribe(() => {
|
2788
|
+
for (const { changes } of processedFeed.getNewItems()) {
|
2789
|
+
processed.add(changes[0]);
|
2741
2790
|
}
|
2742
2791
|
});
|
2743
2792
|
const { account } = this;
|
2744
|
-
const
|
2745
|
-
|
2746
|
-
|
2747
|
-
|
2748
|
-
|
2793
|
+
const messagesFeed = new IncrementalFeed(this.messages.core);
|
2794
|
+
const processMessage = async (txKey, messageId) => {
|
2795
|
+
const message = await node.load(messageId);
|
2796
|
+
if (message === "unavailable") {
|
2797
|
+
throw new Error(`Inbox: message ${messageId} is unavailable`);
|
2798
|
+
}
|
2799
|
+
const value = await loadCoValue(
|
2800
|
+
coValueClassFromCoValueClassOrSchema(Schema4),
|
2801
|
+
message.get("payload"),
|
2802
|
+
{
|
2803
|
+
loadAs: account
|
2804
|
+
}
|
2805
|
+
);
|
2806
|
+
if (!value) {
|
2807
|
+
throw new Error(
|
2808
|
+
`Inbox: Unable to load the payload of message ${messageId}`
|
2809
|
+
);
|
2810
|
+
}
|
2811
|
+
const accountID = getAccountIDfromSessionID(
|
2812
|
+
txKey.split("/")[0]
|
2813
|
+
);
|
2814
|
+
if (!accountID) {
|
2815
|
+
throw new Error(`Inbox: Unknown account for message ${messageId}`);
|
2816
|
+
}
|
2817
|
+
const result = await callback(value, accountID);
|
2818
|
+
const inboxMessage = node.expectCoValueLoaded(messageId).getCurrentContent();
|
2819
|
+
if (result) {
|
2820
|
+
inboxMessage.set("result", result.$jazz.id);
|
2821
|
+
}
|
2822
|
+
inboxMessage.set("processed", true);
|
2823
|
+
this.processed.push(txKey);
|
2824
|
+
};
|
2825
|
+
const handleError = (txKey, messageId, error) => {
|
2826
|
+
console.error(error);
|
2827
|
+
const stringifiedError = String(error);
|
2828
|
+
this.processed.push(txKey);
|
2829
|
+
this.failed.push({ errors: [stringifiedError], value: messageId });
|
2830
|
+
try {
|
2831
|
+
const inboxMessage = node.expectCoValueLoaded(messageId).getCurrentContent();
|
2832
|
+
inboxMessage.set("error", stringifiedError);
|
2833
|
+
inboxMessage.set("processed", true);
|
2834
|
+
} catch (error2) {
|
2835
|
+
}
|
2749
2836
|
};
|
2750
|
-
const
|
2751
|
-
|
2752
|
-
|
2753
|
-
|
2837
|
+
const messageQueue = new MessageQueue(
|
2838
|
+
concurrencyLimit,
|
2839
|
+
processMessage,
|
2840
|
+
handleError
|
2841
|
+
);
|
2842
|
+
const handleNewMessages = () => {
|
2843
|
+
for (const tx of messagesFeed.getNewItems()) {
|
2844
|
+
const accountID = getAccountIDfromSessionID(tx.txID.sessionID);
|
2754
2845
|
if (!accountID) {
|
2755
|
-
console.warn(
|
2846
|
+
console.warn(
|
2847
|
+
"Received message from unknown account",
|
2848
|
+
tx.txID.sessionID
|
2849
|
+
);
|
2756
2850
|
continue;
|
2757
2851
|
}
|
2758
|
-
|
2759
|
-
|
2760
|
-
|
2761
|
-
|
2762
|
-
|
2763
|
-
|
2764
|
-
|
2765
|
-
return Promise.reject(
|
2766
|
-
new Error("Unable to load inbox message " + id)
|
2767
|
-
);
|
2768
|
-
}
|
2769
|
-
return loadCoValue(
|
2770
|
-
coValueClassFromCoValueClassOrSchema(Schema4),
|
2771
|
-
message.get("payload"),
|
2772
|
-
{
|
2773
|
-
loadAs: account
|
2774
|
-
}
|
2775
|
-
);
|
2776
|
-
}).then((value) => {
|
2777
|
-
if (!value) {
|
2778
|
-
return Promise.reject(
|
2779
|
-
new Error("Unable to load inbox message " + id)
|
2780
|
-
);
|
2781
|
-
}
|
2782
|
-
return callback(value, accountID);
|
2783
|
-
}).then((result) => {
|
2784
|
-
const inboxMessage = node.expectCoValueLoaded(item.value).getCurrentContent();
|
2785
|
-
if (result) {
|
2786
|
-
inboxMessage.set("result", result.$jazz.id);
|
2787
|
-
}
|
2788
|
-
inboxMessage.set("processed", true);
|
2789
|
-
this.processed.push(txKey);
|
2790
|
-
this.processing.delete(txKey);
|
2791
|
-
}).catch((error) => {
|
2792
|
-
console.error("Error processing inbox message", error);
|
2793
|
-
this.processing.delete(txKey);
|
2794
|
-
const errors = failed.get(txKey) ?? [];
|
2795
|
-
const stringifiedError = String(error);
|
2796
|
-
errors.push(stringifiedError);
|
2797
|
-
let inboxMessage;
|
2798
|
-
try {
|
2799
|
-
inboxMessage = node.expectCoValueLoaded(item.value).getCurrentContent();
|
2800
|
-
inboxMessage.set("error", stringifiedError);
|
2801
|
-
} catch (error2) {
|
2802
|
-
}
|
2803
|
-
if (errors.length > retries) {
|
2804
|
-
inboxMessage?.set("processed", true);
|
2805
|
-
this.processed.push(txKey);
|
2806
|
-
this.failed.push({ errors, value: item.value });
|
2807
|
-
} else {
|
2808
|
-
failed.set(txKey, errors);
|
2809
|
-
if (!failTimer) {
|
2810
|
-
failTimer = setTimeout(
|
2811
|
-
() => handleNewMessages(stream),
|
2812
|
-
100
|
2813
|
-
);
|
2814
|
-
}
|
2815
|
-
}
|
2816
|
-
});
|
2817
|
-
}
|
2852
|
+
const id = tx.changes[0];
|
2853
|
+
if (!isCoValueId(id)) {
|
2854
|
+
continue;
|
2855
|
+
}
|
2856
|
+
const txKey = `${tx.txID.sessionID}/${tx.txID.txIndex}`;
|
2857
|
+
if (processed.has(txKey)) {
|
2858
|
+
continue;
|
2818
2859
|
}
|
2860
|
+
messageQueue.enqueue(txKey, id);
|
2819
2861
|
}
|
2820
2862
|
};
|
2821
2863
|
const unsubscribe = this.messages.subscribe(handleNewMessages);
|
2822
2864
|
return () => {
|
2823
2865
|
unsubscribe();
|
2824
|
-
clearFailTimer();
|
2825
2866
|
};
|
2826
2867
|
}
|
2827
2868
|
static async load(account) {
|
@@ -2845,6 +2886,7 @@ var Inbox = class _Inbox {
|
|
2845
2886
|
if (messages === "unavailable" || processed === "unavailable" || failed === "unavailable") {
|
2846
2887
|
throw new Error("Inbox not found");
|
2847
2888
|
}
|
2889
|
+
await processed.core.waitForFullStreaming();
|
2848
2890
|
return new _Inbox(account, root, messages, processed, failed);
|
2849
2891
|
}
|
2850
2892
|
};
|
@@ -5816,4 +5858,4 @@ export {
|
|
5816
5858
|
JazzContextManager
|
5817
5859
|
};
|
5818
5860
|
/* istanbul ignore file -- @preserve */
|
5819
|
-
//# sourceMappingURL=chunk-
|
5861
|
+
//# sourceMappingURL=chunk-45VKEOXG.js.map
|