better-auth 0.2.8-beta.8 → 0.2.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/dist/.DS_Store +0 -0
- package/dist/access.js +13 -2
- package/dist/adapters/drizzle.d.ts +1 -1
- package/dist/adapters/drizzle.js +13 -23
- package/dist/adapters/mongodb.d.ts +1 -1
- package/dist/adapters/mongodb.js +3 -2
- package/dist/adapters/prisma.d.ts +1 -1
- package/dist/adapters/prisma.js +3 -280
- package/dist/api.d.ts +1 -1
- package/dist/api.js +407 -269
- package/dist/cli.js +213 -55
- package/dist/client/plugins.d.ts +5 -3
- package/dist/client/plugins.js +49 -34
- package/dist/client.d.ts +3 -1
- package/dist/client.js +34 -32
- package/dist/{index-CKn-Zrry.d.ts → index-C9S3KShG.d.ts} +50 -63
- package/dist/{index-DtRHPoYF.d.ts → index-UOcOxfoL.d.ts} +6 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +501 -372
- package/dist/next-js.d.ts +1 -1
- package/dist/next-js.js +6 -5
- package/dist/node.d.ts +1 -1
- package/dist/node.js +5 -5
- package/dist/plugins.d.ts +8 -5
- package/dist/plugins.js +716 -498
- package/dist/react.d.ts +4 -2
- package/dist/react.js +37 -33
- package/dist/social.js +116 -68
- package/dist/solid-start.d.ts +1 -1
- package/dist/solid-start.js +3 -2
- package/dist/solid.d.ts +2 -1
- package/dist/solid.js +35 -32
- package/dist/svelte-kit.d.ts +1 -1
- package/dist/svelte-kit.js +6 -4
- package/dist/svelte.d.ts +2 -1
- package/dist/svelte.js +33 -32
- package/dist/types.d.ts +2 -2
- package/dist/types.js +0 -1
- package/dist/vue.d.ts +3 -1
- package/dist/vue.js +35 -32
- package/package.json +2 -3
- package/dist/hide-metadata-DEHJp1rk.d.ts +0 -5
- package/dist/utils.d.ts +0 -51
- package/dist/utils.js +0 -426
package/dist/index.js
CHANGED
|
@@ -1,23 +1,18 @@
|
|
|
1
|
-
import { createMiddleware, createMiddlewareCreator, createEndpointCreator, APIError, createRouter } from 'better-call';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import '@noble/ciphers/chacha';
|
|
4
|
-
import '@noble/ciphers/utils';
|
|
5
|
-
import '@noble/ciphers/webcrypto';
|
|
6
|
-
import '@noble/hashes/sha256';
|
|
7
|
-
import { generateCodeVerifier, generateState as generateState$1 } from 'oslo/oauth2';
|
|
8
|
-
import { Facebook, GitHub, Google, Spotify, Twitch, Twitter, OAuth2Tokens } from 'arctic';
|
|
9
|
-
import { createJWT, validateJWT, parseJWT } from 'oslo/jwt';
|
|
10
|
-
import { betterFetch } from '@better-fetch/fetch';
|
|
11
|
-
import { TimeSpan } from 'oslo';
|
|
12
|
-
import { nanoid } from 'nanoid';
|
|
13
|
-
import { createConsola } from 'consola';
|
|
14
|
-
import chalk from 'chalk';
|
|
15
|
-
import { SqliteDialect, MysqlDialect, PostgresDialect, Kysely } from 'kysely';
|
|
16
|
-
import { scrypt } from 'node:crypto';
|
|
17
|
-
import { encodeHex, decodeHex } from 'oslo/encoding';
|
|
18
|
-
import { defu } from 'defu';
|
|
19
|
-
|
|
20
1
|
// src/api/index.ts
|
|
2
|
+
import {
|
|
3
|
+
APIError as APIError5,
|
|
4
|
+
createRouter
|
|
5
|
+
} from "better-call";
|
|
6
|
+
|
|
7
|
+
// src/api/middlewares/csrf.ts
|
|
8
|
+
import { APIError } from "better-call";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
// src/crypto/index.ts
|
|
12
|
+
import { xchacha20poly1305 } from "@noble/ciphers/chacha";
|
|
13
|
+
import { bytesToHex, hexToBytes, utf8ToBytes } from "@noble/ciphers/utils";
|
|
14
|
+
import { managedNonce } from "@noble/ciphers/webcrypto";
|
|
15
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
21
16
|
async function hs256(secretKey, message) {
|
|
22
17
|
const enc = new TextEncoder();
|
|
23
18
|
const algorithm = { name: "HMAC", hash: "SHA-256" };
|
|
@@ -35,6 +30,13 @@ async function hs256(secretKey, message) {
|
|
|
35
30
|
);
|
|
36
31
|
return btoa(String.fromCharCode(...new Uint8Array(signature)));
|
|
37
32
|
}
|
|
33
|
+
|
|
34
|
+
// src/api/call.ts
|
|
35
|
+
import {
|
|
36
|
+
createEndpointCreator,
|
|
37
|
+
createMiddleware,
|
|
38
|
+
createMiddlewareCreator
|
|
39
|
+
} from "better-call";
|
|
38
40
|
var optionsMiddleware = createMiddleware(async () => {
|
|
39
41
|
return {};
|
|
40
42
|
});
|
|
@@ -94,6 +96,30 @@ var csrfMiddleware = createAuthMiddleware(
|
|
|
94
96
|
}
|
|
95
97
|
);
|
|
96
98
|
|
|
99
|
+
// src/api/routes/sign-in.ts
|
|
100
|
+
import { APIError as APIError3 } from "better-call";
|
|
101
|
+
import { generateCodeVerifier } from "oslo/oauth2";
|
|
102
|
+
import { z as z4 } from "zod";
|
|
103
|
+
|
|
104
|
+
// src/social-providers/apple.ts
|
|
105
|
+
import "arctic";
|
|
106
|
+
import { parseJWT } from "oslo/jwt";
|
|
107
|
+
import "@better-fetch/fetch";
|
|
108
|
+
|
|
109
|
+
// src/error/better-auth-error.ts
|
|
110
|
+
var BetterAuthError = class extends Error {
|
|
111
|
+
constructor(message, cause) {
|
|
112
|
+
super(message);
|
|
113
|
+
this.name = "BetterAuthError";
|
|
114
|
+
this.message = message;
|
|
115
|
+
this.cause = cause;
|
|
116
|
+
this.stack = "";
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// src/social-providers/utils.ts
|
|
121
|
+
import { OAuth2Tokens } from "arctic";
|
|
122
|
+
|
|
97
123
|
// src/utils/base-url.ts
|
|
98
124
|
function checkHasPath(url) {
|
|
99
125
|
try {
|
|
@@ -127,232 +153,9 @@ function getBaseURL(url, path) {
|
|
|
127
153
|
}
|
|
128
154
|
return void 0;
|
|
129
155
|
}
|
|
130
|
-
function getCookies(options) {
|
|
131
|
-
const secure = !!options.advanced?.useSecureCookies || process.env.NODE_ENV !== "development" && process.env.NODE_ENV !== "test";
|
|
132
|
-
const secureCookiePrefix = secure ? "__Secure-" : "";
|
|
133
|
-
const cookiePrefix = "better-auth";
|
|
134
|
-
const sessionMaxAge = new TimeSpan(7, "d").seconds();
|
|
135
|
-
return {
|
|
136
|
-
sessionToken: {
|
|
137
|
-
name: `${secureCookiePrefix}${cookiePrefix}.session_token`,
|
|
138
|
-
options: {
|
|
139
|
-
httpOnly: true,
|
|
140
|
-
sameSite: "lax",
|
|
141
|
-
path: "/",
|
|
142
|
-
secure: !!secureCookiePrefix,
|
|
143
|
-
maxAge: sessionMaxAge
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
csrfToken: {
|
|
147
|
-
name: `${secureCookiePrefix ? "__Host-" : ""}${cookiePrefix}.csrf_token`,
|
|
148
|
-
options: {
|
|
149
|
-
httpOnly: true,
|
|
150
|
-
sameSite: "lax",
|
|
151
|
-
path: "/",
|
|
152
|
-
secure: !!secureCookiePrefix,
|
|
153
|
-
maxAge: 60 * 60 * 24 * 7
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
state: {
|
|
157
|
-
name: `${secureCookiePrefix}${cookiePrefix}.state`,
|
|
158
|
-
options: {
|
|
159
|
-
httpOnly: true,
|
|
160
|
-
sameSite: "lax",
|
|
161
|
-
path: "/",
|
|
162
|
-
secure: !!secureCookiePrefix,
|
|
163
|
-
maxAge: 60 * 15
|
|
164
|
-
// 15 minutes in seconds
|
|
165
|
-
}
|
|
166
|
-
},
|
|
167
|
-
pkCodeVerifier: {
|
|
168
|
-
name: `${secureCookiePrefix}${cookiePrefix}.pk_code_verifier`,
|
|
169
|
-
options: {
|
|
170
|
-
httpOnly: true,
|
|
171
|
-
sameSite: "lax",
|
|
172
|
-
path: "/",
|
|
173
|
-
secure: !!secureCookiePrefix,
|
|
174
|
-
maxAge: 60 * 15
|
|
175
|
-
// 15 minutes in seconds
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
dontRememberToken: {
|
|
179
|
-
name: `${secureCookiePrefix}${cookiePrefix}.dont_remember`,
|
|
180
|
-
options: {
|
|
181
|
-
httpOnly: true,
|
|
182
|
-
sameSite: "lax",
|
|
183
|
-
path: "/",
|
|
184
|
-
secure: !!secureCookiePrefix
|
|
185
|
-
//no max age so it expires when the browser closes
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
|
-
nonce: {
|
|
189
|
-
name: `${secureCookiePrefix}${cookiePrefix}.nonce`,
|
|
190
|
-
options: {
|
|
191
|
-
httpOnly: true,
|
|
192
|
-
sameSite: "lax",
|
|
193
|
-
path: "/",
|
|
194
|
-
secure: !!secureCookiePrefix,
|
|
195
|
-
maxAge: 60 * 15
|
|
196
|
-
// 15 minutes in seconds
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
function createCookieGetter(options) {
|
|
202
|
-
const secure = !!options.advanced?.useSecureCookies || process.env.NODE_ENV === "production";
|
|
203
|
-
const secureCookiePrefix = secure ? "__Secure-" : "";
|
|
204
|
-
const cookiePrefix = "better-auth";
|
|
205
|
-
function getCookie(cookieName, options2) {
|
|
206
|
-
return {
|
|
207
|
-
name: process.env.NODE_ENV === "production" ? `${secureCookiePrefix}${cookiePrefix}.${cookieName}` : `${cookiePrefix}.${cookieName}`,
|
|
208
|
-
options: {
|
|
209
|
-
secure: !!secureCookiePrefix,
|
|
210
|
-
sameSite: "lax",
|
|
211
|
-
path: "/",
|
|
212
|
-
maxAge: 60 * 15,
|
|
213
|
-
// 15 minutes in seconds
|
|
214
|
-
...options2
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
return getCookie;
|
|
219
|
-
}
|
|
220
|
-
async function setSessionCookie(ctx, sessionToken, dontRememberMe, overrides) {
|
|
221
|
-
const options = ctx.context.authCookies.sessionToken.options;
|
|
222
|
-
options.maxAge = dontRememberMe ? void 0 : options.maxAge;
|
|
223
|
-
await ctx.setSignedCookie(
|
|
224
|
-
ctx.context.authCookies.sessionToken.name,
|
|
225
|
-
sessionToken,
|
|
226
|
-
ctx.context.secret,
|
|
227
|
-
options
|
|
228
|
-
);
|
|
229
|
-
if (dontRememberMe) {
|
|
230
|
-
await ctx.setSignedCookie(
|
|
231
|
-
ctx.context.authCookies.dontRememberToken.name,
|
|
232
|
-
"true",
|
|
233
|
-
ctx.context.secret,
|
|
234
|
-
ctx.context.authCookies.dontRememberToken.options
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
function deleteSessionCookie(ctx) {
|
|
239
|
-
ctx.setCookie(ctx.context.authCookies.sessionToken.name, "", {
|
|
240
|
-
maxAge: 0
|
|
241
|
-
});
|
|
242
|
-
ctx.setCookie(ctx.context.authCookies.dontRememberToken.name, "", {
|
|
243
|
-
maxAge: 0
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
// src/utils/date.ts
|
|
248
|
-
var getDate = (span, unit = "ms") => {
|
|
249
|
-
const date = /* @__PURE__ */ new Date();
|
|
250
|
-
return new Date(date.getTime() + (unit === "sec" ? span * 1e3 : span));
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
// src/utils/get-request-ip.ts
|
|
254
|
-
function getIp(req) {
|
|
255
|
-
const testIP = "127.0.0.1";
|
|
256
|
-
if (process.env.NODE_ENV === "test") {
|
|
257
|
-
return testIP;
|
|
258
|
-
}
|
|
259
|
-
const headers = [
|
|
260
|
-
"x-client-ip",
|
|
261
|
-
"x-forwarded-for",
|
|
262
|
-
"cf-connecting-ip",
|
|
263
|
-
"fastly-client-ip",
|
|
264
|
-
"x-real-ip",
|
|
265
|
-
"x-cluster-client-ip",
|
|
266
|
-
"x-forwarded",
|
|
267
|
-
"forwarded-for",
|
|
268
|
-
"forwarded"
|
|
269
|
-
];
|
|
270
|
-
for (const header of headers) {
|
|
271
|
-
const value = req.headers.get(header);
|
|
272
|
-
if (typeof value === "string") {
|
|
273
|
-
const ip = value.split(",")[0].trim();
|
|
274
|
-
if (ip) return ip;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return null;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// src/utils/hide-metadata.ts
|
|
281
|
-
var HIDE_METADATA = {
|
|
282
|
-
isAction: false
|
|
283
|
-
};
|
|
284
|
-
var generateId = (size) => {
|
|
285
|
-
return nanoid(size);
|
|
286
|
-
};
|
|
287
|
-
var consola = createConsola({
|
|
288
|
-
formatOptions: {
|
|
289
|
-
date: false,
|
|
290
|
-
colors: true,
|
|
291
|
-
compact: true
|
|
292
|
-
},
|
|
293
|
-
defaults: {
|
|
294
|
-
tag: "Better Auth"
|
|
295
|
-
}
|
|
296
|
-
});
|
|
297
|
-
var createLogger = (options) => {
|
|
298
|
-
return {
|
|
299
|
-
log: (...args) => {
|
|
300
|
-
!options?.disabled && consola.log("", ...args);
|
|
301
|
-
},
|
|
302
|
-
error: (...args) => {
|
|
303
|
-
!options?.disabled && consola.error("", ...args);
|
|
304
|
-
},
|
|
305
|
-
warn: (...args) => {
|
|
306
|
-
!options?.disabled && consola.warn("", ...args);
|
|
307
|
-
},
|
|
308
|
-
info: (...args) => {
|
|
309
|
-
!options?.disabled && consola.info("", ...args);
|
|
310
|
-
},
|
|
311
|
-
debug: (...args) => {
|
|
312
|
-
!options?.disabled && consola.debug("", ...args);
|
|
313
|
-
},
|
|
314
|
-
box: (...args) => {
|
|
315
|
-
!options?.disabled && consola.box("", ...args);
|
|
316
|
-
},
|
|
317
|
-
success: (...args) => {
|
|
318
|
-
!options?.disabled && consola.success("", ...args);
|
|
319
|
-
},
|
|
320
|
-
break: (...args) => {
|
|
321
|
-
!options?.disabled && console.log("\n");
|
|
322
|
-
}
|
|
323
|
-
};
|
|
324
|
-
};
|
|
325
|
-
var logger = createLogger();
|
|
326
|
-
function generateState(callbackURL, currentURL, dontRememberMe) {
|
|
327
|
-
const code = generateState$1();
|
|
328
|
-
const state = JSON.stringify({
|
|
329
|
-
code,
|
|
330
|
-
callbackURL,
|
|
331
|
-
currentURL,
|
|
332
|
-
dontRememberMe
|
|
333
|
-
});
|
|
334
|
-
return { state, code };
|
|
335
|
-
}
|
|
336
|
-
function parseState(state) {
|
|
337
|
-
const data = z.object({
|
|
338
|
-
code: z.string(),
|
|
339
|
-
callbackURL: z.string().optional(),
|
|
340
|
-
currentURL: z.string().optional(),
|
|
341
|
-
dontRememberMe: z.boolean().optional()
|
|
342
|
-
}).safeParse(JSON.parse(state));
|
|
343
|
-
return data;
|
|
344
|
-
}
|
|
345
156
|
|
|
346
|
-
// src/
|
|
347
|
-
|
|
348
|
-
constructor(message, cause) {
|
|
349
|
-
super(message);
|
|
350
|
-
this.name = "BetterAuthError";
|
|
351
|
-
this.message = message;
|
|
352
|
-
this.cause = cause;
|
|
353
|
-
this.stack = "";
|
|
354
|
-
}
|
|
355
|
-
};
|
|
157
|
+
// src/social-providers/utils.ts
|
|
158
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
356
159
|
function getRedirectURI(providerId, redirectURI) {
|
|
357
160
|
return redirectURI || `${getBaseURL()}/callback/${providerId}`;
|
|
358
161
|
}
|
|
@@ -424,6 +227,9 @@ var apple = (options) => {
|
|
|
424
227
|
}
|
|
425
228
|
};
|
|
426
229
|
};
|
|
230
|
+
|
|
231
|
+
// src/social-providers/discord.ts
|
|
232
|
+
import { betterFetch as betterFetch3 } from "@better-fetch/fetch";
|
|
427
233
|
var discord = (options) => {
|
|
428
234
|
return {
|
|
429
235
|
id: "discord",
|
|
@@ -447,7 +253,7 @@ var discord = (options) => {
|
|
|
447
253
|
});
|
|
448
254
|
},
|
|
449
255
|
async getUserInfo(token) {
|
|
450
|
-
const { data: profile, error: error2 } = await
|
|
256
|
+
const { data: profile, error: error2 } = await betterFetch3(
|
|
451
257
|
"https://discord.com/api/users/@me",
|
|
452
258
|
{
|
|
453
259
|
headers: {
|
|
@@ -478,6 +284,10 @@ var discord = (options) => {
|
|
|
478
284
|
}
|
|
479
285
|
};
|
|
480
286
|
};
|
|
287
|
+
|
|
288
|
+
// src/social-providers/facebook.ts
|
|
289
|
+
import { betterFetch as betterFetch4 } from "@better-fetch/fetch";
|
|
290
|
+
import { Facebook } from "arctic";
|
|
481
291
|
var facebook = (options) => {
|
|
482
292
|
const facebookArctic = new Facebook(
|
|
483
293
|
options.clientId,
|
|
@@ -501,7 +311,7 @@ var facebook = (options) => {
|
|
|
501
311
|
});
|
|
502
312
|
},
|
|
503
313
|
async getUserInfo(token) {
|
|
504
|
-
const { data: profile, error: error2 } = await
|
|
314
|
+
const { data: profile, error: error2 } = await betterFetch4(
|
|
505
315
|
"https://graph.facebook.com/me",
|
|
506
316
|
{
|
|
507
317
|
auth: {
|
|
@@ -525,6 +335,10 @@ var facebook = (options) => {
|
|
|
525
335
|
}
|
|
526
336
|
};
|
|
527
337
|
};
|
|
338
|
+
|
|
339
|
+
// src/social-providers/github.ts
|
|
340
|
+
import { betterFetch as betterFetch5 } from "@better-fetch/fetch";
|
|
341
|
+
import { GitHub } from "arctic";
|
|
528
342
|
var github = ({
|
|
529
343
|
clientId,
|
|
530
344
|
clientSecret,
|
|
@@ -546,7 +360,7 @@ var github = ({
|
|
|
546
360
|
return await githubArctic.validateAuthorizationCode(state);
|
|
547
361
|
},
|
|
548
362
|
async getUserInfo(token) {
|
|
549
|
-
const { data: profile, error: error2 } = await
|
|
363
|
+
const { data: profile, error: error2 } = await betterFetch5(
|
|
550
364
|
"https://api.github.com/user",
|
|
551
365
|
{
|
|
552
366
|
auth: {
|
|
@@ -560,7 +374,7 @@ var github = ({
|
|
|
560
374
|
}
|
|
561
375
|
let emailVerified = false;
|
|
562
376
|
if (!profile.email) {
|
|
563
|
-
const { data, error: error3 } = await
|
|
377
|
+
const { data, error: error3 } = await betterFetch5("https://api.github.com/user/emails", {
|
|
564
378
|
auth: {
|
|
565
379
|
type: "Bearer",
|
|
566
380
|
token: token.accessToken()
|
|
@@ -586,6 +400,54 @@ var github = ({
|
|
|
586
400
|
}
|
|
587
401
|
};
|
|
588
402
|
};
|
|
403
|
+
|
|
404
|
+
// src/social-providers/google.ts
|
|
405
|
+
import { Google } from "arctic";
|
|
406
|
+
import { parseJWT as parseJWT2 } from "oslo/jwt";
|
|
407
|
+
|
|
408
|
+
// src/utils/logger.ts
|
|
409
|
+
import { createConsola } from "consola";
|
|
410
|
+
var consola = createConsola({
|
|
411
|
+
formatOptions: {
|
|
412
|
+
date: false,
|
|
413
|
+
colors: true,
|
|
414
|
+
compact: true
|
|
415
|
+
},
|
|
416
|
+
defaults: {
|
|
417
|
+
tag: "Better Auth"
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
var createLogger = (options) => {
|
|
421
|
+
return {
|
|
422
|
+
log: (...args) => {
|
|
423
|
+
!options?.disabled && consola.log("", ...args);
|
|
424
|
+
},
|
|
425
|
+
error: (...args) => {
|
|
426
|
+
!options?.disabled && consola.error("", ...args);
|
|
427
|
+
},
|
|
428
|
+
warn: (...args) => {
|
|
429
|
+
!options?.disabled && consola.warn("", ...args);
|
|
430
|
+
},
|
|
431
|
+
info: (...args) => {
|
|
432
|
+
!options?.disabled && consola.info("", ...args);
|
|
433
|
+
},
|
|
434
|
+
debug: (...args) => {
|
|
435
|
+
!options?.disabled && consola.debug("", ...args);
|
|
436
|
+
},
|
|
437
|
+
box: (...args) => {
|
|
438
|
+
!options?.disabled && consola.box("", ...args);
|
|
439
|
+
},
|
|
440
|
+
success: (...args) => {
|
|
441
|
+
!options?.disabled && consola.success("", ...args);
|
|
442
|
+
},
|
|
443
|
+
break: (...args) => {
|
|
444
|
+
!options?.disabled && console.log("\n");
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
};
|
|
448
|
+
var logger = createLogger();
|
|
449
|
+
|
|
450
|
+
// src/social-providers/google.ts
|
|
589
451
|
var google = (options) => {
|
|
590
452
|
const googleArctic = new Google(
|
|
591
453
|
options.clientId,
|
|
@@ -626,7 +488,7 @@ var google = (options) => {
|
|
|
626
488
|
if (!token.idToken) {
|
|
627
489
|
return null;
|
|
628
490
|
}
|
|
629
|
-
const user =
|
|
491
|
+
const user = parseJWT2(token.idToken())?.payload;
|
|
630
492
|
return {
|
|
631
493
|
user: {
|
|
632
494
|
id: user.sub,
|
|
@@ -640,6 +502,10 @@ var google = (options) => {
|
|
|
640
502
|
}
|
|
641
503
|
};
|
|
642
504
|
};
|
|
505
|
+
|
|
506
|
+
// src/social-providers/spotify.ts
|
|
507
|
+
import { betterFetch as betterFetch6 } from "@better-fetch/fetch";
|
|
508
|
+
import { Spotify } from "arctic";
|
|
643
509
|
var spotify = (options) => {
|
|
644
510
|
const spotifyArctic = new Spotify(
|
|
645
511
|
options.clientId,
|
|
@@ -663,7 +529,7 @@ var spotify = (options) => {
|
|
|
663
529
|
});
|
|
664
530
|
},
|
|
665
531
|
async getUserInfo(token) {
|
|
666
|
-
const { data: profile, error: error2 } = await
|
|
532
|
+
const { data: profile, error: error2 } = await betterFetch6(
|
|
667
533
|
"https://api.spotify.com/v1/me",
|
|
668
534
|
{
|
|
669
535
|
method: "GET",
|
|
@@ -688,6 +554,10 @@ var spotify = (options) => {
|
|
|
688
554
|
}
|
|
689
555
|
};
|
|
690
556
|
};
|
|
557
|
+
|
|
558
|
+
// src/social-providers/twitch.ts
|
|
559
|
+
import { betterFetch as betterFetch7 } from "@better-fetch/fetch";
|
|
560
|
+
import { Twitch } from "arctic";
|
|
691
561
|
var twitch = (options) => {
|
|
692
562
|
const twitchArctic = new Twitch(
|
|
693
563
|
options.clientId,
|
|
@@ -710,7 +580,7 @@ var twitch = (options) => {
|
|
|
710
580
|
});
|
|
711
581
|
},
|
|
712
582
|
async getUserInfo(token) {
|
|
713
|
-
const { data: profile, error: error2 } = await
|
|
583
|
+
const { data: profile, error: error2 } = await betterFetch7(
|
|
714
584
|
"https://api.twitch.tv/helix/users",
|
|
715
585
|
{
|
|
716
586
|
method: "GET",
|
|
@@ -735,6 +605,10 @@ var twitch = (options) => {
|
|
|
735
605
|
}
|
|
736
606
|
};
|
|
737
607
|
};
|
|
608
|
+
|
|
609
|
+
// src/social-providers/twitter.ts
|
|
610
|
+
import { betterFetch as betterFetch8 } from "@better-fetch/fetch";
|
|
611
|
+
import { Twitter } from "arctic";
|
|
738
612
|
var twitter = (options) => {
|
|
739
613
|
const twitterArctic = new Twitter(
|
|
740
614
|
options.clientId,
|
|
@@ -762,7 +636,7 @@ var twitter = (options) => {
|
|
|
762
636
|
});
|
|
763
637
|
},
|
|
764
638
|
async getUserInfo(token) {
|
|
765
|
-
const { data: profile, error: error2 } = await
|
|
639
|
+
const { data: profile, error: error2 } = await betterFetch8(
|
|
766
640
|
"https://api.x.com/2/users/me?user.fields=profile_image_url",
|
|
767
641
|
{
|
|
768
642
|
method: "GET",
|
|
@@ -791,6 +665,9 @@ var twitter = (options) => {
|
|
|
791
665
|
};
|
|
792
666
|
};
|
|
793
667
|
|
|
668
|
+
// src/types/provider.ts
|
|
669
|
+
import "arctic";
|
|
670
|
+
|
|
794
671
|
// src/social-providers/index.ts
|
|
795
672
|
var oAuthProviders = {
|
|
796
673
|
apple,
|
|
@@ -803,6 +680,189 @@ var oAuthProviders = {
|
|
|
803
680
|
twitter
|
|
804
681
|
};
|
|
805
682
|
var oAuthProviderList = Object.keys(oAuthProviders);
|
|
683
|
+
|
|
684
|
+
// src/utils/state.ts
|
|
685
|
+
import { generateState as generateStateOAuth } from "oslo/oauth2";
|
|
686
|
+
import { z as z2 } from "zod";
|
|
687
|
+
function generateState(callbackURL, currentURL, dontRememberMe) {
|
|
688
|
+
const code = generateStateOAuth();
|
|
689
|
+
const state = JSON.stringify({
|
|
690
|
+
code,
|
|
691
|
+
callbackURL,
|
|
692
|
+
currentURL,
|
|
693
|
+
dontRememberMe
|
|
694
|
+
});
|
|
695
|
+
return { state, code };
|
|
696
|
+
}
|
|
697
|
+
function parseState(state) {
|
|
698
|
+
const data = z2.object({
|
|
699
|
+
code: z2.string(),
|
|
700
|
+
callbackURL: z2.string().optional(),
|
|
701
|
+
currentURL: z2.string().optional(),
|
|
702
|
+
dontRememberMe: z2.boolean().optional()
|
|
703
|
+
}).safeParse(JSON.parse(state));
|
|
704
|
+
return data;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
// src/api/routes/session.ts
|
|
708
|
+
import { APIError as APIError2 } from "better-call";
|
|
709
|
+
|
|
710
|
+
// src/utils/date.ts
|
|
711
|
+
var getDate = (span, unit = "ms") => {
|
|
712
|
+
const date = /* @__PURE__ */ new Date();
|
|
713
|
+
return new Date(date.getTime() + (unit === "sec" ? span * 1e3 : span));
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
// src/utils/cookies.ts
|
|
717
|
+
import { TimeSpan } from "oslo";
|
|
718
|
+
function getCookies(options) {
|
|
719
|
+
const secure = !!options.advanced?.useSecureCookies || process.env.NODE_ENV !== "development" && process.env.NODE_ENV !== "test";
|
|
720
|
+
const secureCookiePrefix = secure ? "__Secure-" : "";
|
|
721
|
+
const cookiePrefix = "better-auth";
|
|
722
|
+
const sessionMaxAge = new TimeSpan(7, "d").seconds();
|
|
723
|
+
return {
|
|
724
|
+
sessionToken: {
|
|
725
|
+
name: `${secureCookiePrefix}${cookiePrefix}.session_token`,
|
|
726
|
+
options: {
|
|
727
|
+
httpOnly: true,
|
|
728
|
+
sameSite: "lax",
|
|
729
|
+
path: "/",
|
|
730
|
+
secure: !!secureCookiePrefix,
|
|
731
|
+
maxAge: sessionMaxAge
|
|
732
|
+
}
|
|
733
|
+
},
|
|
734
|
+
csrfToken: {
|
|
735
|
+
name: `${secureCookiePrefix ? "__Host-" : ""}${cookiePrefix}.csrf_token`,
|
|
736
|
+
options: {
|
|
737
|
+
httpOnly: true,
|
|
738
|
+
sameSite: "lax",
|
|
739
|
+
path: "/",
|
|
740
|
+
secure: !!secureCookiePrefix,
|
|
741
|
+
maxAge: 60 * 60 * 24 * 7
|
|
742
|
+
}
|
|
743
|
+
},
|
|
744
|
+
state: {
|
|
745
|
+
name: `${secureCookiePrefix}${cookiePrefix}.state`,
|
|
746
|
+
options: {
|
|
747
|
+
httpOnly: true,
|
|
748
|
+
sameSite: "lax",
|
|
749
|
+
path: "/",
|
|
750
|
+
secure: !!secureCookiePrefix,
|
|
751
|
+
maxAge: 60 * 15
|
|
752
|
+
// 15 minutes in seconds
|
|
753
|
+
}
|
|
754
|
+
},
|
|
755
|
+
pkCodeVerifier: {
|
|
756
|
+
name: `${secureCookiePrefix}${cookiePrefix}.pk_code_verifier`,
|
|
757
|
+
options: {
|
|
758
|
+
httpOnly: true,
|
|
759
|
+
sameSite: "lax",
|
|
760
|
+
path: "/",
|
|
761
|
+
secure: !!secureCookiePrefix,
|
|
762
|
+
maxAge: 60 * 15
|
|
763
|
+
// 15 minutes in seconds
|
|
764
|
+
}
|
|
765
|
+
},
|
|
766
|
+
dontRememberToken: {
|
|
767
|
+
name: `${secureCookiePrefix}${cookiePrefix}.dont_remember`,
|
|
768
|
+
options: {
|
|
769
|
+
httpOnly: true,
|
|
770
|
+
sameSite: "lax",
|
|
771
|
+
path: "/",
|
|
772
|
+
secure: !!secureCookiePrefix
|
|
773
|
+
//no max age so it expires when the browser closes
|
|
774
|
+
}
|
|
775
|
+
},
|
|
776
|
+
nonce: {
|
|
777
|
+
name: `${secureCookiePrefix}${cookiePrefix}.nonce`,
|
|
778
|
+
options: {
|
|
779
|
+
httpOnly: true,
|
|
780
|
+
sameSite: "lax",
|
|
781
|
+
path: "/",
|
|
782
|
+
secure: !!secureCookiePrefix,
|
|
783
|
+
maxAge: 60 * 15
|
|
784
|
+
// 15 minutes in seconds
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
function createCookieGetter(options) {
|
|
790
|
+
const secure = !!options.advanced?.useSecureCookies || process.env.NODE_ENV === "production";
|
|
791
|
+
const secureCookiePrefix = secure ? "__Secure-" : "";
|
|
792
|
+
const cookiePrefix = "better-auth";
|
|
793
|
+
function getCookie(cookieName, options2) {
|
|
794
|
+
return {
|
|
795
|
+
name: process.env.NODE_ENV === "production" ? `${secureCookiePrefix}${cookiePrefix}.${cookieName}` : `${cookiePrefix}.${cookieName}`,
|
|
796
|
+
options: {
|
|
797
|
+
secure: !!secureCookiePrefix,
|
|
798
|
+
sameSite: "lax",
|
|
799
|
+
path: "/",
|
|
800
|
+
maxAge: 60 * 15,
|
|
801
|
+
// 15 minutes in seconds
|
|
802
|
+
...options2
|
|
803
|
+
}
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
return getCookie;
|
|
807
|
+
}
|
|
808
|
+
async function setSessionCookie(ctx, sessionToken, dontRememberMe, overrides) {
|
|
809
|
+
const options = ctx.context.authCookies.sessionToken.options;
|
|
810
|
+
options.maxAge = dontRememberMe ? void 0 : options.maxAge;
|
|
811
|
+
await ctx.setSignedCookie(
|
|
812
|
+
ctx.context.authCookies.sessionToken.name,
|
|
813
|
+
sessionToken,
|
|
814
|
+
ctx.context.secret,
|
|
815
|
+
options
|
|
816
|
+
);
|
|
817
|
+
if (dontRememberMe) {
|
|
818
|
+
await ctx.setSignedCookie(
|
|
819
|
+
ctx.context.authCookies.dontRememberToken.name,
|
|
820
|
+
"true",
|
|
821
|
+
ctx.context.secret,
|
|
822
|
+
ctx.context.authCookies.dontRememberToken.options
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
function deleteSessionCookie(ctx) {
|
|
827
|
+
ctx.setCookie(ctx.context.authCookies.sessionToken.name, "", {
|
|
828
|
+
maxAge: 0
|
|
829
|
+
});
|
|
830
|
+
ctx.setCookie(ctx.context.authCookies.dontRememberToken.name, "", {
|
|
831
|
+
maxAge: 0
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// src/api/routes/session.ts
|
|
836
|
+
import { z as z3 } from "zod";
|
|
837
|
+
|
|
838
|
+
// src/utils/get-request-ip.ts
|
|
839
|
+
function getIp(req) {
|
|
840
|
+
const testIP = "127.0.0.1";
|
|
841
|
+
if (process.env.NODE_ENV === "test") {
|
|
842
|
+
return testIP;
|
|
843
|
+
}
|
|
844
|
+
const headers = [
|
|
845
|
+
"x-client-ip",
|
|
846
|
+
"x-forwarded-for",
|
|
847
|
+
"cf-connecting-ip",
|
|
848
|
+
"fastly-client-ip",
|
|
849
|
+
"x-real-ip",
|
|
850
|
+
"x-cluster-client-ip",
|
|
851
|
+
"x-forwarded",
|
|
852
|
+
"forwarded-for",
|
|
853
|
+
"forwarded"
|
|
854
|
+
];
|
|
855
|
+
for (const header of headers) {
|
|
856
|
+
const value = req.headers.get(header);
|
|
857
|
+
if (typeof value === "string") {
|
|
858
|
+
const ip = value.split(",")[0].trim();
|
|
859
|
+
if (ip) return ip;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
return null;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
// src/api/routes/session.ts
|
|
806
866
|
function getRequestUniqueKey(ctx, token) {
|
|
807
867
|
if (!ctx.request) {
|
|
808
868
|
return "";
|
|
@@ -895,7 +955,7 @@ var getSessionFromCtx = async (ctx) => {
|
|
|
895
955
|
var sessionMiddleware = createAuthMiddleware(async (ctx) => {
|
|
896
956
|
const session = await getSessionFromCtx(ctx);
|
|
897
957
|
if (!session?.session) {
|
|
898
|
-
throw new
|
|
958
|
+
throw new APIError2("UNAUTHORIZED");
|
|
899
959
|
}
|
|
900
960
|
return {
|
|
901
961
|
session
|
|
@@ -930,8 +990,8 @@ var revokeSession = createAuthEndpoint(
|
|
|
930
990
|
"/user/revoke-session",
|
|
931
991
|
{
|
|
932
992
|
method: "POST",
|
|
933
|
-
body:
|
|
934
|
-
id:
|
|
993
|
+
body: z3.object({
|
|
994
|
+
id: z3.string()
|
|
935
995
|
}),
|
|
936
996
|
use: [sessionMiddleware],
|
|
937
997
|
requireHeaders: true
|
|
@@ -984,26 +1044,26 @@ var signInOAuth = createAuthEndpoint(
|
|
|
984
1044
|
{
|
|
985
1045
|
method: "POST",
|
|
986
1046
|
requireHeaders: true,
|
|
987
|
-
query:
|
|
1047
|
+
query: z4.object({
|
|
988
1048
|
/**
|
|
989
1049
|
* Redirect to the current URL after the
|
|
990
1050
|
* user has signed in.
|
|
991
1051
|
*/
|
|
992
|
-
currentURL:
|
|
1052
|
+
currentURL: z4.string().optional()
|
|
993
1053
|
}).optional(),
|
|
994
|
-
body:
|
|
1054
|
+
body: z4.object({
|
|
995
1055
|
/**
|
|
996
1056
|
* Callback URL to redirect to after the user has signed in.
|
|
997
1057
|
*/
|
|
998
|
-
callbackURL:
|
|
1058
|
+
callbackURL: z4.string().optional(),
|
|
999
1059
|
/**
|
|
1000
1060
|
* OAuth2 provider to use`
|
|
1001
1061
|
*/
|
|
1002
|
-
provider:
|
|
1062
|
+
provider: z4.enum(oAuthProviderList),
|
|
1003
1063
|
/**
|
|
1004
1064
|
* If this is true the session will only be valid for the current browser session
|
|
1005
1065
|
*/
|
|
1006
|
-
dontRememberMe:
|
|
1066
|
+
dontRememberMe: z4.boolean().default(false).optional()
|
|
1007
1067
|
})
|
|
1008
1068
|
},
|
|
1009
1069
|
async (c) => {
|
|
@@ -1017,7 +1077,7 @@ var signInOAuth = createAuthEndpoint(
|
|
|
1017
1077
|
provider: c.body.provider
|
|
1018
1078
|
}
|
|
1019
1079
|
);
|
|
1020
|
-
throw new
|
|
1080
|
+
throw new APIError3("NOT_FOUND", {
|
|
1021
1081
|
message: "Provider not found"
|
|
1022
1082
|
});
|
|
1023
1083
|
}
|
|
@@ -1057,7 +1117,7 @@ var signInOAuth = createAuthEndpoint(
|
|
|
1057
1117
|
redirect: true
|
|
1058
1118
|
};
|
|
1059
1119
|
} catch (e) {
|
|
1060
|
-
throw new
|
|
1120
|
+
throw new APIError3("INTERNAL_SERVER_ERROR");
|
|
1061
1121
|
}
|
|
1062
1122
|
}
|
|
1063
1123
|
);
|
|
@@ -1065,15 +1125,15 @@ var signInEmail = createAuthEndpoint(
|
|
|
1065
1125
|
"/sign-in/email",
|
|
1066
1126
|
{
|
|
1067
1127
|
method: "POST",
|
|
1068
|
-
body:
|
|
1069
|
-
email:
|
|
1070
|
-
password:
|
|
1071
|
-
callbackURL:
|
|
1128
|
+
body: z4.object({
|
|
1129
|
+
email: z4.string().email(),
|
|
1130
|
+
password: z4.string(),
|
|
1131
|
+
callbackURL: z4.string().optional(),
|
|
1072
1132
|
/**
|
|
1073
1133
|
* If this is true the session will only be valid for the current browser session
|
|
1074
1134
|
* @default false
|
|
1075
1135
|
*/
|
|
1076
|
-
dontRememberMe:
|
|
1136
|
+
dontRememberMe: z4.boolean().default(false).optional()
|
|
1077
1137
|
})
|
|
1078
1138
|
},
|
|
1079
1139
|
async (ctx) => {
|
|
@@ -1081,7 +1141,7 @@ var signInEmail = createAuthEndpoint(
|
|
|
1081
1141
|
ctx.context.logger.error(
|
|
1082
1142
|
"Email and password is not enabled. Make sure to enable it in the options on you `auth.ts` file. Check `https://better-auth.com/docs/authentication/email-password` for more!"
|
|
1083
1143
|
);
|
|
1084
|
-
throw new
|
|
1144
|
+
throw new APIError3("BAD_REQUEST", {
|
|
1085
1145
|
message: "Email and password is not enabled"
|
|
1086
1146
|
});
|
|
1087
1147
|
}
|
|
@@ -1092,9 +1152,9 @@ var signInEmail = createAuthEndpoint(
|
|
|
1092
1152
|
);
|
|
1093
1153
|
}
|
|
1094
1154
|
const { email, password } = ctx.body;
|
|
1095
|
-
const checkEmail =
|
|
1155
|
+
const checkEmail = z4.string().email().safeParse(email);
|
|
1096
1156
|
if (!checkEmail.success) {
|
|
1097
|
-
throw new
|
|
1157
|
+
throw new APIError3("BAD_REQUEST", {
|
|
1098
1158
|
message: "Invalid email"
|
|
1099
1159
|
});
|
|
1100
1160
|
}
|
|
@@ -1102,7 +1162,7 @@ var signInEmail = createAuthEndpoint(
|
|
|
1102
1162
|
if (!user) {
|
|
1103
1163
|
await ctx.context.password.hash(password);
|
|
1104
1164
|
ctx.context.logger.error("User not found", { email });
|
|
1105
|
-
throw new
|
|
1165
|
+
throw new APIError3("UNAUTHORIZED", {
|
|
1106
1166
|
message: "Invalid email or password"
|
|
1107
1167
|
});
|
|
1108
1168
|
}
|
|
@@ -1111,14 +1171,14 @@ var signInEmail = createAuthEndpoint(
|
|
|
1111
1171
|
);
|
|
1112
1172
|
if (!credentialAccount) {
|
|
1113
1173
|
ctx.context.logger.error("Credential account not found", { email });
|
|
1114
|
-
throw new
|
|
1174
|
+
throw new APIError3("UNAUTHORIZED", {
|
|
1115
1175
|
message: "Invalid email or password"
|
|
1116
1176
|
});
|
|
1117
1177
|
}
|
|
1118
1178
|
const currentPassword = credentialAccount?.password;
|
|
1119
1179
|
if (!currentPassword) {
|
|
1120
1180
|
ctx.context.logger.error("Password not found", { email });
|
|
1121
|
-
throw new
|
|
1181
|
+
throw new APIError3("UNAUTHORIZED", {
|
|
1122
1182
|
message: "Unexpected error"
|
|
1123
1183
|
});
|
|
1124
1184
|
}
|
|
@@ -1128,7 +1188,7 @@ var signInEmail = createAuthEndpoint(
|
|
|
1128
1188
|
);
|
|
1129
1189
|
if (!validPassword) {
|
|
1130
1190
|
ctx.context.logger.error("Invalid password");
|
|
1131
|
-
throw new
|
|
1191
|
+
throw new APIError3("UNAUTHORIZED", {
|
|
1132
1192
|
message: "Invalid email or password"
|
|
1133
1193
|
});
|
|
1134
1194
|
}
|
|
@@ -1139,7 +1199,7 @@ var signInEmail = createAuthEndpoint(
|
|
|
1139
1199
|
);
|
|
1140
1200
|
if (!session) {
|
|
1141
1201
|
ctx.context.logger.error("Failed to create session");
|
|
1142
|
-
throw new
|
|
1202
|
+
throw new APIError3("INTERNAL_SERVER_ERROR");
|
|
1143
1203
|
}
|
|
1144
1204
|
await setSessionCookie(ctx, session.id, ctx.body.dontRememberMe);
|
|
1145
1205
|
return ctx.json({
|
|
@@ -1150,46 +1210,64 @@ var signInEmail = createAuthEndpoint(
|
|
|
1150
1210
|
});
|
|
1151
1211
|
}
|
|
1152
1212
|
);
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1213
|
+
|
|
1214
|
+
// src/api/routes/callback.ts
|
|
1215
|
+
import { APIError as APIError4 } from "better-call";
|
|
1216
|
+
import { z as z6 } from "zod";
|
|
1217
|
+
|
|
1218
|
+
// src/db/schema.ts
|
|
1219
|
+
import { z as z5 } from "zod";
|
|
1220
|
+
var accountSchema = z5.object({
|
|
1221
|
+
id: z5.string(),
|
|
1222
|
+
providerId: z5.string(),
|
|
1223
|
+
accountId: z5.string(),
|
|
1224
|
+
userId: z5.string(),
|
|
1225
|
+
accessToken: z5.string().nullable().optional(),
|
|
1226
|
+
refreshToken: z5.string().nullable().optional(),
|
|
1227
|
+
idToken: z5.string().nullable().optional(),
|
|
1161
1228
|
/**
|
|
1162
1229
|
* Access token expires at
|
|
1163
1230
|
*/
|
|
1164
|
-
expiresAt:
|
|
1231
|
+
expiresAt: z5.date().nullable().optional(),
|
|
1165
1232
|
/**
|
|
1166
1233
|
* Password is only stored in the credential provider
|
|
1167
1234
|
*/
|
|
1168
|
-
password:
|
|
1235
|
+
password: z5.string().optional().nullable()
|
|
1169
1236
|
});
|
|
1170
|
-
var userSchema =
|
|
1171
|
-
id:
|
|
1172
|
-
email:
|
|
1173
|
-
emailVerified:
|
|
1174
|
-
name:
|
|
1175
|
-
image:
|
|
1176
|
-
createdAt:
|
|
1177
|
-
updatedAt:
|
|
1237
|
+
var userSchema = z5.object({
|
|
1238
|
+
id: z5.string(),
|
|
1239
|
+
email: z5.string().transform((val) => val.toLowerCase()),
|
|
1240
|
+
emailVerified: z5.boolean().default(false),
|
|
1241
|
+
name: z5.string(),
|
|
1242
|
+
image: z5.string().optional(),
|
|
1243
|
+
createdAt: z5.date().default(/* @__PURE__ */ new Date()),
|
|
1244
|
+
updatedAt: z5.date().default(/* @__PURE__ */ new Date())
|
|
1178
1245
|
});
|
|
1179
|
-
|
|
1180
|
-
id:
|
|
1181
|
-
userId:
|
|
1182
|
-
expiresAt:
|
|
1183
|
-
ipAddress:
|
|
1184
|
-
userAgent:
|
|
1246
|
+
var sessionSchema = z5.object({
|
|
1247
|
+
id: z5.string(),
|
|
1248
|
+
userId: z5.string(),
|
|
1249
|
+
expiresAt: z5.date(),
|
|
1250
|
+
ipAddress: z5.string().optional(),
|
|
1251
|
+
userAgent: z5.string().optional()
|
|
1185
1252
|
});
|
|
1186
|
-
|
|
1187
|
-
id:
|
|
1188
|
-
value:
|
|
1189
|
-
expiresAt:
|
|
1190
|
-
identifier:
|
|
1253
|
+
var verificationSchema = z5.object({
|
|
1254
|
+
id: z5.string(),
|
|
1255
|
+
value: z5.string(),
|
|
1256
|
+
expiresAt: z5.date(),
|
|
1257
|
+
identifier: z5.string()
|
|
1191
1258
|
});
|
|
1192
1259
|
|
|
1260
|
+
// src/utils/id.ts
|
|
1261
|
+
import { nanoid } from "nanoid";
|
|
1262
|
+
var generateId = (size) => {
|
|
1263
|
+
return nanoid(size);
|
|
1264
|
+
};
|
|
1265
|
+
|
|
1266
|
+
// src/utils/hide-metadata.ts
|
|
1267
|
+
var HIDE_METADATA = {
|
|
1268
|
+
isAction: false
|
|
1269
|
+
};
|
|
1270
|
+
|
|
1193
1271
|
// src/utils/getAccount.ts
|
|
1194
1272
|
function getAccountTokens(tokens) {
|
|
1195
1273
|
const accessToken = tokens.accessToken();
|
|
@@ -1211,10 +1289,10 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1211
1289
|
"/callback/:id",
|
|
1212
1290
|
{
|
|
1213
1291
|
method: "GET",
|
|
1214
|
-
query:
|
|
1215
|
-
state:
|
|
1216
|
-
code:
|
|
1217
|
-
error:
|
|
1292
|
+
query: z6.object({
|
|
1293
|
+
state: z6.string(),
|
|
1294
|
+
code: z6.string().optional(),
|
|
1295
|
+
error: z6.string().optional()
|
|
1218
1296
|
}),
|
|
1219
1297
|
metadata: HIDE_METADATA
|
|
1220
1298
|
},
|
|
@@ -1342,7 +1420,7 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1342
1420
|
}
|
|
1343
1421
|
}
|
|
1344
1422
|
if (!userId && !id)
|
|
1345
|
-
throw new
|
|
1423
|
+
throw new APIError4("INTERNAL_SERVER_ERROR", {
|
|
1346
1424
|
message: "Unable to create user"
|
|
1347
1425
|
});
|
|
1348
1426
|
try {
|
|
@@ -1372,13 +1450,16 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1372
1450
|
throw c.redirect(callbackURL);
|
|
1373
1451
|
}
|
|
1374
1452
|
);
|
|
1453
|
+
|
|
1454
|
+
// src/api/routes/sign-out.ts
|
|
1455
|
+
import { z as z7 } from "zod";
|
|
1375
1456
|
var signOut = createAuthEndpoint(
|
|
1376
1457
|
"/sign-out",
|
|
1377
1458
|
{
|
|
1378
1459
|
method: "POST",
|
|
1379
|
-
body:
|
|
1380
|
-
|
|
1381
|
-
callbackURL:
|
|
1460
|
+
body: z7.optional(
|
|
1461
|
+
z7.object({
|
|
1462
|
+
callbackURL: z7.string().optional()
|
|
1382
1463
|
})
|
|
1383
1464
|
)
|
|
1384
1465
|
},
|
|
@@ -1400,22 +1481,28 @@ var signOut = createAuthEndpoint(
|
|
|
1400
1481
|
});
|
|
1401
1482
|
}
|
|
1402
1483
|
);
|
|
1484
|
+
|
|
1485
|
+
// src/api/routes/forget-password.ts
|
|
1486
|
+
import { TimeSpan as TimeSpan2 } from "oslo";
|
|
1487
|
+
import { createJWT, parseJWT as parseJWT3 } from "oslo/jwt";
|
|
1488
|
+
import { validateJWT } from "oslo/jwt";
|
|
1489
|
+
import { z as z8 } from "zod";
|
|
1403
1490
|
var forgetPassword = createAuthEndpoint(
|
|
1404
1491
|
"/forget-password",
|
|
1405
1492
|
{
|
|
1406
1493
|
method: "POST",
|
|
1407
|
-
body:
|
|
1494
|
+
body: z8.object({
|
|
1408
1495
|
/**
|
|
1409
1496
|
* The email address of the user to send a password reset email to.
|
|
1410
1497
|
*/
|
|
1411
|
-
email:
|
|
1498
|
+
email: z8.string().email(),
|
|
1412
1499
|
/**
|
|
1413
1500
|
* The URL to redirect the user to reset their password.
|
|
1414
1501
|
* If the token isn't valid or expired, it'll be redirected with a query parameter `?
|
|
1415
1502
|
* error=INVALID_TOKEN`. If the token is valid, it'll be redirected with a query parameter `?
|
|
1416
1503
|
* token=VALID_TOKEN
|
|
1417
1504
|
*/
|
|
1418
|
-
redirectTo:
|
|
1505
|
+
redirectTo: z8.string()
|
|
1419
1506
|
})
|
|
1420
1507
|
},
|
|
1421
1508
|
async (ctx) => {
|
|
@@ -1453,7 +1540,7 @@ var forgetPassword = createAuthEndpoint(
|
|
|
1453
1540
|
redirectTo: ctx.body.redirectTo
|
|
1454
1541
|
},
|
|
1455
1542
|
{
|
|
1456
|
-
expiresIn: new
|
|
1543
|
+
expiresIn: new TimeSpan2(1, "h"),
|
|
1457
1544
|
issuer: "better-auth",
|
|
1458
1545
|
subject: "forget-password",
|
|
1459
1546
|
audiences: [user.user.email],
|
|
@@ -1478,9 +1565,9 @@ var forgetPasswordCallback = createAuthEndpoint(
|
|
|
1478
1565
|
async (ctx) => {
|
|
1479
1566
|
const { token } = ctx.params;
|
|
1480
1567
|
let decodedToken;
|
|
1481
|
-
const schema =
|
|
1482
|
-
email:
|
|
1483
|
-
redirectTo:
|
|
1568
|
+
const schema = z8.object({
|
|
1569
|
+
email: z8.string(),
|
|
1570
|
+
redirectTo: z8.string()
|
|
1484
1571
|
});
|
|
1485
1572
|
try {
|
|
1486
1573
|
decodedToken = await validateJWT(
|
|
@@ -1492,7 +1579,7 @@ var forgetPasswordCallback = createAuthEndpoint(
|
|
|
1492
1579
|
throw Error("Token expired");
|
|
1493
1580
|
}
|
|
1494
1581
|
} catch (e) {
|
|
1495
|
-
const decoded =
|
|
1582
|
+
const decoded = parseJWT3(token);
|
|
1496
1583
|
const jwt = schema.safeParse(decoded?.payload);
|
|
1497
1584
|
if (jwt.success) {
|
|
1498
1585
|
throw ctx.redirect(`${jwt.data?.redirectTo}?error=invalid_token`);
|
|
@@ -1508,12 +1595,12 @@ var resetPassword = createAuthEndpoint(
|
|
|
1508
1595
|
"/reset-password",
|
|
1509
1596
|
{
|
|
1510
1597
|
method: "POST",
|
|
1511
|
-
query:
|
|
1512
|
-
currentURL:
|
|
1598
|
+
query: z8.object({
|
|
1599
|
+
currentURL: z8.string()
|
|
1513
1600
|
}).optional(),
|
|
1514
|
-
body:
|
|
1515
|
-
newPassword:
|
|
1516
|
-
callbackURL:
|
|
1601
|
+
body: z8.object({
|
|
1602
|
+
newPassword: z8.string(),
|
|
1603
|
+
callbackURL: z8.string().optional()
|
|
1517
1604
|
})
|
|
1518
1605
|
},
|
|
1519
1606
|
async (ctx) => {
|
|
@@ -1540,7 +1627,7 @@ var resetPassword = createAuthEndpoint(
|
|
|
1540
1627
|
Buffer.from(ctx.context.secret),
|
|
1541
1628
|
token
|
|
1542
1629
|
);
|
|
1543
|
-
const email =
|
|
1630
|
+
const email = z8.string().email().parse(jwt.payload.email);
|
|
1544
1631
|
const user = await ctx.context.internalAdapter.findUserByEmail(email);
|
|
1545
1632
|
if (!user) {
|
|
1546
1633
|
return ctx.json(
|
|
@@ -1620,15 +1707,20 @@ var resetPassword = createAuthEndpoint(
|
|
|
1620
1707
|
}
|
|
1621
1708
|
}
|
|
1622
1709
|
);
|
|
1710
|
+
|
|
1711
|
+
// src/api/routes/verify-email.ts
|
|
1712
|
+
import { TimeSpan as TimeSpan3 } from "oslo";
|
|
1713
|
+
import { createJWT as createJWT2, validateJWT as validateJWT2 } from "oslo/jwt";
|
|
1714
|
+
import { z as z9 } from "zod";
|
|
1623
1715
|
async function createEmailVerificationToken(secret, email) {
|
|
1624
|
-
const token = await
|
|
1716
|
+
const token = await createJWT2(
|
|
1625
1717
|
"HS256",
|
|
1626
1718
|
Buffer.from(secret),
|
|
1627
1719
|
{
|
|
1628
1720
|
email: email.toLowerCase()
|
|
1629
1721
|
},
|
|
1630
1722
|
{
|
|
1631
|
-
expiresIn: new
|
|
1723
|
+
expiresIn: new TimeSpan3(1, "h"),
|
|
1632
1724
|
issuer: "better-auth",
|
|
1633
1725
|
subject: "verify-email",
|
|
1634
1726
|
audiences: [email],
|
|
@@ -1641,12 +1733,12 @@ var sendVerificationEmail = createAuthEndpoint(
|
|
|
1641
1733
|
"/send-verification-email",
|
|
1642
1734
|
{
|
|
1643
1735
|
method: "POST",
|
|
1644
|
-
query:
|
|
1645
|
-
currentURL:
|
|
1736
|
+
query: z9.object({
|
|
1737
|
+
currentURL: z9.string().optional()
|
|
1646
1738
|
}).optional(),
|
|
1647
|
-
body:
|
|
1648
|
-
email:
|
|
1649
|
-
callbackURL:
|
|
1739
|
+
body: z9.object({
|
|
1740
|
+
email: z9.string().email(),
|
|
1741
|
+
callbackURL: z9.string().optional()
|
|
1650
1742
|
})
|
|
1651
1743
|
},
|
|
1652
1744
|
async (ctx) => {
|
|
@@ -1679,16 +1771,16 @@ var verifyEmail = createAuthEndpoint(
|
|
|
1679
1771
|
"/verify-email",
|
|
1680
1772
|
{
|
|
1681
1773
|
method: "GET",
|
|
1682
|
-
query:
|
|
1683
|
-
token:
|
|
1684
|
-
callbackURL:
|
|
1774
|
+
query: z9.object({
|
|
1775
|
+
token: z9.string(),
|
|
1776
|
+
callbackURL: z9.string().optional()
|
|
1685
1777
|
})
|
|
1686
1778
|
},
|
|
1687
1779
|
async (ctx) => {
|
|
1688
1780
|
const { token } = ctx.query;
|
|
1689
1781
|
let jwt;
|
|
1690
1782
|
try {
|
|
1691
|
-
jwt = await
|
|
1783
|
+
jwt = await validateJWT2("HS256", Buffer.from(ctx.context.secret), token);
|
|
1692
1784
|
} catch (e) {
|
|
1693
1785
|
ctx.context.logger.error("Failed to verify email", e);
|
|
1694
1786
|
return ctx.json(null, {
|
|
@@ -1699,8 +1791,8 @@ var verifyEmail = createAuthEndpoint(
|
|
|
1699
1791
|
}
|
|
1700
1792
|
});
|
|
1701
1793
|
}
|
|
1702
|
-
const schema =
|
|
1703
|
-
email:
|
|
1794
|
+
const schema = z9.object({
|
|
1795
|
+
email: z9.string().email()
|
|
1704
1796
|
});
|
|
1705
1797
|
const parsed = schema.parse(jwt.payload);
|
|
1706
1798
|
const user = await ctx.context.internalAdapter.findUserByEmail(
|
|
@@ -1732,6 +1824,9 @@ var verifyEmail = createAuthEndpoint(
|
|
|
1732
1824
|
}
|
|
1733
1825
|
);
|
|
1734
1826
|
|
|
1827
|
+
// src/api/routes/update-user.ts
|
|
1828
|
+
import { z as z10 } from "zod";
|
|
1829
|
+
|
|
1735
1830
|
// src/crypto/random.ts
|
|
1736
1831
|
function byteToBinary(byte) {
|
|
1737
1832
|
return byte.toString(2).padStart(8, "0");
|
|
@@ -1794,9 +1889,9 @@ var updateUser = createAuthEndpoint(
|
|
|
1794
1889
|
"/user/update",
|
|
1795
1890
|
{
|
|
1796
1891
|
method: "POST",
|
|
1797
|
-
body:
|
|
1798
|
-
name:
|
|
1799
|
-
image:
|
|
1892
|
+
body: z10.object({
|
|
1893
|
+
name: z10.string().optional(),
|
|
1894
|
+
image: z10.string().optional()
|
|
1800
1895
|
}),
|
|
1801
1896
|
use: [sessionMiddleware]
|
|
1802
1897
|
},
|
|
@@ -1820,20 +1915,20 @@ var changePassword = createAuthEndpoint(
|
|
|
1820
1915
|
"/user/change-password",
|
|
1821
1916
|
{
|
|
1822
1917
|
method: "POST",
|
|
1823
|
-
body:
|
|
1918
|
+
body: z10.object({
|
|
1824
1919
|
/**
|
|
1825
1920
|
* The new password to set
|
|
1826
1921
|
*/
|
|
1827
|
-
newPassword:
|
|
1922
|
+
newPassword: z10.string(),
|
|
1828
1923
|
/**
|
|
1829
1924
|
* The current password of the user
|
|
1830
1925
|
*/
|
|
1831
|
-
currentPassword:
|
|
1926
|
+
currentPassword: z10.string(),
|
|
1832
1927
|
/**
|
|
1833
1928
|
* revoke all sessions that are not the
|
|
1834
1929
|
* current one logged in by the user
|
|
1835
1930
|
*/
|
|
1836
|
-
revokeOtherSessions:
|
|
1931
|
+
revokeOtherSessions: z10.boolean().optional()
|
|
1837
1932
|
}),
|
|
1838
1933
|
use: [sessionMiddleware]
|
|
1839
1934
|
},
|
|
@@ -1903,11 +1998,11 @@ var setPassword = createAuthEndpoint(
|
|
|
1903
1998
|
"/user/set-password",
|
|
1904
1999
|
{
|
|
1905
2000
|
method: "POST",
|
|
1906
|
-
body:
|
|
2001
|
+
body: z10.object({
|
|
1907
2002
|
/**
|
|
1908
2003
|
* The new password to set
|
|
1909
2004
|
*/
|
|
1910
|
-
newPassword:
|
|
2005
|
+
newPassword: z10.string()
|
|
1911
2006
|
}),
|
|
1912
2007
|
use: [sessionMiddleware]
|
|
1913
2008
|
},
|
|
@@ -1957,8 +2052,8 @@ var deleteUser = createAuthEndpoint(
|
|
|
1957
2052
|
"/user/delete",
|
|
1958
2053
|
{
|
|
1959
2054
|
method: "POST",
|
|
1960
|
-
body:
|
|
1961
|
-
password:
|
|
2055
|
+
body: z10.object({
|
|
2056
|
+
password: z10.string()
|
|
1962
2057
|
}),
|
|
1963
2058
|
use: [sessionMiddleware]
|
|
1964
2059
|
},
|
|
@@ -2136,19 +2231,22 @@ var ok = createAuthEndpoint(
|
|
|
2136
2231
|
});
|
|
2137
2232
|
}
|
|
2138
2233
|
);
|
|
2234
|
+
|
|
2235
|
+
// src/api/routes/sign-up.ts
|
|
2236
|
+
import { z as z11 } from "zod";
|
|
2139
2237
|
var signUpEmail = createAuthEndpoint(
|
|
2140
2238
|
"/sign-up/email",
|
|
2141
2239
|
{
|
|
2142
2240
|
method: "POST",
|
|
2143
|
-
query:
|
|
2144
|
-
currentURL:
|
|
2241
|
+
query: z11.object({
|
|
2242
|
+
currentURL: z11.string().optional()
|
|
2145
2243
|
}).optional(),
|
|
2146
|
-
body:
|
|
2147
|
-
name:
|
|
2148
|
-
email:
|
|
2149
|
-
password:
|
|
2150
|
-
image:
|
|
2151
|
-
callbackURL:
|
|
2244
|
+
body: z11.object({
|
|
2245
|
+
name: z11.string(),
|
|
2246
|
+
email: z11.string(),
|
|
2247
|
+
password: z11.string(),
|
|
2248
|
+
image: z11.string().optional(),
|
|
2249
|
+
callbackURL: z11.string().optional()
|
|
2152
2250
|
})
|
|
2153
2251
|
},
|
|
2154
2252
|
async (ctx) => {
|
|
@@ -2161,7 +2259,7 @@ var signUpEmail = createAuthEndpoint(
|
|
|
2161
2259
|
});
|
|
2162
2260
|
}
|
|
2163
2261
|
const { name, email, password, image } = ctx.body;
|
|
2164
|
-
const isValidEmail =
|
|
2262
|
+
const isValidEmail = z11.string().email().safeParse(email);
|
|
2165
2263
|
if (!isValidEmail.success) {
|
|
2166
2264
|
return ctx.json(null, {
|
|
2167
2265
|
status: 400,
|
|
@@ -2263,6 +2361,9 @@ var signUpEmail = createAuthEndpoint(
|
|
|
2263
2361
|
}
|
|
2264
2362
|
);
|
|
2265
2363
|
|
|
2364
|
+
// src/api/index.ts
|
|
2365
|
+
import chalk from "chalk";
|
|
2366
|
+
|
|
2266
2367
|
// src/api/rate-limiter.ts
|
|
2267
2368
|
function shouldRateLimit(max, window2, rateLimitData) {
|
|
2268
2369
|
const now = Date.now();
|
|
@@ -2560,7 +2661,7 @@ var router = (ctx, options) => {
|
|
|
2560
2661
|
onError(e) {
|
|
2561
2662
|
const log = options.logger?.verboseLogging ? logger : void 0;
|
|
2562
2663
|
if (options.logger?.disabled !== true) {
|
|
2563
|
-
if (e instanceof
|
|
2664
|
+
if (e instanceof APIError5) {
|
|
2564
2665
|
if (e.status === "INTERNAL_SERVER_ERROR") {
|
|
2565
2666
|
logger.error(e);
|
|
2566
2667
|
}
|
|
@@ -2770,6 +2871,14 @@ var getAuthTables = (options) => {
|
|
|
2770
2871
|
...shouldAddRateLimitTable ? rateLimitTable : {}
|
|
2771
2872
|
};
|
|
2772
2873
|
};
|
|
2874
|
+
|
|
2875
|
+
// src/adapters/kysely-adapter/dialect.ts
|
|
2876
|
+
import { Kysely } from "kysely";
|
|
2877
|
+
import {
|
|
2878
|
+
MysqlDialect,
|
|
2879
|
+
PostgresDialect,
|
|
2880
|
+
SqliteDialect
|
|
2881
|
+
} from "kysely";
|
|
2773
2882
|
var createKyselyAdapter = async (config2) => {
|
|
2774
2883
|
const db = config2.database;
|
|
2775
2884
|
let dialect = void 0;
|
|
@@ -2811,6 +2920,9 @@ var createKyselyAdapter = async (config2) => {
|
|
|
2811
2920
|
};
|
|
2812
2921
|
};
|
|
2813
2922
|
|
|
2923
|
+
// src/cli/utils/get-migration.ts
|
|
2924
|
+
import "kysely";
|
|
2925
|
+
|
|
2814
2926
|
// src/cli/utils/get-schema.ts
|
|
2815
2927
|
function getSchema(config2) {
|
|
2816
2928
|
const tables = getAuthTables(config2);
|
|
@@ -3197,6 +3309,10 @@ async function getAdapter(options, isCli) {
|
|
|
3197
3309
|
});
|
|
3198
3310
|
}
|
|
3199
3311
|
|
|
3312
|
+
// src/crypto/password.ts
|
|
3313
|
+
import { scrypt } from "node:crypto";
|
|
3314
|
+
import { decodeHex, encodeHex } from "oslo/encoding";
|
|
3315
|
+
|
|
3200
3316
|
// src/crypto/buffer.ts
|
|
3201
3317
|
function constantTimeEqual(a, b) {
|
|
3202
3318
|
const aBuffer = new Uint8Array(a);
|
|
@@ -3250,9 +3366,9 @@ var verifyPassword = async (hash, password) => {
|
|
|
3250
3366
|
};
|
|
3251
3367
|
|
|
3252
3368
|
// src/db/with-hooks.ts
|
|
3253
|
-
function getWithHooks(adapter,
|
|
3254
|
-
const hooks =
|
|
3255
|
-
const tables = getAuthTables(options);
|
|
3369
|
+
function getWithHooks(adapter, ctx) {
|
|
3370
|
+
const hooks = ctx.hooks;
|
|
3371
|
+
const tables = getAuthTables(ctx.options);
|
|
3256
3372
|
async function createWithHooks(data, model) {
|
|
3257
3373
|
let actualData = data;
|
|
3258
3374
|
for (const hook of hooks || []) {
|
|
@@ -3314,10 +3430,11 @@ function getWithHooks(adapter, options) {
|
|
|
3314
3430
|
}
|
|
3315
3431
|
|
|
3316
3432
|
// src/db/internal-adapter.ts
|
|
3317
|
-
var createInternalAdapter = (adapter,
|
|
3433
|
+
var createInternalAdapter = (adapter, ctx) => {
|
|
3434
|
+
const options = ctx.options;
|
|
3318
3435
|
const sessionExpiration = options.session?.expiresIn || 60 * 60 * 24 * 7;
|
|
3319
3436
|
const tables = getAuthTables(options);
|
|
3320
|
-
const { createWithHooks, updateWithHooks } = getWithHooks(adapter,
|
|
3437
|
+
const { createWithHooks, updateWithHooks } = getWithHooks(adapter, ctx);
|
|
3321
3438
|
return {
|
|
3322
3439
|
createOAuthUser: async (user, account) => {
|
|
3323
3440
|
try {
|
|
@@ -3403,7 +3520,7 @@ var createInternalAdapter = (adapter, options) => {
|
|
|
3403
3520
|
return updatedSession;
|
|
3404
3521
|
},
|
|
3405
3522
|
deleteSession: async (id) => {
|
|
3406
|
-
await adapter.delete({
|
|
3523
|
+
const session = await adapter.delete({
|
|
3407
3524
|
model: tables.session.tableName,
|
|
3408
3525
|
where: [
|
|
3409
3526
|
{
|
|
@@ -3569,6 +3686,9 @@ var createInternalAdapter = (adapter, options) => {
|
|
|
3569
3686
|
};
|
|
3570
3687
|
};
|
|
3571
3688
|
|
|
3689
|
+
// src/init.ts
|
|
3690
|
+
import { defu } from "defu";
|
|
3691
|
+
|
|
3572
3692
|
// src/utils/constants.ts
|
|
3573
3693
|
var DEFAULT_SECRET = "better-auth-secret-123456789";
|
|
3574
3694
|
|
|
@@ -3618,7 +3738,7 @@ var crossSubdomainCookies = (options) => {
|
|
|
3618
3738
|
|
|
3619
3739
|
// src/init.ts
|
|
3620
3740
|
var init = async (opts) => {
|
|
3621
|
-
const { options, context } = runPluginInit(opts);
|
|
3741
|
+
const { options, context, dbHooks } = runPluginInit(opts);
|
|
3622
3742
|
const plugins = options.plugins || [];
|
|
3623
3743
|
const internalPlugins = getInternalPlugins(options);
|
|
3624
3744
|
const adapter = await getAdapter(options);
|
|
@@ -3678,7 +3798,10 @@ var init = async (opts) => {
|
|
|
3678
3798
|
}
|
|
3679
3799
|
},
|
|
3680
3800
|
adapter,
|
|
3681
|
-
internalAdapter: createInternalAdapter(adapter,
|
|
3801
|
+
internalAdapter: createInternalAdapter(adapter, {
|
|
3802
|
+
options,
|
|
3803
|
+
hooks: dbHooks.filter((u) => u !== void 0)
|
|
3804
|
+
}),
|
|
3682
3805
|
createAuthCookie: createCookieGetter(options),
|
|
3683
3806
|
...context
|
|
3684
3807
|
};
|
|
@@ -3686,11 +3809,15 @@ var init = async (opts) => {
|
|
|
3686
3809
|
function runPluginInit(options) {
|
|
3687
3810
|
const plugins = options.plugins || [];
|
|
3688
3811
|
let context = {};
|
|
3812
|
+
const dbHooks = [options.databaseHooks];
|
|
3689
3813
|
for (const plugin of plugins) {
|
|
3690
3814
|
if (plugin.init) {
|
|
3691
3815
|
const result = plugin.init(options);
|
|
3692
3816
|
if (typeof result === "object") {
|
|
3693
3817
|
if (result.options) {
|
|
3818
|
+
if (result.options.databaseHooks) {
|
|
3819
|
+
dbHooks.push(result.options.databaseHooks);
|
|
3820
|
+
}
|
|
3694
3821
|
options = defu(options, result.options);
|
|
3695
3822
|
}
|
|
3696
3823
|
if (result.context) {
|
|
@@ -3701,7 +3828,8 @@ function runPluginInit(options) {
|
|
|
3701
3828
|
}
|
|
3702
3829
|
return {
|
|
3703
3830
|
options,
|
|
3704
|
-
context
|
|
3831
|
+
context,
|
|
3832
|
+
dbHooks
|
|
3705
3833
|
};
|
|
3706
3834
|
}
|
|
3707
3835
|
function getInternalPlugins(options) {
|
|
@@ -3744,5 +3872,6 @@ var betterAuth = (options) => {
|
|
|
3744
3872
|
$Infer: {}
|
|
3745
3873
|
};
|
|
3746
3874
|
};
|
|
3747
|
-
|
|
3748
|
-
|
|
3875
|
+
export {
|
|
3876
|
+
betterAuth
|
|
3877
|
+
};
|