@convex-dev/better-auth 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +44 -0
- package/dist/commonjs/client/adapter.d.ts +4 -0
- package/dist/commonjs/client/adapter.d.ts.map +1 -0
- package/dist/commonjs/client/adapter.js +189 -0
- package/dist/commonjs/client/adapter.js.map +1 -0
- package/dist/commonjs/client/cors.d.ts +72 -0
- package/dist/commonjs/client/cors.d.ts.map +1 -0
- package/dist/commonjs/client/cors.js +281 -0
- package/dist/commonjs/client/cors.js.map +1 -0
- package/dist/commonjs/client/index.d.ts +302 -0
- package/dist/commonjs/client/index.d.ts.map +1 -0
- package/dist/commonjs/client/index.js +232 -0
- package/dist/commonjs/client/index.js.map +1 -0
- package/dist/commonjs/client/plugins/index.d.ts +3 -0
- package/dist/commonjs/client/plugins/index.d.ts.map +1 -0
- package/dist/commonjs/client/plugins/index.js +3 -0
- package/dist/commonjs/client/plugins/index.js.map +1 -0
- package/dist/commonjs/component/_generated/api.d.ts +12 -0
- package/dist/commonjs/component/_generated/api.d.ts.map +1 -0
- package/dist/commonjs/component/_generated/api.js +22 -0
- package/dist/commonjs/component/_generated/api.js.map +1 -0
- package/dist/commonjs/component/_generated/server.d.ts +64 -0
- package/dist/commonjs/component/_generated/server.d.ts.map +1 -0
- package/dist/commonjs/component/_generated/server.js +74 -0
- package/dist/commonjs/component/_generated/server.js.map +1 -0
- package/dist/commonjs/component/convex.config.d.ts +3 -0
- package/dist/commonjs/component/convex.config.d.ts.map +1 -0
- package/dist/commonjs/component/convex.config.js +4 -0
- package/dist/commonjs/component/convex.config.js.map +1 -0
- package/dist/commonjs/component/lib.d.ts +584 -0
- package/dist/commonjs/component/lib.d.ts.map +1 -0
- package/dist/commonjs/component/lib.js +323 -0
- package/dist/commonjs/component/lib.js.map +1 -0
- package/dist/commonjs/component/schema.d.ts +116 -0
- package/dist/commonjs/component/schema.d.ts.map +1 -0
- package/dist/commonjs/component/schema.js +68 -0
- package/dist/commonjs/component/schema.js.map +1 -0
- package/dist/commonjs/component/util.d.ts +394 -0
- package/dist/commonjs/component/util.d.ts.map +1 -0
- package/dist/commonjs/component/util.js +4 -0
- package/dist/commonjs/component/util.js.map +1 -0
- package/dist/commonjs/nextjs/index.d.ts +10 -0
- package/dist/commonjs/nextjs/index.d.ts.map +1 -0
- package/dist/commonjs/nextjs/index.js +23 -0
- package/dist/commonjs/nextjs/index.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/plugins/convex/client.d.ts +6 -0
- package/dist/commonjs/plugins/convex/client.d.ts.map +1 -0
- package/dist/commonjs/plugins/convex/client.js +7 -0
- package/dist/commonjs/plugins/convex/client.js.map +1 -0
- package/dist/commonjs/plugins/convex/index.d.ts +280 -0
- package/dist/commonjs/plugins/convex/index.d.ts.map +1 -0
- package/dist/commonjs/plugins/convex/index.js +253 -0
- package/dist/commonjs/plugins/convex/index.js.map +1 -0
- package/dist/commonjs/plugins/cross-domain/client.d.ts +123 -0
- package/dist/commonjs/plugins/cross-domain/client.d.ts.map +1 -0
- package/dist/commonjs/plugins/cross-domain/client.js +164 -0
- package/dist/commonjs/plugins/cross-domain/client.js.map +1 -0
- package/dist/commonjs/plugins/cross-domain/index.d.ts +81 -0
- package/dist/commonjs/plugins/cross-domain/index.d.ts.map +1 -0
- package/dist/commonjs/plugins/cross-domain/index.js +135 -0
- package/dist/commonjs/plugins/cross-domain/index.js.map +1 -0
- package/dist/commonjs/plugins/index.d.ts +3 -0
- package/dist/commonjs/plugins/index.d.ts.map +1 -0
- package/dist/commonjs/plugins/index.js +3 -0
- package/dist/commonjs/plugins/index.js.map +1 -0
- package/dist/commonjs/react/client.d.ts +31 -0
- package/dist/commonjs/react/client.d.ts.map +1 -0
- package/dist/commonjs/react/client.js +102 -0
- package/dist/commonjs/react/client.js.map +1 -0
- package/dist/commonjs/react/index.d.ts +9 -0
- package/dist/commonjs/react/index.d.ts.map +1 -0
- package/dist/commonjs/react/index.js +15 -0
- package/dist/commonjs/react/index.js.map +1 -0
- package/dist/commonjs/react-start/index.d.ts +10 -0
- package/dist/commonjs/react-start/index.d.ts.map +1 -0
- package/dist/commonjs/react-start/index.js +32 -0
- package/dist/commonjs/react-start/index.js.map +1 -0
- package/dist/esm/client/adapter.d.ts +4 -0
- package/dist/esm/client/adapter.d.ts.map +1 -0
- package/dist/esm/client/adapter.js +189 -0
- package/dist/esm/client/adapter.js.map +1 -0
- package/dist/esm/client/cors.d.ts +72 -0
- package/dist/esm/client/cors.d.ts.map +1 -0
- package/dist/esm/client/cors.js +281 -0
- package/dist/esm/client/cors.js.map +1 -0
- package/dist/esm/client/index.d.ts +302 -0
- package/dist/esm/client/index.d.ts.map +1 -0
- package/dist/esm/client/index.js +232 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/client/plugins/index.d.ts +3 -0
- package/dist/esm/client/plugins/index.d.ts.map +1 -0
- package/dist/esm/client/plugins/index.js +3 -0
- package/dist/esm/client/plugins/index.js.map +1 -0
- package/dist/esm/component/_generated/api.d.ts +12 -0
- package/dist/esm/component/_generated/api.d.ts.map +1 -0
- package/dist/esm/component/_generated/api.js +22 -0
- package/dist/esm/component/_generated/api.js.map +1 -0
- package/dist/esm/component/_generated/server.d.ts +64 -0
- package/dist/esm/component/_generated/server.d.ts.map +1 -0
- package/dist/esm/component/_generated/server.js +74 -0
- package/dist/esm/component/_generated/server.js.map +1 -0
- package/dist/esm/component/convex.config.d.ts +3 -0
- package/dist/esm/component/convex.config.d.ts.map +1 -0
- package/dist/esm/component/convex.config.js +4 -0
- package/dist/esm/component/convex.config.js.map +1 -0
- package/dist/esm/component/lib.d.ts +584 -0
- package/dist/esm/component/lib.d.ts.map +1 -0
- package/dist/esm/component/lib.js +323 -0
- package/dist/esm/component/lib.js.map +1 -0
- package/dist/esm/component/schema.d.ts +116 -0
- package/dist/esm/component/schema.d.ts.map +1 -0
- package/dist/esm/component/schema.js +68 -0
- package/dist/esm/component/schema.js.map +1 -0
- package/dist/esm/component/util.d.ts +394 -0
- package/dist/esm/component/util.d.ts.map +1 -0
- package/dist/esm/component/util.js +4 -0
- package/dist/esm/component/util.js.map +1 -0
- package/dist/esm/nextjs/index.d.ts +10 -0
- package/dist/esm/nextjs/index.d.ts.map +1 -0
- package/dist/esm/nextjs/index.js +23 -0
- package/dist/esm/nextjs/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/plugins/convex/client.d.ts +6 -0
- package/dist/esm/plugins/convex/client.d.ts.map +1 -0
- package/dist/esm/plugins/convex/client.js +7 -0
- package/dist/esm/plugins/convex/client.js.map +1 -0
- package/dist/esm/plugins/convex/index.d.ts +280 -0
- package/dist/esm/plugins/convex/index.d.ts.map +1 -0
- package/dist/esm/plugins/convex/index.js +253 -0
- package/dist/esm/plugins/convex/index.js.map +1 -0
- package/dist/esm/plugins/cross-domain/client.d.ts +123 -0
- package/dist/esm/plugins/cross-domain/client.d.ts.map +1 -0
- package/dist/esm/plugins/cross-domain/client.js +164 -0
- package/dist/esm/plugins/cross-domain/client.js.map +1 -0
- package/dist/esm/plugins/cross-domain/index.d.ts +81 -0
- package/dist/esm/plugins/cross-domain/index.d.ts.map +1 -0
- package/dist/esm/plugins/cross-domain/index.js +135 -0
- package/dist/esm/plugins/cross-domain/index.js.map +1 -0
- package/dist/esm/plugins/index.d.ts +3 -0
- package/dist/esm/plugins/index.d.ts.map +1 -0
- package/dist/esm/plugins/index.js +3 -0
- package/dist/esm/plugins/index.js.map +1 -0
- package/dist/esm/react/client.d.ts +31 -0
- package/dist/esm/react/client.d.ts.map +1 -0
- package/dist/esm/react/client.js +102 -0
- package/dist/esm/react/client.js.map +1 -0
- package/dist/esm/react/index.d.ts +9 -0
- package/dist/esm/react/index.d.ts.map +1 -0
- package/dist/esm/react/index.js +15 -0
- package/dist/esm/react/index.js.map +1 -0
- package/dist/esm/react-start/index.d.ts +10 -0
- package/dist/esm/react-start/index.d.ts.map +1 -0
- package/dist/esm/react-start/index.js +32 -0
- package/dist/esm/react-start/index.js.map +1 -0
- package/package.json +161 -0
- package/plugins/package.json +5 -0
- package/react/package.json +5 -0
- package/src/client/adapter.ts +236 -0
- package/src/client/cors.ts +403 -0
- package/src/client/index.ts +381 -0
- package/src/client/plugins/index.ts +2 -0
- package/src/component/_generated/api.d.ts +313 -0
- package/src/component/_generated/api.js +23 -0
- package/src/component/_generated/dataModel.d.ts +60 -0
- package/src/component/_generated/server.d.ts +149 -0
- package/src/component/_generated/server.js +90 -0
- package/src/component/convex.config.ts +5 -0
- package/src/component/lib.ts +391 -0
- package/src/component/schema.ts +74 -0
- package/src/component/util.ts +4 -0
- package/src/nextjs/index.ts +30 -0
- package/src/plugins/convex/client.ts +9 -0
- package/src/plugins/convex/index.ts +296 -0
- package/src/plugins/cross-domain/client.ts +209 -0
- package/src/plugins/cross-domain/index.ts +156 -0
- package/src/plugins/index.ts +2 -0
- package/src/react/client.tsx +184 -0
- package/src/react/index.tsx +38 -0
- package/src/react-start/index.ts +51 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createCookieGetter } from "better-auth/cookies";
|
|
2
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
3
|
+
export const getCookieName = async (createAuth) => {
|
|
4
|
+
const auth = createAuth({});
|
|
5
|
+
const createCookie = createCookieGetter(auth.options);
|
|
6
|
+
const cookie = createCookie("convex_jwt");
|
|
7
|
+
return cookie.name;
|
|
8
|
+
};
|
|
9
|
+
export const fetchSession = async (createAuth, request) => {
|
|
10
|
+
if (!request) {
|
|
11
|
+
throw new Error("No request found");
|
|
12
|
+
}
|
|
13
|
+
const baseURL = new URL(request.url).origin;
|
|
14
|
+
const { data: session } = await betterFetch("/api/auth/get-session", {
|
|
15
|
+
baseURL,
|
|
16
|
+
headers: {
|
|
17
|
+
cookie: request.headers.get("cookie") ?? "",
|
|
18
|
+
origin: baseURL,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
return {
|
|
22
|
+
session,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
export const reactStartHandler = (request, opts) => {
|
|
26
|
+
const convexSiteUrl = opts?.convexSiteUrl ?? process.env.CONVEX_SITE_URL;
|
|
27
|
+
const requestUrl = new URL(request.url);
|
|
28
|
+
const nextUrl = `${convexSiteUrl}${requestUrl.pathname}${requestUrl.search}`;
|
|
29
|
+
request.headers.set("accept-encoding", "application/json");
|
|
30
|
+
return fetch(nextUrl, new Request(request, { redirect: "manual" }));
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react-start/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGlD,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAChC,UAAyE,EACzE,EAAE;IACF,MAAM,IAAI,GAAG,UAAU,CAAC,EAAS,CAAC,CAAC;IACnC,MAAM,YAAY,GAAG,kBAAkB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACtD,MAAM,MAAM,GAAG,YAAY,CAAC,YAAY,CAAC,CAAC;IAC1C,OAAO,MAAM,CAAC,IAAI,CAAC;AACrB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAG/B,UAAa,EACb,OAAiB,EACjB,EAAE;IAGF,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;IACtC,CAAC;IACD,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC;IAC5C,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,WAAW,CACzC,uBAAuB,EACvB;QACE,OAAO;QACP,OAAO,EAAE;YACP,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE;YAC3C,MAAM,EAAE,OAAO;SAChB;KACF,CACF,CAAC;IACF,OAAO;QACL,OAAO;KACR,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,OAAgB,EAChB,IAAiC,EACjC,EAAE;IACF,MAAM,aAAa,GAAG,IAAI,EAAE,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC;IACzE,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACxC,MAAM,OAAO,GAAG,GAAG,aAAa,GAAG,UAAU,CAAC,QAAQ,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC;IAC7E,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,kBAAkB,CAAC,CAAC;IAC3D,OAAO,KAAK,CAAC,OAAO,EAAE,IAAI,OAAO,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC;AACtE,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@convex-dev/better-auth",
|
|
3
|
+
"description": "A Better Auth component for Convex.",
|
|
4
|
+
"repository": "github:get-convex/better-auth",
|
|
5
|
+
"homepage": "https://github.com/get-convex/better-auth#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/get-convex/better-auth/issues"
|
|
8
|
+
},
|
|
9
|
+
"version": "0.6.0",
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"convex",
|
|
13
|
+
"component",
|
|
14
|
+
"auth",
|
|
15
|
+
"authentication",
|
|
16
|
+
"better-auth",
|
|
17
|
+
"convex-auth"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "npm run build:esm && npm run build:cjs",
|
|
22
|
+
"build:esm": "tsc --project ./esm.json && tsc-alias -p ./esm.json && echo '{\\n \"type\": \"module\"\\n}' > dist/esm/package.json",
|
|
23
|
+
"build:cjs": "tsc --project ./commonjs.json && echo '{\\n \"type\": \"commonjs\"\\n}' > dist/commonjs/package.json",
|
|
24
|
+
"typecheck": "tsc --noEmit",
|
|
25
|
+
"prepare": "npm run build",
|
|
26
|
+
"prepack": "node node10stubs.mjs",
|
|
27
|
+
"postpack": "node node10stubs.mjs --cleanup",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:debug": "vitest --inspect-brk --no-file-parallelism",
|
|
30
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=text"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"src",
|
|
35
|
+
"react",
|
|
36
|
+
"plugins"
|
|
37
|
+
],
|
|
38
|
+
"exports": {
|
|
39
|
+
"./package.json": "./package.json",
|
|
40
|
+
".": {
|
|
41
|
+
"import": {
|
|
42
|
+
"@convex-dev/component-source": "./src/client/index.ts",
|
|
43
|
+
"types": "./dist/esm/client/index.d.ts",
|
|
44
|
+
"default": "./dist/esm/client/index.js"
|
|
45
|
+
},
|
|
46
|
+
"require": {
|
|
47
|
+
"@convex-dev/component-source": "./src/client/index.ts",
|
|
48
|
+
"types": "./dist/commonjs/client/index.d.ts",
|
|
49
|
+
"default": "./dist/commonjs/client/index.js"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"./client/plugins": {
|
|
53
|
+
"import": {
|
|
54
|
+
"@convex-dev/component-source": "./src/client/plugins/index.ts",
|
|
55
|
+
"types": "./dist/esm/client/plugins/index.d.ts",
|
|
56
|
+
"default": "./dist/esm/client/plugins/index.js"
|
|
57
|
+
},
|
|
58
|
+
"require": {
|
|
59
|
+
"@convex-dev/component-source": "./src/plugins/clientPlugins.ts",
|
|
60
|
+
"types": "./dist/commonjs/plugins/clientPlugins.d.ts",
|
|
61
|
+
"default": "./dist/commonjs/plugins/clientPlugins.js"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"./nextjs": {
|
|
65
|
+
"import": {
|
|
66
|
+
"@convex-dev/component-source": "./src/nextjs/index.ts",
|
|
67
|
+
"types": "./dist/esm/nextjs/index.d.ts",
|
|
68
|
+
"default": "./dist/esm/nextjs/index.js"
|
|
69
|
+
},
|
|
70
|
+
"require": {
|
|
71
|
+
"@convex-dev/component-source": "./src/nextjs/index.ts",
|
|
72
|
+
"types": "./dist/commonjs/nextjs/index.d.ts",
|
|
73
|
+
"default": "./dist/commonjs/nextjs/index.js"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"./plugins": {
|
|
77
|
+
"import": {
|
|
78
|
+
"@convex-dev/component-source": "./src/plugins/index.ts",
|
|
79
|
+
"types": "./dist/esm/plugins/index.d.ts",
|
|
80
|
+
"default": "./dist/esm/plugins/index.js"
|
|
81
|
+
},
|
|
82
|
+
"require": {
|
|
83
|
+
"@convex-dev/component-source": "./src/plugins/index.ts",
|
|
84
|
+
"types": "./dist/commonjs/plugins/index.d.ts",
|
|
85
|
+
"default": "./dist/commonjs/plugins/index.js"
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"./react": {
|
|
89
|
+
"import": {
|
|
90
|
+
"@convex-dev/component-source": "./src/react/index.tsx",
|
|
91
|
+
"types": "./dist/esm/react/index.d.ts",
|
|
92
|
+
"default": "./dist/esm/react/index.js"
|
|
93
|
+
},
|
|
94
|
+
"require": {
|
|
95
|
+
"@convex-dev/component-source": "./src/react/index.tsx",
|
|
96
|
+
"types": "./dist/commonjs/react/index.d.ts",
|
|
97
|
+
"default": "./dist/commonjs/react/index.js"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"./react-start": {
|
|
101
|
+
"import": {
|
|
102
|
+
"@convex-dev/component-source": "./src/react-start/index.ts",
|
|
103
|
+
"types": "./dist/esm/react-start/index.d.ts",
|
|
104
|
+
"default": "./dist/esm/react-start/index.js"
|
|
105
|
+
},
|
|
106
|
+
"require": {
|
|
107
|
+
"@convex-dev/component-source": "./src/react-start/index.ts",
|
|
108
|
+
"types": "./dist/commonjs/react-start/index.d.ts",
|
|
109
|
+
"default": "./dist/commonjs/react-start/index.js"
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
"./convex.config": {
|
|
113
|
+
"import": {
|
|
114
|
+
"@convex-dev/component-source": "./src/component/convex.config.ts",
|
|
115
|
+
"types": "./dist/esm/component/convex.config.d.ts",
|
|
116
|
+
"default": "./dist/esm/component/convex.config.js"
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
"typesVersions": {
|
|
121
|
+
"*": {
|
|
122
|
+
"next-js": [
|
|
123
|
+
"./dist/esm/nextjs/index.d.ts"
|
|
124
|
+
]
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
"peerDependencies": {
|
|
128
|
+
"better-auth": "1.2.7",
|
|
129
|
+
"convex": "~1.16.5 || >=1.17.0 <1.35.0",
|
|
130
|
+
"react": "^18.3.1 || ^19.0.0",
|
|
131
|
+
"react-dom": "^18.3.1 || ^19.0.0"
|
|
132
|
+
},
|
|
133
|
+
"devDependencies": {
|
|
134
|
+
"@eslint/js": "^9.9.1",
|
|
135
|
+
"@tanstack/react-start": "^1.120.19",
|
|
136
|
+
"@types/node": "^18.17.0",
|
|
137
|
+
"@types/react": "19.1.6",
|
|
138
|
+
"@types/react-dom": "19.1.6",
|
|
139
|
+
"convex-test": "^0.0.33",
|
|
140
|
+
"eslint": "^9.9.1",
|
|
141
|
+
"globals": "^15.9.0",
|
|
142
|
+
"next": "^15.1.8",
|
|
143
|
+
"prettier": "3.2.5",
|
|
144
|
+
"tsc-alias": "^1.8.16",
|
|
145
|
+
"typescript": "^5.8.2",
|
|
146
|
+
"typescript-eslint": "^8.4.0",
|
|
147
|
+
"vitest": "^3.2.2"
|
|
148
|
+
},
|
|
149
|
+
"main": "./dist/commonjs/client/index.js",
|
|
150
|
+
"types": "./dist/commonjs/client/index.d.ts",
|
|
151
|
+
"module": "./dist/esm/client/index.js",
|
|
152
|
+
"dependencies": {
|
|
153
|
+
"@better-auth/utils": "^0.2.5",
|
|
154
|
+
"better-call": "^1.0.9",
|
|
155
|
+
"convex-helpers": "^0.1.87",
|
|
156
|
+
"is-network-error": "^1.1.0",
|
|
157
|
+
"remeda": "^2.21.6",
|
|
158
|
+
"type-fest": "^4.39.1",
|
|
159
|
+
"zod": "^3.24.4"
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { BetterAuth } from "./index";
|
|
2
|
+
import { transformInput } from "../component/lib";
|
|
3
|
+
import { createAdapter } from "better-auth/adapters";
|
|
4
|
+
import {
|
|
5
|
+
GenericActionCtx,
|
|
6
|
+
GenericMutationCtx,
|
|
7
|
+
GenericQueryCtx,
|
|
8
|
+
} from "convex/server";
|
|
9
|
+
|
|
10
|
+
export const convexAdapter = <
|
|
11
|
+
Ctx extends
|
|
12
|
+
| GenericQueryCtx<any>
|
|
13
|
+
| GenericMutationCtx<any>
|
|
14
|
+
| GenericActionCtx<any>,
|
|
15
|
+
>(
|
|
16
|
+
ctx: Ctx,
|
|
17
|
+
component: BetterAuth
|
|
18
|
+
) =>
|
|
19
|
+
createAdapter({
|
|
20
|
+
config: {
|
|
21
|
+
adapterId: "convex",
|
|
22
|
+
adapterName: "Convex Adapter",
|
|
23
|
+
debugLogs: component.config.verbose ?? false,
|
|
24
|
+
disableIdGeneration: true,
|
|
25
|
+
},
|
|
26
|
+
adapter: ({ schema }) => {
|
|
27
|
+
return {
|
|
28
|
+
id: "convex",
|
|
29
|
+
create: async ({ model, data, select }): Promise<any> => {
|
|
30
|
+
if (!("runMutation" in ctx)) {
|
|
31
|
+
throw new Error("ctx is not an action ctx");
|
|
32
|
+
}
|
|
33
|
+
if (select) {
|
|
34
|
+
throw new Error("select is not supported");
|
|
35
|
+
}
|
|
36
|
+
const createFn =
|
|
37
|
+
model === "user"
|
|
38
|
+
? component.config.authFunctions.createUser
|
|
39
|
+
: model === "session"
|
|
40
|
+
? component.config.authFunctions.createSession
|
|
41
|
+
: component.component.lib.create;
|
|
42
|
+
return ctx.runMutation(createFn, {
|
|
43
|
+
input: { table: model, ...transformInput(model, data) },
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
findOne: async ({ model, where }): Promise<any> => {
|
|
47
|
+
if (where.length === 1 && where[0].operator === "eq") {
|
|
48
|
+
const { value, field } = where[0];
|
|
49
|
+
const result = await ctx.runQuery(component.component.lib.getBy, {
|
|
50
|
+
table: model,
|
|
51
|
+
field,
|
|
52
|
+
unique:
|
|
53
|
+
field === "id" ? true : schema[model].fields[field].unique,
|
|
54
|
+
value: value instanceof Date ? value.getTime() : value,
|
|
55
|
+
});
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
if (
|
|
59
|
+
model === "account" &&
|
|
60
|
+
where.length === 2 &&
|
|
61
|
+
where[0].field === "accountId" &&
|
|
62
|
+
where[1].field === "providerId" &&
|
|
63
|
+
where[0].connector === "AND"
|
|
64
|
+
) {
|
|
65
|
+
return ctx.runQuery(
|
|
66
|
+
component.component.lib.getAccountByAccountIdAndProviderId,
|
|
67
|
+
{
|
|
68
|
+
accountId: where[0].value as string,
|
|
69
|
+
providerId: where[1].value as string,
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
throw new Error("where clause not supported");
|
|
74
|
+
},
|
|
75
|
+
findMany: async ({
|
|
76
|
+
model,
|
|
77
|
+
where,
|
|
78
|
+
sortBy,
|
|
79
|
+
offset,
|
|
80
|
+
limit,
|
|
81
|
+
}): Promise<any[]> => {
|
|
82
|
+
if (offset) {
|
|
83
|
+
throw new Error("where clause not supported");
|
|
84
|
+
}
|
|
85
|
+
if (
|
|
86
|
+
model === "jwks" &&
|
|
87
|
+
!where &&
|
|
88
|
+
(!sortBy ||
|
|
89
|
+
(sortBy?.field === "createdAt" && sortBy?.direction === "desc"))
|
|
90
|
+
) {
|
|
91
|
+
return ctx.runQuery(component.component.lib.getJwks, { limit });
|
|
92
|
+
}
|
|
93
|
+
if (where?.length !== 1 || where[0].operator !== "eq") {
|
|
94
|
+
throw new Error("where clause not supported");
|
|
95
|
+
}
|
|
96
|
+
if (offset) {
|
|
97
|
+
throw new Error("offset not supported");
|
|
98
|
+
}
|
|
99
|
+
if (model === "account" && where[0].field === "userId") {
|
|
100
|
+
return ctx.runQuery(component.component.lib.getAccountsByUserId, {
|
|
101
|
+
userId: where[0].value as any,
|
|
102
|
+
limit,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
if (model === "verification" && where[0].field === "identifier") {
|
|
106
|
+
return ctx.runQuery(
|
|
107
|
+
component.component.lib.listVerificationsByIdentifier,
|
|
108
|
+
{
|
|
109
|
+
identifier: where[0].value as string,
|
|
110
|
+
sortBy,
|
|
111
|
+
limit,
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
throw new Error("where clause not supported");
|
|
116
|
+
},
|
|
117
|
+
count: async ({ where }) => {
|
|
118
|
+
throw new Error("count not implemented");
|
|
119
|
+
// return 0;
|
|
120
|
+
},
|
|
121
|
+
update: async ({ model, where, update }): Promise<any> => {
|
|
122
|
+
if (!("runMutation" in ctx)) {
|
|
123
|
+
throw new Error("ctx is not an action ctx");
|
|
124
|
+
}
|
|
125
|
+
if (where?.length === 1 && where[0].operator === "eq") {
|
|
126
|
+
const { value, field } = where[0];
|
|
127
|
+
const updateFn =
|
|
128
|
+
model === "user"
|
|
129
|
+
? component.config.authFunctions.updateUser
|
|
130
|
+
: component.component.lib.update;
|
|
131
|
+
return ctx.runMutation(updateFn, {
|
|
132
|
+
input: {
|
|
133
|
+
table: model as any,
|
|
134
|
+
where: {
|
|
135
|
+
field,
|
|
136
|
+
value: value instanceof Date ? value.getTime() : value,
|
|
137
|
+
},
|
|
138
|
+
value: transformInput(model, update as any),
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
throw new Error("where clause not supported");
|
|
143
|
+
},
|
|
144
|
+
delete: async ({ model, where }) => {
|
|
145
|
+
if (!("runMutation" in ctx)) {
|
|
146
|
+
throw new Error("ctx is not an action ctx");
|
|
147
|
+
}
|
|
148
|
+
if (where?.length === 1 && where[0].operator === "eq") {
|
|
149
|
+
const { field, value } = where[0];
|
|
150
|
+
const deleteFn =
|
|
151
|
+
model === "user"
|
|
152
|
+
? component.config.authFunctions.deleteUser
|
|
153
|
+
: component.component.lib.deleteBy;
|
|
154
|
+
await ctx.runMutation(deleteFn, {
|
|
155
|
+
table: model,
|
|
156
|
+
field,
|
|
157
|
+
value: value instanceof Date ? value.getTime() : value,
|
|
158
|
+
});
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
throw new Error("where clause not supported");
|
|
162
|
+
// return null
|
|
163
|
+
},
|
|
164
|
+
deleteMany: async ({ model, where }) => {
|
|
165
|
+
if (!("runAction" in ctx)) {
|
|
166
|
+
throw new Error("ctx is not an action ctx");
|
|
167
|
+
}
|
|
168
|
+
if (
|
|
169
|
+
model === "verification" &&
|
|
170
|
+
where?.length === 1 &&
|
|
171
|
+
where[0].operator === "lt" &&
|
|
172
|
+
where[0].field === "expiresAt"
|
|
173
|
+
) {
|
|
174
|
+
return ctx.runAction(
|
|
175
|
+
component.component.lib.deleteOldVerifications,
|
|
176
|
+
{
|
|
177
|
+
currentTimestamp: Date.now(),
|
|
178
|
+
}
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
if (where?.length === 1 && where[0].field === "userId") {
|
|
182
|
+
return ctx.runAction(component.component.lib.deleteAllForUser, {
|
|
183
|
+
table: model,
|
|
184
|
+
userId: where[0].value as any,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
throw new Error("where clause not supported");
|
|
188
|
+
// return count;
|
|
189
|
+
},
|
|
190
|
+
updateMany: async ({ model, where, update }) => {
|
|
191
|
+
if (!("runMutation" in ctx)) {
|
|
192
|
+
throw new Error("ctx is not an action ctx");
|
|
193
|
+
}
|
|
194
|
+
if (
|
|
195
|
+
model === "twoFactor" &&
|
|
196
|
+
where?.length === 1 &&
|
|
197
|
+
where[0].operator === "eq" &&
|
|
198
|
+
where[0].field === "userId"
|
|
199
|
+
) {
|
|
200
|
+
return ctx.runMutation(component.component.lib.updateTwoFactor, {
|
|
201
|
+
userId: where[0].value as string,
|
|
202
|
+
update: transformInput(model, update as any),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
if (
|
|
206
|
+
model === "account" &&
|
|
207
|
+
where?.length === 2 &&
|
|
208
|
+
where[0].operator === "eq" &&
|
|
209
|
+
where[0].connector === "AND" &&
|
|
210
|
+
where[0].field === "userId" &&
|
|
211
|
+
where[1].field === "providerId"
|
|
212
|
+
) {
|
|
213
|
+
return ctx.runMutation(
|
|
214
|
+
component.component.lib.updateUserProviderAccounts,
|
|
215
|
+
{
|
|
216
|
+
userId: where[0].value as string,
|
|
217
|
+
providerId: where[1].value as string,
|
|
218
|
+
update: transformInput(model, update as any),
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
throw new Error("updateMany not implemented");
|
|
223
|
+
//return 0;
|
|
224
|
+
/*
|
|
225
|
+
const { model, where, update } = data;
|
|
226
|
+
const table = db[model];
|
|
227
|
+
const res = convertWhereClause(where, table, model);
|
|
228
|
+
res.forEach((record) => {
|
|
229
|
+
Object.assign(record, update);
|
|
230
|
+
});
|
|
231
|
+
return res[0] || null;
|
|
232
|
+
*/
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
});
|