better-auth 0.0.8-beta.2 → 0.0.8-beta.21

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.
Files changed (58) hide show
  1. package/dist/access.d.ts +3 -4
  2. package/dist/access.js +3 -13
  3. package/dist/access.js.map +1 -1
  4. package/dist/cli.d.ts +1 -2
  5. package/dist/cli.js +36 -44
  6. package/dist/cli.js.map +1 -1
  7. package/dist/client/plugins.d.ts +97 -2407
  8. package/dist/client/plugins.js +143 -203
  9. package/dist/client/plugins.js.map +1 -1
  10. package/dist/client.d.ts +159 -1213
  11. package/dist/client.js +125 -527
  12. package/dist/client.js.map +1 -1
  13. package/dist/{helper-B5_2Vzba.d.ts → helper-D8dhRz72.d.ts} +1 -4
  14. package/dist/{index-Dg4eEXZW.d.ts → index-B9jOjqnF.d.ts} +1 -1
  15. package/dist/{schema-BOszzrbQ.d.ts → index-CcxejJTH.d.ts} +172 -142
  16. package/dist/{client-CaF9eUcv.d.ts → index-Dwhjsk4l.d.ts} +2014 -1971
  17. package/dist/index.d.ts +1124 -6
  18. package/dist/index.js +737 -707
  19. package/dist/index.js.map +1 -1
  20. package/dist/internal-adapter-CVKQ4XR9.d.ts +637 -0
  21. package/dist/next-js.d.ts +17 -7
  22. package/dist/next-js.js +20 -3
  23. package/dist/next-js.js.map +1 -1
  24. package/dist/plugins.d.ts +12 -883
  25. package/dist/plugins.js +743 -679
  26. package/dist/plugins.js.map +1 -1
  27. package/dist/react.d.ts +312 -12
  28. package/dist/react.js +138 -148
  29. package/dist/react.js.map +1 -1
  30. package/dist/social.d.ts +2 -2
  31. package/dist/social.js +179 -151
  32. package/dist/social.js.map +1 -1
  33. package/dist/solid-start.d.ts +7 -6
  34. package/dist/solid-start.js +3 -3
  35. package/dist/solid-start.js.map +1 -1
  36. package/dist/solid.d.ts +91 -2713
  37. package/dist/solid.js +130 -139
  38. package/dist/solid.js.map +1 -1
  39. package/dist/{statement-COylZd3J.d.ts → statement-D6SPoYOh.d.ts} +7 -7
  40. package/dist/svelte-kit.d.ts +6 -5
  41. package/dist/svelte-kit.js +10 -9
  42. package/dist/svelte-kit.js.map +1 -1
  43. package/dist/svelte.d.ts +89 -2713
  44. package/dist/svelte.js +124 -138
  45. package/dist/svelte.js.map +1 -1
  46. package/dist/types-D4WrjKeJ.d.ts +81 -0
  47. package/dist/types.d.ts +31 -5
  48. package/dist/types.js +2 -0
  49. package/dist/types.js.map +1 -1
  50. package/dist/vue.d.ts +313 -12
  51. package/dist/vue.js +131 -145
  52. package/dist/vue.js.map +1 -1
  53. package/package.json +8 -3
  54. package/dist/index-CGeV0d2g.d.ts +0 -1498
  55. package/dist/preact.d.ts +0 -8
  56. package/dist/preact.js +0 -291
  57. package/dist/preact.js.map +0 -1
  58. package/dist/type-tYx_kmry.d.ts +0 -5724
@@ -0,0 +1,637 @@
1
+ import { Kysely, Migration, Dialect } from 'kysely';
2
+ import { O as OAuthProvider, U as User, A as Account, S as Session } from './index-CcxejJTH.js';
3
+ import { ZodSchema } from 'zod';
4
+ import { ContextTools, CookieOptions, Endpoint, EndpointResponse } from 'better-call';
5
+ import { L as LiteralString } from './helper-D8dhRz72.js';
6
+
7
+ declare const createFieldAttribute: <T extends FieldType, C extends Omit<FieldAttributeConfig<T>, "type">>(type: T, config?: C) => {
8
+ required?: boolean;
9
+ returned?: boolean;
10
+ hashValue?: boolean;
11
+ defaultValue?: InferValueType<T> | (() => InferValueType<T>) | undefined;
12
+ transform?: ((value: InferValueType<T>) => InferValueType<T>) | undefined;
13
+ references?: {
14
+ /**
15
+ * The model to reference.
16
+ */
17
+ model: string;
18
+ /**
19
+ * The field on the referenced model.
20
+ */
21
+ field: string;
22
+ /**
23
+ * The action to perform when the reference is deleted.
24
+ * @default "cascade"
25
+ */
26
+ onDelete?: "no action" | "restrict" | "cascade" | "set null" | "set default";
27
+ };
28
+ unique?: boolean;
29
+ validator?: ZodSchema;
30
+ type: T;
31
+ };
32
+ type FieldAttribute<T extends FieldType = FieldType> = {
33
+ type: T;
34
+ } & FieldAttributeConfig<T>;
35
+ type FieldType = "string" | "number" | "boolean" | "date";
36
+ type InferValueType<T extends FieldType> = T extends "string" ? string : T extends "number" ? number : T extends "boolean" ? boolean : T extends "date" ? Date : never;
37
+ type InferFieldOutput<T extends FieldAttribute> = T["returned"] extends false ? never : T["required"] extends false ? InferValueType<T["type"]> | undefined : InferValueType<T["type"]>;
38
+ type FieldAttributeConfig<T extends FieldType = FieldType> = {
39
+ /**
40
+ * if the field should be required on a new record.
41
+ * @default false
42
+ */
43
+ required?: boolean;
44
+ /**
45
+ * If the value should be returned on a response body.
46
+ * @default true
47
+ */
48
+ returned?: boolean;
49
+ /**
50
+ * If the value should be hashed when it's stored.
51
+ * @default false
52
+ */
53
+ hashValue?: boolean;
54
+ /**
55
+ * Default value for the field
56
+ *
57
+ * Note: This will not create a default value on the database level. It will only
58
+ * be used when creating a new record.
59
+ */
60
+ defaultValue?: InferValueType<T> | (() => InferValueType<T>);
61
+ /**
62
+ * transform the value before storing it.
63
+ */
64
+ transform?: (value: InferValueType<T>) => InferValueType<T>;
65
+ /**
66
+ * Reference to another model.
67
+ */
68
+ references?: {
69
+ /**
70
+ * The model to reference.
71
+ */
72
+ model: string;
73
+ /**
74
+ * The field on the referenced model.
75
+ */
76
+ field: string;
77
+ /**
78
+ * The action to perform when the reference is deleted.
79
+ * @default "cascade"
80
+ */
81
+ onDelete?: "no action" | "restrict" | "cascade" | "set null" | "set default";
82
+ };
83
+ unique?: boolean;
84
+ /**
85
+ * A zod schema to validate the value.
86
+ */
87
+ validator?: ZodSchema;
88
+ };
89
+
90
+ /**
91
+ * Adapter where clause
92
+ */
93
+ type Where = {
94
+ operator?: "eq" | "ne" | "lt" | "lte" | "gt" | "gte";
95
+ value: string;
96
+ field: string;
97
+ connector?: "AND" | "OR";
98
+ };
99
+ /**
100
+ * Adapter Interface
101
+ */
102
+ interface Adapter {
103
+ create: <T, R = T>(data: {
104
+ model: string;
105
+ data: T;
106
+ select?: string[];
107
+ }) => Promise<R>;
108
+ findOne: <T>(data: {
109
+ model: string;
110
+ where: Where[];
111
+ select?: string[];
112
+ }) => Promise<T | null>;
113
+ findMany: <T>(data: {
114
+ model: string;
115
+ where?: Where[];
116
+ }) => Promise<T[]>;
117
+ update: <T>(data: {
118
+ model: string;
119
+ where: Where[];
120
+ update: Record<string, any>;
121
+ }) => Promise<T | null>;
122
+ delete: <T>(data: {
123
+ model: string;
124
+ where: Where[];
125
+ }) => Promise<void>;
126
+ /**
127
+ * adapter specific configuration
128
+ */
129
+ config?: {
130
+ /**
131
+ * the format of the date fields
132
+ */
133
+ dateFormat?: "number" | "date";
134
+ /**
135
+ * if the adapter will throw an error when a
136
+ * record already exists. If this is set to
137
+ * false, there will be a check if the record
138
+ * exists or not before creating a new one.
139
+ */
140
+ failsOnRecordExist?: boolean;
141
+ };
142
+ createSchema?: (data: {
143
+ model: string;
144
+ fields: Record<string, FieldAttribute>;
145
+ }[]) => Promise<void>;
146
+ }
147
+
148
+ declare const createKyselyAdapter: (config: BetterAuthOptions) => Kysely<any> | null;
149
+
150
+ declare function getAdapter(options: BetterAuthOptions): Adapter;
151
+
152
+ type HookEndpointContext = ContextTools & {
153
+ context: AuthContext;
154
+ } & {
155
+ body: any;
156
+ request?: Request;
157
+ headers?: Headers;
158
+ params?: Record<string, string> | undefined;
159
+ query?: any;
160
+ method?: any;
161
+ };
162
+
163
+ declare function getCookies(options: BetterAuthOptions): {
164
+ sessionToken: {
165
+ name: string;
166
+ options: {
167
+ httpOnly: true;
168
+ sameSite: "lax";
169
+ path: string;
170
+ secure: boolean;
171
+ maxAge: number;
172
+ };
173
+ };
174
+ csrfToken: {
175
+ name: string;
176
+ options: {
177
+ httpOnly: true;
178
+ sameSite: "lax";
179
+ path: string;
180
+ secure: boolean;
181
+ maxAge: number;
182
+ };
183
+ };
184
+ state: {
185
+ name: string;
186
+ options: {
187
+ httpOnly: true;
188
+ sameSite: "lax";
189
+ path: string;
190
+ secure: boolean;
191
+ maxAge: number;
192
+ };
193
+ };
194
+ pkCodeVerifier: {
195
+ name: string;
196
+ options: CookieOptions;
197
+ };
198
+ dontRememberToken: {
199
+ name: string;
200
+ options: CookieOptions;
201
+ };
202
+ nonce: {
203
+ name: string;
204
+ options: CookieOptions;
205
+ };
206
+ };
207
+ declare function createCookieGetter(options: BetterAuthOptions): (cookieName: string, options?: CookieOptions) => {
208
+ name: string;
209
+ options: CookieOptions;
210
+ };
211
+ type BetterAuthCookies = ReturnType<typeof getCookies>;
212
+
213
+ declare const createLogger: (options?: {
214
+ disabled?: boolean;
215
+ }) => {
216
+ log: (...args: any[]) => void;
217
+ error: (...args: any[]) => void;
218
+ warn: (...args: any[]) => void;
219
+ info: (...args: any[]) => void;
220
+ debug: (...args: any[]) => void;
221
+ box: (...args: any[]) => void;
222
+ success: (...args: any[]) => void;
223
+ break: (...args: any[]) => void;
224
+ };
225
+
226
+ type AuthContext = {
227
+ options: BetterAuthOptions;
228
+ baseURL: string;
229
+ authCookies: BetterAuthCookies;
230
+ logger: ReturnType<typeof createLogger>;
231
+ db: ReturnType<typeof createKyselyAdapter>;
232
+ adapter: ReturnType<typeof getAdapter>;
233
+ internalAdapter: ReturnType<typeof createInternalAdapter>;
234
+ createAuthCookie: ReturnType<typeof createCookieGetter>;
235
+ secret: string;
236
+ session: {
237
+ updateAge: number;
238
+ expiresIn: number;
239
+ };
240
+ password: {
241
+ hash: (password: string) => Promise<string>;
242
+ verify: (password: string, hash: string) => Promise<boolean>;
243
+ };
244
+ };
245
+
246
+ type AuthEndpoint = Endpoint<(ctx: {
247
+ options: BetterAuthOptions;
248
+ body: any;
249
+ query: any;
250
+ headers: Headers;
251
+ }) => Promise<EndpointResponse>>;
252
+
253
+ type PluginSchema = {
254
+ [table: string]: {
255
+ fields: {
256
+ [field in string]: FieldAttribute;
257
+ };
258
+ disableMigration?: boolean;
259
+ };
260
+ };
261
+ type BetterAuthPlugin = {
262
+ id: LiteralString;
263
+ endpoints?: {
264
+ [key: string]: AuthEndpoint;
265
+ };
266
+ middlewares?: {
267
+ path: string;
268
+ middleware: Endpoint;
269
+ }[];
270
+ hooks?: {
271
+ before?: {
272
+ matcher: (context: HookEndpointContext) => boolean;
273
+ handler: (context: HookEndpointContext) => Promise<void | {
274
+ context: Partial<HookEndpointContext>;
275
+ }>;
276
+ }[];
277
+ after?: {
278
+ matcher: (context: HookEndpointContext) => boolean;
279
+ handler: (context: HookEndpointContext & {
280
+ returned: EndpointResponse;
281
+ }) => Promise<void | {
282
+ response: EndpointResponse;
283
+ }>;
284
+ }[];
285
+ };
286
+ /**
287
+ * Schema the plugin needs
288
+ *
289
+ * This will also be used to migrate the database. If the fields are dynamic from the plugins
290
+ * configuration each time the configuration is changed a new migration will be created.
291
+ *
292
+ * NOTE: If you want to create migrations manually using
293
+ * migrations option or any other way you
294
+ * can disable migration per table basis.
295
+ *
296
+ * @example
297
+ * ```ts
298
+ * schema: {
299
+ * user: {
300
+ * fields: {
301
+ * email: {
302
+ * type: "string",
303
+ * },
304
+ * emailVerified: {
305
+ * type: "boolean",
306
+ * defaultValue: false,
307
+ * },
308
+ * },
309
+ * }
310
+ * } as PluginSchema
311
+ * ```
312
+ */
313
+ schema?: PluginSchema;
314
+ /**
315
+ * The migrations of the plugin. If you define schema that will automatically create
316
+ * migrations for you.
317
+ *
318
+ * ⚠️ Only uses this if you dont't want to use the schema option and you disabled migrations for
319
+ * the tables.
320
+ */
321
+ migrations?: Record<string, Migration>;
322
+ /**
323
+ * The options of the plugin
324
+ */
325
+ options?: Record<string, any>;
326
+ };
327
+
328
+ interface BetterAuthOptions {
329
+ /**
330
+ * Base URL for the better auth. This is typically the
331
+ * root URL where your
332
+ * application server is hosted. If not explicitly set,
333
+ * the system will check the following environment variable:
334
+ *
335
+ * process.env.BETTER_AUTH_URL
336
+ *
337
+ * If not set it will throw an error.
338
+ */
339
+ baseURL?: string;
340
+ /**
341
+ * Base path for the better auth. This is typically the path where the
342
+ * better auth routes are mounted.
343
+ *
344
+ * @default "/api/auth"
345
+ */
346
+ basePath?: string;
347
+ /**
348
+ * The secret to use for encryption,
349
+ * signing and hashing.
350
+ *
351
+ * By default better auth will look for
352
+ * the following environment variables:
353
+ * process.env.BETTER_AUTH_SECRET,
354
+ * process.env.AUTH_SECRET
355
+ * If none of these environment
356
+ * variables are set,
357
+ * it will default to
358
+ * "better-auth-secret-123456789".
359
+ *
360
+ * on production if it's not set
361
+ * it will throw an error.
362
+ *
363
+ * you can generate a good secret
364
+ * using the following command:
365
+ * @example
366
+ * ```bash
367
+ * openssl rand -base64 32
368
+ * ```
369
+ */
370
+ secret?: string;
371
+ /**
372
+ * list of social providers
373
+ */
374
+ socialProvider?: OAuthProvider[];
375
+ /**
376
+ * Plugins
377
+ */
378
+ plugins?: BetterAuthPlugin[];
379
+ /**
380
+ * Advanced options
381
+ */
382
+ advanced?: {
383
+ /**
384
+ * Use secure cookies
385
+ *
386
+ * @default false
387
+ */
388
+ useSecureCookies?: boolean;
389
+ /**
390
+ * Disable CSRF check
391
+ */
392
+ disableCSRFCheck?: boolean;
393
+ };
394
+ /**
395
+ * Disable logging
396
+ *
397
+ * @default false
398
+ */
399
+ disableLog?: boolean;
400
+ /**
401
+ * Database configuration
402
+ */
403
+ database: {
404
+ provider: "postgres" | "sqlite" | "mysql";
405
+ url: string;
406
+ } | Dialect;
407
+ /**
408
+ * User configuration
409
+ */
410
+ user?: {
411
+ /**
412
+ * The model name for the user. Defaults to "user".
413
+ */
414
+ modelName?: string;
415
+ /**
416
+ * Additional fields to add to the user model
417
+ */
418
+ additionalFields?: Record<string, FieldAttribute>;
419
+ };
420
+ session?: {
421
+ modelName?: string;
422
+ /**
423
+ * Expiration time for the session token. The value
424
+ * should be in seconds.
425
+ * @default 7 days (60 * 60 * 24 * 7)
426
+ */
427
+ expiresIn?: number;
428
+ /**
429
+ * How often the session should be refreshed. The value
430
+ * should be in seconds.
431
+ * If set 0 the session will be refreshed every time it is used.
432
+ * @default 1 day (60 * 60 * 24)
433
+ */
434
+ updateAge?: number;
435
+ };
436
+ account?: {
437
+ modelName?: string;
438
+ };
439
+ /**
440
+ * Email and password authentication
441
+ */
442
+ emailAndPassword?: {
443
+ /**
444
+ * Enable email and password authentication
445
+ *
446
+ * @default false
447
+ */
448
+ enabled: boolean;
449
+ /**
450
+ * The maximum length of the password.
451
+ *
452
+ * @default 8
453
+ */
454
+ maxPasswordLength?: number;
455
+ /**
456
+ * The minimum length of the password.
457
+ *
458
+ * @default 32
459
+ */
460
+ minPasswordLength?: number;
461
+ /**
462
+ * send reset password email
463
+ *
464
+ * @param token the token to send to the email. Make sure to include the token as a
465
+ * parameter in the URL. You'll need to send it back to reset the password.
466
+ * @param user the user to send the email to
467
+ */
468
+ sendResetPasswordToken?: (token: string, user: User) => Promise<void>;
469
+ /**
470
+ * @param email the email to send the verification email to
471
+ * @param url the url to send the verification
472
+ * email to
473
+ * @param token the actual token. You can use this
474
+ * if you want to custom endpoint to verify the
475
+ * email.
476
+ */
477
+ sendVerificationEmail?: (email: string, url: string, token: string) => Promise<void>;
478
+ /**
479
+ * Send a verification email automatically after
480
+ * sign up
481
+ *
482
+ * @default false
483
+ */
484
+ sendEmailVerificationOnSignUp?: boolean;
485
+ /**
486
+ * Password hashing and verification
487
+ *
488
+ * By default Argon2id is used for password hashing and verification.
489
+ * You can provide your own hashing and verification function
490
+ * if you want to use a different algorithm.
491
+ */
492
+ password?: {
493
+ hash?: (password: string) => Promise<string>;
494
+ verify?: (password: string, hash: string) => Promise<boolean>;
495
+ };
496
+ };
497
+ /**
498
+ * List of trusted origins.
499
+ */
500
+ trustedOrigins?: string[];
501
+ }
502
+
503
+ declare const createInternalAdapter: (adapter: Adapter, options: BetterAuthOptions) => {
504
+ createOAuthUser: (user: User, account: Account) => Promise<{
505
+ user: {
506
+ id: string;
507
+ email: string;
508
+ emailVerified: boolean;
509
+ name: string;
510
+ createdAt: Date;
511
+ updatedAt: Date;
512
+ image?: string | undefined;
513
+ };
514
+ account: {
515
+ id: string;
516
+ providerId: string;
517
+ accountId: string;
518
+ userId: string;
519
+ accessToken?: string | null | undefined;
520
+ refreshToken?: string | null | undefined;
521
+ idToken?: string | null | undefined;
522
+ accessTokenExpiresAt?: Date | null | undefined;
523
+ refreshTokenExpiresAt?: Date | null | undefined;
524
+ password?: string | null | undefined;
525
+ };
526
+ } | null>;
527
+ createUser: (user: User) => Promise<{
528
+ id: string;
529
+ email: string;
530
+ emailVerified: boolean;
531
+ name: string;
532
+ createdAt: Date;
533
+ updatedAt: Date;
534
+ image?: string | undefined;
535
+ }>;
536
+ createSession: (userId: string, request?: Request, dontRememberMe?: boolean) => Promise<{
537
+ id: string;
538
+ userId: string;
539
+ expiresAt: Date;
540
+ ipAddress?: string | undefined;
541
+ userAgent?: string | undefined;
542
+ }>;
543
+ findSession: (sessionId: string) => Promise<{
544
+ session: {
545
+ id: string;
546
+ userId: string;
547
+ expiresAt: Date;
548
+ ipAddress?: string | undefined;
549
+ userAgent?: string | undefined;
550
+ };
551
+ user: {
552
+ id: string;
553
+ email: string;
554
+ emailVerified: boolean;
555
+ name: string;
556
+ createdAt: Date;
557
+ updatedAt: Date;
558
+ image?: string | undefined;
559
+ };
560
+ } | null>;
561
+ updateSession: (sessionId: string, session: Partial<Session>) => Promise<{
562
+ id: string;
563
+ userId: string;
564
+ expiresAt: Date;
565
+ ipAddress?: string | undefined;
566
+ userAgent?: string | undefined;
567
+ } | null>;
568
+ deleteSession: (id: string) => Promise<void>;
569
+ findUserByEmail: (email: string) => Promise<{
570
+ user: {
571
+ id: string;
572
+ email: string;
573
+ emailVerified: boolean;
574
+ name: string;
575
+ createdAt: Date;
576
+ updatedAt: Date;
577
+ image?: string | undefined;
578
+ };
579
+ accounts: {
580
+ id: string;
581
+ providerId: string;
582
+ accountId: string;
583
+ userId: string;
584
+ accessToken?: string | null | undefined;
585
+ refreshToken?: string | null | undefined;
586
+ idToken?: string | null | undefined;
587
+ accessTokenExpiresAt?: Date | null | undefined;
588
+ refreshTokenExpiresAt?: Date | null | undefined;
589
+ password?: string | null | undefined;
590
+ }[];
591
+ } | null>;
592
+ findUserById: (userId: string) => Promise<{
593
+ id: string;
594
+ email: string;
595
+ emailVerified: boolean;
596
+ name: string;
597
+ createdAt: Date;
598
+ updatedAt: Date;
599
+ image?: string | undefined;
600
+ } | null>;
601
+ linkAccount: (account: Account) => Promise<{
602
+ id: string;
603
+ providerId: string;
604
+ accountId: string;
605
+ userId: string;
606
+ accessToken?: string | null | undefined;
607
+ refreshToken?: string | null | undefined;
608
+ idToken?: string | null | undefined;
609
+ accessTokenExpiresAt?: Date | null | undefined;
610
+ refreshTokenExpiresAt?: Date | null | undefined;
611
+ password?: string | null | undefined;
612
+ }>;
613
+ updateUserByEmail: (email: string, data: Partial<User & Record<string, any>>) => Promise<{
614
+ id: string;
615
+ email: string;
616
+ emailVerified: boolean;
617
+ name: string;
618
+ createdAt: Date;
619
+ updatedAt: Date;
620
+ image?: string | undefined;
621
+ } | null>;
622
+ updatePassword: (userId: string, password: string) => Promise<{
623
+ id: string;
624
+ providerId: string;
625
+ accountId: string;
626
+ userId: string;
627
+ accessToken?: string | null | undefined;
628
+ refreshToken?: string | null | undefined;
629
+ idToken?: string | null | undefined;
630
+ accessTokenExpiresAt?: Date | null | undefined;
631
+ refreshTokenExpiresAt?: Date | null | undefined;
632
+ password?: string | null | undefined;
633
+ } | null>;
634
+ };
635
+ type InternalAdapter = ReturnType<typeof createInternalAdapter>;
636
+
637
+ export { type AuthContext as A, type BetterAuthOptions as B, type FieldAttribute as F, type HookEndpointContext as H, type InferFieldOutput as I, type BetterAuthPlugin as a, type InternalAdapter as b, createInternalAdapter as c, createFieldAttribute as d, type FieldType as e, type InferValueType as f, type FieldAttributeConfig as g };
package/dist/next-js.d.ts CHANGED
@@ -1,14 +1,24 @@
1
- import { A as Auth } from './index-CGeV0d2g.js';
2
- import 'better-call';
3
- import 'zod';
4
- import './helper-B5_2Vzba.js';
5
- import './schema-BOszzrbQ.js';
6
- import 'arctic';
1
+ import { Auth } from './index.js';
2
+ import { NextResponse } from 'next/server';
3
+ import './helper-D8dhRz72.js';
4
+ import './internal-adapter-CVKQ4XR9.js';
7
5
  import 'kysely';
6
+ import './index-CcxejJTH.js';
7
+ import 'arctic';
8
+ import 'zod';
9
+ import 'better-call';
8
10
 
9
11
  declare function toNextJsHandler(auth: Auth | Auth["handler"]): {
10
12
  GET: (request: Request) => Promise<Response>;
11
13
  POST: (request: Request) => Promise<Response>;
12
14
  };
15
+ /**
16
+ * Middleware that checks if the user is authenticated.
17
+ * If not, it redirects to the redirectTo URL.
18
+ */
19
+ declare function authMiddleware(options: {
20
+ baePath?: string;
21
+ redirectTo: string;
22
+ }): (request: Request) => Promise<NextResponse<unknown>>;
13
23
 
14
- export { toNextJsHandler };
24
+ export { authMiddleware, toNextJsHandler };
package/dist/next-js.js CHANGED
@@ -1,3 +1,6 @@
1
+ import { betterFetch } from '@better-fetch/fetch';
2
+ import { NextResponse } from 'next/server';
3
+
1
4
  // src/integrations/next-js.ts
2
5
  function toNextJsHandler(auth) {
3
6
  const handler = async (request) => {
@@ -8,7 +11,21 @@ function toNextJsHandler(auth) {
8
11
  POST: handler
9
12
  };
10
13
  }
11
- export {
12
- toNextJsHandler
13
- };
14
+ function authMiddleware(options) {
15
+ return async (request) => {
16
+ const url = new URL(request.url).origin;
17
+ const basePath = options?.baePath || "/api/auth";
18
+ const fullURL = `${url}${basePath}/session`;
19
+ const res = await betterFetch(fullURL, {
20
+ headers: request.headers
21
+ });
22
+ if (!res.data) {
23
+ return NextResponse.redirect(new URL(options.redirectTo, url));
24
+ }
25
+ return NextResponse.next();
26
+ };
27
+ }
28
+
29
+ export { authMiddleware, toNextJsHandler };
30
+ //# sourceMappingURL=next-js.js.map
14
31
  //# sourceMappingURL=next-js.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/integrations/next-js.ts"],"sourcesContent":["import type { Auth } from \"../auth\";\n\nexport function toNextJsHandler(auth: Auth | Auth[\"handler\"]) {\n\tconst handler = async (request: Request) => {\n\t\treturn \"handler\" in auth ? auth.handler(request) : auth(request);\n\t};\n\treturn {\n\t\tGET: handler,\n\t\tPOST: handler,\n\t};\n}\n"],"mappings":";AAEO,SAAS,gBAAgB,MAA8B;AAC7D,QAAM,UAAU,OAAO,YAAqB;AAC3C,WAAO,aAAa,OAAO,KAAK,QAAQ,OAAO,IAAI,KAAK,OAAO;AAAA,EAChE;AACA,SAAO;AAAA,IACN,KAAK;AAAA,IACL,MAAM;AAAA,EACP;AACD;","names":[]}
1
+ {"version":3,"sources":["../src/integrations/next-js.ts"],"names":[],"mappings":";;;;AAKO,SAAS,gBAAgB,IAA8B,EAAA;AAC7D,EAAM,MAAA,OAAA,GAAU,OAAO,OAAqB,KAAA;AAC3C,IAAA,OAAO,aAAa,IAAO,GAAA,IAAA,CAAK,QAAQ,OAAO,CAAA,GAAI,KAAK,OAAO,CAAA,CAAA;AAAA,GAChE,CAAA;AACA,EAAO,OAAA;AAAA,IACN,GAAK,EAAA,OAAA;AAAA,IACL,IAAM,EAAA,OAAA;AAAA,GACP,CAAA;AACD,CAAA;AAMO,SAAS,eAAe,OAG5B,EAAA;AACF,EAAA,OAAO,OAAO,OAAqB,KAAA;AAClC,IAAA,MAAM,GAAM,GAAA,IAAI,GAAI,CAAA,OAAA,CAAQ,GAAG,CAAE,CAAA,MAAA,CAAA;AACjC,IAAM,MAAA,QAAA,GAAW,SAAS,OAAW,IAAA,WAAA,CAAA;AACrC,IAAA,MAAM,OAAU,GAAA,CAAA,EAAG,GAAG,CAAA,EAAG,QAAQ,CAAA,QAAA,CAAA,CAAA;AACjC,IAAM,MAAA,GAAA,GAAM,MAAM,WAAA,CAEf,OAAS,EAAA;AAAA,MACX,SAAS,OAAQ,CAAA,OAAA;AAAA,KACjB,CAAA,CAAA;AACD,IAAI,IAAA,CAAC,IAAI,IAAM,EAAA;AACd,MAAA,OAAO,aAAa,QAAS,CAAA,IAAI,IAAI,OAAQ,CAAA,UAAA,EAAY,GAAG,CAAC,CAAA,CAAA;AAAA,KAC9D;AACA,IAAA,OAAO,aAAa,IAAK,EAAA,CAAA;AAAA,GAC1B,CAAA;AACD","file":"next-js.js","sourcesContent":["import { betterFetch } from \"@better-fetch/fetch\";\nimport type { Auth } from \"../auth\";\nimport type { Session } from \"../adapters/schema\";\nimport { NextRequest, NextResponse } from \"next/server\";\n\nexport function toNextJsHandler(auth: Auth | Auth[\"handler\"]) {\n\tconst handler = async (request: Request) => {\n\t\treturn \"handler\" in auth ? auth.handler(request) : auth(request);\n\t};\n\treturn {\n\t\tGET: handler,\n\t\tPOST: handler,\n\t};\n}\n\n/**\n * Middleware that checks if the user is authenticated.\n * If not, it redirects to the redirectTo URL.\n */\nexport function authMiddleware(options: {\n\tbaePath?: string;\n\tredirectTo: string;\n}) {\n\treturn async (request: Request) => {\n\t\tconst url = new URL(request.url).origin;\n\t\tconst basePath = options?.baePath || \"/api/auth\";\n\t\tconst fullURL = `${url}${basePath}/session`;\n\t\tconst res = await betterFetch<{\n\t\t\tsession: Session;\n\t\t}>(fullURL, {\n\t\t\theaders: request.headers,\n\t\t});\n\t\tif (!res.data) {\n\t\t\treturn NextResponse.redirect(new URL(options.redirectTo, url));\n\t\t}\n\t\treturn NextResponse.next();\n\t};\n}\n"]}