@better-auth/core 1.4.0-beta.6 → 1.4.0-beta.8
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 +22 -9
- package/build.config.ts +8 -1
- package/dist/async_hooks/index.cjs +27 -0
- package/dist/async_hooks/index.d.cts +10 -0
- package/dist/async_hooks/index.d.mts +10 -0
- package/dist/async_hooks/index.d.ts +10 -0
- package/dist/async_hooks/index.mjs +25 -0
- package/dist/db/adapter/index.cjs +2 -0
- package/dist/db/adapter/index.d.cts +23 -0
- package/dist/db/adapter/index.d.mts +23 -0
- package/dist/db/adapter/index.d.ts +23 -0
- package/dist/db/adapter/index.mjs +1 -0
- package/dist/db/index.cjs +89 -0
- package/dist/db/index.d.cts +102 -105
- package/dist/db/index.d.mts +102 -105
- package/dist/db/index.d.ts +102 -105
- package/dist/db/index.mjs +69 -0
- package/dist/env/index.cjs +315 -0
- package/dist/env/index.d.cts +85 -0
- package/dist/env/index.d.mts +85 -0
- package/dist/env/index.d.ts +85 -0
- package/dist/env/index.mjs +300 -0
- package/dist/index.d.cts +114 -1
- package/dist/index.d.mts +114 -1
- package/dist/index.d.ts +114 -1
- package/dist/oauth2/index.cjs +368 -0
- package/dist/oauth2/index.d.cts +277 -0
- package/dist/oauth2/index.d.mts +277 -0
- package/dist/oauth2/index.d.ts +277 -0
- package/dist/oauth2/index.mjs +357 -0
- package/dist/shared/core.DeNN5HMO.d.cts +143 -0
- package/dist/shared/core.DeNN5HMO.d.mts +143 -0
- package/dist/shared/core.DeNN5HMO.d.ts +143 -0
- package/package.json +52 -1
- package/src/async_hooks/index.ts +43 -0
- package/src/db/adapter/index.ts +24 -0
- package/src/db/index.ts +13 -0
- package/src/db/plugin.ts +11 -0
- package/src/db/schema/account.ts +34 -0
- package/src/db/schema/rate-limit.ts +21 -0
- package/src/db/schema/session.ts +17 -0
- package/src/db/schema/shared.ts +7 -0
- package/src/db/schema/user.ts +16 -0
- package/src/db/schema/verification.ts +15 -0
- package/src/db/type.ts +50 -0
- package/src/env/color-depth.ts +171 -0
- package/src/env/env-impl.ts +123 -0
- package/src/env/index.ts +23 -0
- package/src/env/logger.test.ts +33 -0
- package/src/env/logger.ts +145 -0
- package/src/index.ts +3 -1
- package/src/oauth2/client-credentials-token.ts +102 -0
- package/src/oauth2/create-authorization-url.ts +85 -0
- package/src/oauth2/index.ts +22 -0
- package/src/oauth2/oauth-provider.ts +194 -0
- package/src/oauth2/refresh-access-token.ts +124 -0
- package/src/oauth2/utils.ts +36 -0
- package/src/oauth2/validate-authorization-code.ts +156 -0
- package/src/types/helper.ts +5 -0
- package/src/types/index.ts +2 -1
- package/src/types/init-options.ts +119 -0
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { base64Url, base64 } from '@better-auth/utils/base64';
|
|
2
|
+
import { betterFetch } from '@better-fetch/fetch';
|
|
3
|
+
import { jwtVerify } from 'jose';
|
|
4
|
+
|
|
5
|
+
function getOAuth2Tokens(data) {
|
|
6
|
+
const getDate = (seconds) => {
|
|
7
|
+
const now = /* @__PURE__ */ new Date();
|
|
8
|
+
return new Date(now.getTime() + seconds * 1e3);
|
|
9
|
+
};
|
|
10
|
+
return {
|
|
11
|
+
tokenType: data.token_type,
|
|
12
|
+
accessToken: data.access_token,
|
|
13
|
+
refreshToken: data.refresh_token,
|
|
14
|
+
accessTokenExpiresAt: data.expires_in ? getDate(data.expires_in) : void 0,
|
|
15
|
+
refreshTokenExpiresAt: data.refresh_token_expires_in ? getDate(data.refresh_token_expires_in) : void 0,
|
|
16
|
+
scopes: data?.scope ? typeof data.scope === "string" ? data.scope.split(" ") : data.scope : [],
|
|
17
|
+
idToken: data.id_token
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
async function generateCodeChallenge(codeVerifier) {
|
|
21
|
+
const encoder = new TextEncoder();
|
|
22
|
+
const data = encoder.encode(codeVerifier);
|
|
23
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
24
|
+
return base64Url.encode(new Uint8Array(hash), {
|
|
25
|
+
padding: false
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function createAuthorizationURL({
|
|
30
|
+
id,
|
|
31
|
+
options,
|
|
32
|
+
authorizationEndpoint,
|
|
33
|
+
state,
|
|
34
|
+
codeVerifier,
|
|
35
|
+
scopes,
|
|
36
|
+
claims,
|
|
37
|
+
redirectURI,
|
|
38
|
+
duration,
|
|
39
|
+
prompt,
|
|
40
|
+
accessType,
|
|
41
|
+
responseType,
|
|
42
|
+
display,
|
|
43
|
+
loginHint,
|
|
44
|
+
hd,
|
|
45
|
+
responseMode,
|
|
46
|
+
additionalParams,
|
|
47
|
+
scopeJoiner
|
|
48
|
+
}) {
|
|
49
|
+
const url = new URL(authorizationEndpoint);
|
|
50
|
+
url.searchParams.set("response_type", responseType || "code");
|
|
51
|
+
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
52
|
+
url.searchParams.set("client_id", primaryClientId);
|
|
53
|
+
url.searchParams.set("state", state);
|
|
54
|
+
url.searchParams.set("scope", scopes.join(scopeJoiner || " "));
|
|
55
|
+
url.searchParams.set("redirect_uri", options.redirectURI || redirectURI);
|
|
56
|
+
duration && url.searchParams.set("duration", duration);
|
|
57
|
+
display && url.searchParams.set("display", display);
|
|
58
|
+
loginHint && url.searchParams.set("login_hint", loginHint);
|
|
59
|
+
prompt && url.searchParams.set("prompt", prompt);
|
|
60
|
+
hd && url.searchParams.set("hd", hd);
|
|
61
|
+
accessType && url.searchParams.set("access_type", accessType);
|
|
62
|
+
responseMode && url.searchParams.set("response_mode", responseMode);
|
|
63
|
+
if (codeVerifier) {
|
|
64
|
+
const codeChallenge = await generateCodeChallenge(codeVerifier);
|
|
65
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
66
|
+
url.searchParams.set("code_challenge", codeChallenge);
|
|
67
|
+
}
|
|
68
|
+
if (claims) {
|
|
69
|
+
const claimsObj = claims.reduce(
|
|
70
|
+
(acc, claim) => {
|
|
71
|
+
acc[claim] = null;
|
|
72
|
+
return acc;
|
|
73
|
+
},
|
|
74
|
+
{}
|
|
75
|
+
);
|
|
76
|
+
url.searchParams.set(
|
|
77
|
+
"claims",
|
|
78
|
+
JSON.stringify({
|
|
79
|
+
id_token: { email: null, email_verified: null, ...claimsObj }
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (additionalParams) {
|
|
84
|
+
Object.entries(additionalParams).forEach(([key, value]) => {
|
|
85
|
+
url.searchParams.set(key, value);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return url;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function createAuthorizationCodeRequest({
|
|
92
|
+
code,
|
|
93
|
+
codeVerifier,
|
|
94
|
+
redirectURI,
|
|
95
|
+
options,
|
|
96
|
+
authentication,
|
|
97
|
+
deviceId,
|
|
98
|
+
headers,
|
|
99
|
+
additionalParams = {},
|
|
100
|
+
resource
|
|
101
|
+
}) {
|
|
102
|
+
const body = new URLSearchParams();
|
|
103
|
+
const requestHeaders = {
|
|
104
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
105
|
+
accept: "application/json",
|
|
106
|
+
"user-agent": "better-auth",
|
|
107
|
+
...headers
|
|
108
|
+
};
|
|
109
|
+
body.set("grant_type", "authorization_code");
|
|
110
|
+
body.set("code", code);
|
|
111
|
+
codeVerifier && body.set("code_verifier", codeVerifier);
|
|
112
|
+
options.clientKey && body.set("client_key", options.clientKey);
|
|
113
|
+
deviceId && body.set("device_id", deviceId);
|
|
114
|
+
body.set("redirect_uri", options.redirectURI || redirectURI);
|
|
115
|
+
if (resource) {
|
|
116
|
+
if (typeof resource === "string") {
|
|
117
|
+
body.append("resource", resource);
|
|
118
|
+
} else {
|
|
119
|
+
for (const _resource of resource) {
|
|
120
|
+
body.append("resource", _resource);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (authentication === "basic") {
|
|
125
|
+
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
126
|
+
const encodedCredentials = base64.encode(
|
|
127
|
+
`${primaryClientId}:${options.clientSecret ?? ""}`
|
|
128
|
+
);
|
|
129
|
+
requestHeaders["authorization"] = `Basic ${encodedCredentials}`;
|
|
130
|
+
} else {
|
|
131
|
+
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
132
|
+
body.set("client_id", primaryClientId);
|
|
133
|
+
if (options.clientSecret) {
|
|
134
|
+
body.set("client_secret", options.clientSecret);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (const [key, value] of Object.entries(additionalParams)) {
|
|
138
|
+
if (!body.has(key)) body.append(key, value);
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
body,
|
|
142
|
+
headers: requestHeaders
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
async function validateAuthorizationCode({
|
|
146
|
+
code,
|
|
147
|
+
codeVerifier,
|
|
148
|
+
redirectURI,
|
|
149
|
+
options,
|
|
150
|
+
tokenEndpoint,
|
|
151
|
+
authentication,
|
|
152
|
+
deviceId,
|
|
153
|
+
headers,
|
|
154
|
+
additionalParams = {},
|
|
155
|
+
resource
|
|
156
|
+
}) {
|
|
157
|
+
const { body, headers: requestHeaders } = createAuthorizationCodeRequest({
|
|
158
|
+
code,
|
|
159
|
+
codeVerifier,
|
|
160
|
+
redirectURI,
|
|
161
|
+
options,
|
|
162
|
+
authentication,
|
|
163
|
+
deviceId,
|
|
164
|
+
headers,
|
|
165
|
+
additionalParams,
|
|
166
|
+
resource
|
|
167
|
+
});
|
|
168
|
+
const { data, error } = await betterFetch(tokenEndpoint, {
|
|
169
|
+
method: "POST",
|
|
170
|
+
body,
|
|
171
|
+
headers: requestHeaders
|
|
172
|
+
});
|
|
173
|
+
if (error) {
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
const tokens = getOAuth2Tokens(data);
|
|
177
|
+
return tokens;
|
|
178
|
+
}
|
|
179
|
+
async function validateToken(token, jwksEndpoint) {
|
|
180
|
+
const { data, error } = await betterFetch(jwksEndpoint, {
|
|
181
|
+
method: "GET",
|
|
182
|
+
headers: {
|
|
183
|
+
accept: "application/json",
|
|
184
|
+
"user-agent": "better-auth"
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
if (error) {
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
const keys = data["keys"];
|
|
191
|
+
const header = JSON.parse(atob(token.split(".")[0]));
|
|
192
|
+
const key = keys.find((key2) => key2.kid === header.kid);
|
|
193
|
+
if (!key) {
|
|
194
|
+
throw new Error("Key not found");
|
|
195
|
+
}
|
|
196
|
+
const verified = await jwtVerify(token, key);
|
|
197
|
+
return verified;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function createRefreshAccessTokenRequest({
|
|
201
|
+
refreshToken,
|
|
202
|
+
options,
|
|
203
|
+
authentication,
|
|
204
|
+
extraParams,
|
|
205
|
+
resource
|
|
206
|
+
}) {
|
|
207
|
+
const body = new URLSearchParams();
|
|
208
|
+
const headers = {
|
|
209
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
210
|
+
accept: "application/json"
|
|
211
|
+
};
|
|
212
|
+
body.set("grant_type", "refresh_token");
|
|
213
|
+
body.set("refresh_token", refreshToken);
|
|
214
|
+
if (authentication === "basic") {
|
|
215
|
+
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
216
|
+
if (primaryClientId) {
|
|
217
|
+
headers["authorization"] = "Basic " + base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`);
|
|
218
|
+
} else {
|
|
219
|
+
headers["authorization"] = "Basic " + base64.encode(`:${options.clientSecret ?? ""}`);
|
|
220
|
+
}
|
|
221
|
+
} else {
|
|
222
|
+
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
223
|
+
body.set("client_id", primaryClientId);
|
|
224
|
+
if (options.clientSecret) {
|
|
225
|
+
body.set("client_secret", options.clientSecret);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (resource) {
|
|
229
|
+
if (typeof resource === "string") {
|
|
230
|
+
body.append("resource", resource);
|
|
231
|
+
} else {
|
|
232
|
+
for (const _resource of resource) {
|
|
233
|
+
body.append("resource", _resource);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (extraParams) {
|
|
238
|
+
for (const [key, value] of Object.entries(extraParams)) {
|
|
239
|
+
body.set(key, value);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
body,
|
|
244
|
+
headers
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
async function refreshAccessToken({
|
|
248
|
+
refreshToken,
|
|
249
|
+
options,
|
|
250
|
+
tokenEndpoint,
|
|
251
|
+
authentication,
|
|
252
|
+
extraParams
|
|
253
|
+
}) {
|
|
254
|
+
const { body, headers } = createRefreshAccessTokenRequest({
|
|
255
|
+
refreshToken,
|
|
256
|
+
options,
|
|
257
|
+
authentication,
|
|
258
|
+
extraParams
|
|
259
|
+
});
|
|
260
|
+
const { data, error } = await betterFetch(tokenEndpoint, {
|
|
261
|
+
method: "POST",
|
|
262
|
+
body,
|
|
263
|
+
headers
|
|
264
|
+
});
|
|
265
|
+
if (error) {
|
|
266
|
+
throw error;
|
|
267
|
+
}
|
|
268
|
+
const tokens = {
|
|
269
|
+
accessToken: data.access_token,
|
|
270
|
+
refreshToken: data.refresh_token,
|
|
271
|
+
tokenType: data.token_type,
|
|
272
|
+
scopes: data.scope?.split(" "),
|
|
273
|
+
idToken: data.id_token
|
|
274
|
+
};
|
|
275
|
+
if (data.expires_in) {
|
|
276
|
+
const now = /* @__PURE__ */ new Date();
|
|
277
|
+
tokens.accessTokenExpiresAt = new Date(
|
|
278
|
+
now.getTime() + data.expires_in * 1e3
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
return tokens;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function createClientCredentialsTokenRequest({
|
|
285
|
+
options,
|
|
286
|
+
scope,
|
|
287
|
+
authentication,
|
|
288
|
+
resource
|
|
289
|
+
}) {
|
|
290
|
+
const body = new URLSearchParams();
|
|
291
|
+
const headers = {
|
|
292
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
293
|
+
accept: "application/json"
|
|
294
|
+
};
|
|
295
|
+
body.set("grant_type", "client_credentials");
|
|
296
|
+
scope && body.set("scope", scope);
|
|
297
|
+
if (resource) {
|
|
298
|
+
if (typeof resource === "string") {
|
|
299
|
+
body.append("resource", resource);
|
|
300
|
+
} else {
|
|
301
|
+
for (const _resource of resource) {
|
|
302
|
+
body.append("resource", _resource);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (authentication === "basic") {
|
|
307
|
+
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
308
|
+
const encodedCredentials = base64Url.encode(
|
|
309
|
+
`${primaryClientId}:${options.clientSecret}`
|
|
310
|
+
);
|
|
311
|
+
headers["authorization"] = `Basic ${encodedCredentials}`;
|
|
312
|
+
} else {
|
|
313
|
+
const primaryClientId = Array.isArray(options.clientId) ? options.clientId[0] : options.clientId;
|
|
314
|
+
body.set("client_id", primaryClientId);
|
|
315
|
+
body.set("client_secret", options.clientSecret);
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
body,
|
|
319
|
+
headers
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
async function clientCredentialsToken({
|
|
323
|
+
options,
|
|
324
|
+
tokenEndpoint,
|
|
325
|
+
scope,
|
|
326
|
+
authentication,
|
|
327
|
+
resource
|
|
328
|
+
}) {
|
|
329
|
+
const { body, headers } = createClientCredentialsTokenRequest({
|
|
330
|
+
options,
|
|
331
|
+
scope,
|
|
332
|
+
authentication,
|
|
333
|
+
resource
|
|
334
|
+
});
|
|
335
|
+
const { data, error } = await betterFetch(tokenEndpoint, {
|
|
336
|
+
method: "POST",
|
|
337
|
+
body,
|
|
338
|
+
headers
|
|
339
|
+
});
|
|
340
|
+
if (error) {
|
|
341
|
+
throw error;
|
|
342
|
+
}
|
|
343
|
+
const tokens = {
|
|
344
|
+
accessToken: data.access_token,
|
|
345
|
+
tokenType: data.token_type,
|
|
346
|
+
scopes: data.scope?.split(" ")
|
|
347
|
+
};
|
|
348
|
+
if (data.expires_in) {
|
|
349
|
+
const now = /* @__PURE__ */ new Date();
|
|
350
|
+
tokens.accessTokenExpiresAt = new Date(
|
|
351
|
+
now.getTime() + data.expires_in * 1e3
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
return tokens;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export { clientCredentialsToken, createAuthorizationCodeRequest, createAuthorizationURL, createClientCredentialsTokenRequest, createRefreshAccessTokenRequest, generateCodeChallenge, getOAuth2Tokens, refreshAccessToken, validateAuthorizationCode, validateToken };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
|
|
3
|
+
type Primitive = string | number | symbol | bigint | boolean | null | undefined;
|
|
4
|
+
type LiteralString = "" | (string & Record<never, never>);
|
|
5
|
+
type LiteralUnion<LiteralType, BaseType extends Primitive> = LiteralType | (BaseType & Record<never, never>);
|
|
6
|
+
|
|
7
|
+
declare module "../index" {
|
|
8
|
+
interface BetterAuthMutators<O, C> {
|
|
9
|
+
"better-auth/db": {};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
type Models = "user" | "account" | "session" | "verification" | "rate-limit" | "organization" | "member" | "invitation" | "jwks" | "passkey" | "two-factor";
|
|
13
|
+
type DBFieldType = "string" | "number" | "boolean" | "date" | "json" | `${"string" | "number"}[]` | Array<LiteralString>;
|
|
14
|
+
type DBPrimitive = string | number | boolean | Date | null | undefined | string[] | number[];
|
|
15
|
+
type DBFieldAttributeConfig = {
|
|
16
|
+
/**
|
|
17
|
+
* If the field should be required on a new record.
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
required?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* If the value should be returned on a response body.
|
|
23
|
+
* @default true
|
|
24
|
+
*/
|
|
25
|
+
returned?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* If a value should be provided when creating a new record.
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
input?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Default value for the field
|
|
33
|
+
*
|
|
34
|
+
* Note: This will not create a default value on the database level. It will only
|
|
35
|
+
* be used when creating a new record.
|
|
36
|
+
*/
|
|
37
|
+
defaultValue?: DBPrimitive | (() => DBPrimitive);
|
|
38
|
+
/**
|
|
39
|
+
* Update value for the field
|
|
40
|
+
*
|
|
41
|
+
* Note: This will create an onUpdate trigger on the database level for supported adapters.
|
|
42
|
+
* It will be called when updating a record.
|
|
43
|
+
*/
|
|
44
|
+
onUpdate?: () => DBPrimitive;
|
|
45
|
+
/**
|
|
46
|
+
* transform the value before storing it.
|
|
47
|
+
*/
|
|
48
|
+
transform?: {
|
|
49
|
+
input?: (value: DBPrimitive) => DBPrimitive | Promise<DBPrimitive>;
|
|
50
|
+
output?: (value: DBPrimitive) => DBPrimitive | Promise<DBPrimitive>;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Reference to another model.
|
|
54
|
+
*/
|
|
55
|
+
references?: {
|
|
56
|
+
/**
|
|
57
|
+
* The model to reference.
|
|
58
|
+
*/
|
|
59
|
+
model: string;
|
|
60
|
+
/**
|
|
61
|
+
* The field on the referenced model.
|
|
62
|
+
*/
|
|
63
|
+
field: string;
|
|
64
|
+
/**
|
|
65
|
+
* The action to perform when the reference is deleted.
|
|
66
|
+
* @default "cascade"
|
|
67
|
+
*/
|
|
68
|
+
onDelete?: "no action" | "restrict" | "cascade" | "set null" | "set default";
|
|
69
|
+
};
|
|
70
|
+
unique?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* If the field should be a bigint on the database instead of integer.
|
|
73
|
+
*/
|
|
74
|
+
bigint?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* A zod schema to validate the value.
|
|
77
|
+
*/
|
|
78
|
+
validator?: {
|
|
79
|
+
input?: ZodType;
|
|
80
|
+
output?: ZodType;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* The name of the field on the database.
|
|
84
|
+
*/
|
|
85
|
+
fieldName?: string;
|
|
86
|
+
/**
|
|
87
|
+
* If the field should be sortable.
|
|
88
|
+
*
|
|
89
|
+
* applicable only for `text` type.
|
|
90
|
+
* It's useful to mark fields varchar instead of text.
|
|
91
|
+
*/
|
|
92
|
+
sortable?: boolean;
|
|
93
|
+
};
|
|
94
|
+
type DBFieldAttribute<T extends DBFieldType = DBFieldType> = {
|
|
95
|
+
type: T;
|
|
96
|
+
} & DBFieldAttributeConfig;
|
|
97
|
+
type BetterAuthDBSchema = Record<string, {
|
|
98
|
+
/**
|
|
99
|
+
* The name of the table in the database
|
|
100
|
+
*/
|
|
101
|
+
modelName: string;
|
|
102
|
+
/**
|
|
103
|
+
* The fields of the table
|
|
104
|
+
*/
|
|
105
|
+
fields: Record<string, DBFieldAttribute>;
|
|
106
|
+
/**
|
|
107
|
+
* Whether to disable migrations for this table
|
|
108
|
+
* @default false
|
|
109
|
+
*/
|
|
110
|
+
disableMigrations?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* The order of the table
|
|
113
|
+
*/
|
|
114
|
+
order?: number;
|
|
115
|
+
}>;
|
|
116
|
+
interface SecondaryStorage {
|
|
117
|
+
/**
|
|
118
|
+
*
|
|
119
|
+
* @param key - Key to get
|
|
120
|
+
* @returns - Value of the key
|
|
121
|
+
*/
|
|
122
|
+
get: (key: string) => Promise<unknown> | unknown;
|
|
123
|
+
set: (
|
|
124
|
+
/**
|
|
125
|
+
* Key to store
|
|
126
|
+
*/
|
|
127
|
+
key: string,
|
|
128
|
+
/**
|
|
129
|
+
* Value to store
|
|
130
|
+
*/
|
|
131
|
+
value: string,
|
|
132
|
+
/**
|
|
133
|
+
* Time to live in seconds
|
|
134
|
+
*/
|
|
135
|
+
ttl?: number) => Promise<void | null | unknown> | void;
|
|
136
|
+
/**
|
|
137
|
+
*
|
|
138
|
+
* @param key - Key to delete
|
|
139
|
+
*/
|
|
140
|
+
delete: (key: string) => Promise<void | null | string> | void;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export type { BetterAuthDBSchema as B, DBFieldAttribute as D, LiteralUnion as L, Models as M, SecondaryStorage as S, LiteralString as a, DBFieldAttributeConfig as b, DBFieldType as c, DBPrimitive as d };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
|
|
3
|
+
type Primitive = string | number | symbol | bigint | boolean | null | undefined;
|
|
4
|
+
type LiteralString = "" | (string & Record<never, never>);
|
|
5
|
+
type LiteralUnion<LiteralType, BaseType extends Primitive> = LiteralType | (BaseType & Record<never, never>);
|
|
6
|
+
|
|
7
|
+
declare module "../index" {
|
|
8
|
+
interface BetterAuthMutators<O, C> {
|
|
9
|
+
"better-auth/db": {};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
type Models = "user" | "account" | "session" | "verification" | "rate-limit" | "organization" | "member" | "invitation" | "jwks" | "passkey" | "two-factor";
|
|
13
|
+
type DBFieldType = "string" | "number" | "boolean" | "date" | "json" | `${"string" | "number"}[]` | Array<LiteralString>;
|
|
14
|
+
type DBPrimitive = string | number | boolean | Date | null | undefined | string[] | number[];
|
|
15
|
+
type DBFieldAttributeConfig = {
|
|
16
|
+
/**
|
|
17
|
+
* If the field should be required on a new record.
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
required?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* If the value should be returned on a response body.
|
|
23
|
+
* @default true
|
|
24
|
+
*/
|
|
25
|
+
returned?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* If a value should be provided when creating a new record.
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
input?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Default value for the field
|
|
33
|
+
*
|
|
34
|
+
* Note: This will not create a default value on the database level. It will only
|
|
35
|
+
* be used when creating a new record.
|
|
36
|
+
*/
|
|
37
|
+
defaultValue?: DBPrimitive | (() => DBPrimitive);
|
|
38
|
+
/**
|
|
39
|
+
* Update value for the field
|
|
40
|
+
*
|
|
41
|
+
* Note: This will create an onUpdate trigger on the database level for supported adapters.
|
|
42
|
+
* It will be called when updating a record.
|
|
43
|
+
*/
|
|
44
|
+
onUpdate?: () => DBPrimitive;
|
|
45
|
+
/**
|
|
46
|
+
* transform the value before storing it.
|
|
47
|
+
*/
|
|
48
|
+
transform?: {
|
|
49
|
+
input?: (value: DBPrimitive) => DBPrimitive | Promise<DBPrimitive>;
|
|
50
|
+
output?: (value: DBPrimitive) => DBPrimitive | Promise<DBPrimitive>;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Reference to another model.
|
|
54
|
+
*/
|
|
55
|
+
references?: {
|
|
56
|
+
/**
|
|
57
|
+
* The model to reference.
|
|
58
|
+
*/
|
|
59
|
+
model: string;
|
|
60
|
+
/**
|
|
61
|
+
* The field on the referenced model.
|
|
62
|
+
*/
|
|
63
|
+
field: string;
|
|
64
|
+
/**
|
|
65
|
+
* The action to perform when the reference is deleted.
|
|
66
|
+
* @default "cascade"
|
|
67
|
+
*/
|
|
68
|
+
onDelete?: "no action" | "restrict" | "cascade" | "set null" | "set default";
|
|
69
|
+
};
|
|
70
|
+
unique?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* If the field should be a bigint on the database instead of integer.
|
|
73
|
+
*/
|
|
74
|
+
bigint?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* A zod schema to validate the value.
|
|
77
|
+
*/
|
|
78
|
+
validator?: {
|
|
79
|
+
input?: ZodType;
|
|
80
|
+
output?: ZodType;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* The name of the field on the database.
|
|
84
|
+
*/
|
|
85
|
+
fieldName?: string;
|
|
86
|
+
/**
|
|
87
|
+
* If the field should be sortable.
|
|
88
|
+
*
|
|
89
|
+
* applicable only for `text` type.
|
|
90
|
+
* It's useful to mark fields varchar instead of text.
|
|
91
|
+
*/
|
|
92
|
+
sortable?: boolean;
|
|
93
|
+
};
|
|
94
|
+
type DBFieldAttribute<T extends DBFieldType = DBFieldType> = {
|
|
95
|
+
type: T;
|
|
96
|
+
} & DBFieldAttributeConfig;
|
|
97
|
+
type BetterAuthDBSchema = Record<string, {
|
|
98
|
+
/**
|
|
99
|
+
* The name of the table in the database
|
|
100
|
+
*/
|
|
101
|
+
modelName: string;
|
|
102
|
+
/**
|
|
103
|
+
* The fields of the table
|
|
104
|
+
*/
|
|
105
|
+
fields: Record<string, DBFieldAttribute>;
|
|
106
|
+
/**
|
|
107
|
+
* Whether to disable migrations for this table
|
|
108
|
+
* @default false
|
|
109
|
+
*/
|
|
110
|
+
disableMigrations?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* The order of the table
|
|
113
|
+
*/
|
|
114
|
+
order?: number;
|
|
115
|
+
}>;
|
|
116
|
+
interface SecondaryStorage {
|
|
117
|
+
/**
|
|
118
|
+
*
|
|
119
|
+
* @param key - Key to get
|
|
120
|
+
* @returns - Value of the key
|
|
121
|
+
*/
|
|
122
|
+
get: (key: string) => Promise<unknown> | unknown;
|
|
123
|
+
set: (
|
|
124
|
+
/**
|
|
125
|
+
* Key to store
|
|
126
|
+
*/
|
|
127
|
+
key: string,
|
|
128
|
+
/**
|
|
129
|
+
* Value to store
|
|
130
|
+
*/
|
|
131
|
+
value: string,
|
|
132
|
+
/**
|
|
133
|
+
* Time to live in seconds
|
|
134
|
+
*/
|
|
135
|
+
ttl?: number) => Promise<void | null | unknown> | void;
|
|
136
|
+
/**
|
|
137
|
+
*
|
|
138
|
+
* @param key - Key to delete
|
|
139
|
+
*/
|
|
140
|
+
delete: (key: string) => Promise<void | null | string> | void;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export type { BetterAuthDBSchema as B, DBFieldAttribute as D, LiteralUnion as L, Models as M, SecondaryStorage as S, LiteralString as a, DBFieldAttributeConfig as b, DBFieldType as c, DBPrimitive as d };
|