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
package/dist/social.js CHANGED
@@ -1,7 +1,10 @@
1
+ import { Discord, Facebook, GitHub, Google, Spotify, Twitch, Twitter } from 'arctic';
2
+ import { parseJWT } from 'oslo/jwt';
3
+ import { betterFetch } from '@better-fetch/fetch';
4
+ import { createOAuth2Request, sendTokenRequest } from 'arctic/dist/request';
5
+ import { createConsola } from 'consola';
6
+
1
7
  // src/social-providers/apple.ts
2
- import "arctic";
3
- import { parseJWT } from "oslo/jwt";
4
- import { betterFetch } from "@better-fetch/fetch";
5
8
 
6
9
  // src/error/better-auth-error.ts
7
10
  var BetterAuthError = class extends Error {
@@ -23,41 +26,44 @@ function checkHasPath(url) {
23
26
  function withPath(url, path = "/api/auth") {
24
27
  const hasPath = checkHasPath(url);
25
28
  if (hasPath) {
26
- return {
27
- baseURL: new URL(url).origin,
28
- withPath: url
29
- };
29
+ return url;
30
30
  }
31
31
  path = path.startsWith("/") ? path : `/${path}`;
32
- return {
33
- baseURL: url,
34
- withPath: `${url}${path}`
35
- };
32
+ return `${url}${path}`;
36
33
  }
37
34
  function getBaseURL(url, path) {
38
- if (url) {
39
- return withPath(url, path);
40
- }
41
35
  const env = typeof process !== "undefined" ? process.env : {};
42
- const fromEnv = env.BETTER_AUTH_URL || env.AUTH_URL || env.NEXT_PUBLIC_AUTH_URL || env.NEXT_PUBLIC_BETTER_AUTH_URL || env.PUBLIC_AUTH_URL || env.PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_AUTH_URL;
36
+ const fromEnv = env.BETTER_AUTH_URL || env.NEXT_PUBLIC_BETTER_AUTH_URL || env.PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_AUTH_URL;
43
37
  if (fromEnv) {
44
38
  return withPath(fromEnv, path);
45
39
  }
46
- const isDev = !fromEnv && (env.NODE_ENV === "development" || env.NODE_ENV === "test");
47
- if (isDev) {
48
- return {
49
- baseURL: "http://localhost:3000",
50
- withPath: "http://localhost:3000/api/auth"
51
- };
40
+ if (typeof window !== "undefined") {
41
+ return withPath(window.location.origin, path);
52
42
  }
53
- throw new BetterAuthError(
54
- "Could not infer baseURL from environment variables"
55
- );
43
+ return void 0;
56
44
  }
57
45
 
58
46
  // src/social-providers/utils.ts
59
47
  function getRedirectURI(providerId, redirectURI) {
60
- return redirectURI || `${getBaseURL()}/api/auth/callback/${providerId}`;
48
+ return redirectURI || `${getBaseURL()}/callback/${providerId}`;
49
+ }
50
+ async function validateAuthorizationCode({
51
+ code,
52
+ codeVerifier,
53
+ redirectURI,
54
+ options,
55
+ tokenEndpoint
56
+ }) {
57
+ const body = new URLSearchParams();
58
+ body.set("grant_type", "authorization_code");
59
+ body.set("code", code);
60
+ body.set("code_verifier", codeVerifier || "");
61
+ body.set("redirect_uri", redirectURI);
62
+ body.set("client_id", options.clientId);
63
+ body.set("client_secret", options.clientSecret);
64
+ const request = createOAuth2Request(tokenEndpoint, body);
65
+ const tokens = await sendTokenRequest(request);
66
+ return tokens;
61
67
  }
62
68
 
63
69
  // src/social-providers/apple.ts
@@ -114,19 +120,11 @@ var apple = ({
114
120
  }
115
121
  };
116
122
  };
117
-
118
- // src/social-providers/discord.ts
119
- import { betterFetch as betterFetch2 } from "@better-fetch/fetch";
120
- import { Discord } from "arctic";
121
- var discord = ({
122
- clientId,
123
- clientSecret,
124
- redirectURI
125
- }) => {
123
+ var discord = (options) => {
126
124
  const discordArctic = new Discord(
127
- clientId,
128
- clientSecret,
129
- getRedirectURI("discord", redirectURI)
125
+ options.clientId,
126
+ options.clientSecret,
127
+ getRedirectURI("discord", options.redirectURI)
130
128
  );
131
129
  return {
132
130
  id: "discord",
@@ -135,14 +133,22 @@ var discord = ({
135
133
  const _scope = scopes || ["email"];
136
134
  return discordArctic.createAuthorizationURL(state, _scope);
137
135
  },
138
- validateAuthorizationCode: discordArctic.validateAuthorizationCode,
136
+ validateAuthorizationCode: async (code, codeVerifier, redirectURI) => {
137
+ return validateAuthorizationCode({
138
+ code,
139
+ codeVerifier,
140
+ redirectURI: redirectURI || getRedirectURI("discord", options.redirectURI),
141
+ options,
142
+ tokenEndpoint: "https://discord.com/api/oauth2/token"
143
+ });
144
+ },
139
145
  async getUserInfo(token) {
140
- const { data: profile, error } = await betterFetch2(
146
+ const { data: profile, error } = await betterFetch(
141
147
  "https://discord.com/api/users/@me",
142
148
  {
143
149
  auth: {
144
150
  type: "Bearer",
145
- token: token.accessToken
151
+ token: token.accessToken()
146
152
  }
147
153
  }
148
154
  );
@@ -161,19 +167,11 @@ var discord = ({
161
167
  }
162
168
  };
163
169
  };
164
-
165
- // src/social-providers/facebook.ts
166
- import { betterFetch as betterFetch3 } from "@better-fetch/fetch";
167
- import { Facebook } from "arctic";
168
- var facebook = ({
169
- clientId,
170
- clientSecret,
171
- redirectURI
172
- }) => {
170
+ var facebook = (options) => {
173
171
  const facebookArctic = new Facebook(
174
- clientId,
175
- clientSecret,
176
- getRedirectURI("facebook", redirectURI)
172
+ options.clientId,
173
+ options.clientSecret,
174
+ getRedirectURI("facebook", options.redirectURI)
177
175
  );
178
176
  return {
179
177
  id: "facebook",
@@ -182,14 +180,22 @@ var facebook = ({
182
180
  const _scopes = scopes || ["email", "public_profile"];
183
181
  return facebookArctic.createAuthorizationURL(state, _scopes);
184
182
  },
185
- validateAuthorizationCode: facebookArctic.validateAuthorizationCode,
183
+ validateAuthorizationCode: async (code, codeVerifier, redirectURI) => {
184
+ return validateAuthorizationCode({
185
+ code,
186
+ codeVerifier,
187
+ redirectURI: redirectURI || getRedirectURI("facebook", options.redirectURI),
188
+ options,
189
+ tokenEndpoint: "https://graph.facebook.com/v16.0/oauth/access_token"
190
+ });
191
+ },
186
192
  async getUserInfo(token) {
187
- const { data: profile, error } = await betterFetch3(
193
+ const { data: profile, error } = await betterFetch(
188
194
  "https://graph.facebook.com/me",
189
195
  {
190
196
  auth: {
191
197
  type: "Bearer",
192
- token: token.accessToken
198
+ token: token.accessToken()
193
199
  }
194
200
  }
195
201
  );
@@ -208,10 +214,6 @@ var facebook = ({
208
214
  }
209
215
  };
210
216
  };
211
-
212
- // src/social-providers/github.ts
213
- import { betterFetch as betterFetch4 } from "@better-fetch/fetch";
214
- import { GitHub } from "arctic";
215
217
  var github = ({
216
218
  clientId,
217
219
  clientSecret,
@@ -229,14 +231,17 @@ var github = ({
229
231
  const _scopes = scopes || ["user:email"];
230
232
  return githubArctic.createAuthorizationURL(state, _scopes);
231
233
  },
232
- validateAuthorizationCode: githubArctic.validateAuthorizationCode,
234
+ validateAuthorizationCode: async (state) => {
235
+ return await githubArctic.validateAuthorizationCode(state);
236
+ },
233
237
  async getUserInfo(token) {
234
- const { data: profile, error } = await betterFetch4(
238
+ console.log(`Bearer ${token.accessToken()}`);
239
+ const { data: profile, error } = await betterFetch(
235
240
  "https://api.github.com/user",
236
241
  {
237
- method: "GET",
238
- headers: {
239
- Authorization: `Bearer ${token.accessToken}`
242
+ auth: {
243
+ type: "Bearer",
244
+ token: token.accessToken()
240
245
  }
241
246
  }
242
247
  );
@@ -245,10 +250,10 @@ var github = ({
245
250
  }
246
251
  let emailVerified = false;
247
252
  if (!profile.email) {
248
- const { data, error: error2 } = await betterFetch4("https://api.github.com/user/emails", {
249
- headers: {
250
- Authorization: `Bearer ${token.accessToken}`,
251
- "User-Agent": "better-auth"
253
+ const { data, error: error2 } = await betterFetch("https://api.github.com/user/emails", {
254
+ auth: {
255
+ type: "Bearer",
256
+ token: token.accessToken()
252
257
  }
253
258
  });
254
259
  if (!error2) {
@@ -271,41 +276,81 @@ var github = ({
271
276
  }
272
277
  };
273
278
  };
274
-
275
- // src/social-providers/google.ts
276
- import { Google } from "arctic";
277
- import { parseJWT as parseJWT2 } from "oslo/jwt";
278
- var google = ({
279
- clientId,
280
- clientSecret,
281
- redirectURI
282
- }) => {
279
+ var consola = createConsola({
280
+ formatOptions: {
281
+ date: false,
282
+ colors: true,
283
+ compact: true
284
+ },
285
+ defaults: {
286
+ tag: "Better Auth"
287
+ }
288
+ });
289
+ var createLogger = (options) => {
290
+ return {
291
+ log: (...args) => {
292
+ consola.log("", ...args);
293
+ },
294
+ error: (...args) => {
295
+ consola.error("", ...args);
296
+ },
297
+ warn: (...args) => {
298
+ consola.warn("", ...args);
299
+ },
300
+ info: (...args) => {
301
+ consola.info("", ...args);
302
+ },
303
+ debug: (...args) => {
304
+ consola.debug("", ...args);
305
+ },
306
+ box: (...args) => {
307
+ consola.box("", ...args);
308
+ },
309
+ success: (...args) => {
310
+ consola.success("", ...args);
311
+ },
312
+ break: (...args) => {
313
+ console.log("\n");
314
+ }
315
+ };
316
+ };
317
+ var logger = createLogger();
318
+ var google = (options) => {
283
319
  const googleArctic = new Google(
284
- clientId,
285
- clientSecret,
286
- getRedirectURI("google", redirectURI)
320
+ options.clientId,
321
+ options.clientSecret,
322
+ getRedirectURI("google", options.redirectURI)
287
323
  );
288
324
  return {
289
325
  id: "google",
290
326
  name: "Google",
291
- createAuthorizationURL({ state, scopes, codeVerifier }) {
327
+ createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
328
+ if (!options.clientId || !options.clientSecret) {
329
+ logger.error(
330
+ "clientId and clientSecret is required for Google. Make sure to you have provided them in the options"
331
+ );
332
+ throw new BetterAuthError("clientId is required for Google");
333
+ }
292
334
  if (!codeVerifier) {
293
335
  throw new BetterAuthError("codeVerifier is required for Google");
294
336
  }
295
337
  const _scopes = scopes || ["email", "profile"];
296
338
  return googleArctic.createAuthorizationURL(state, codeVerifier, _scopes);
297
339
  },
298
- validateAuthorizationCode: async (code, codeVerifier) => {
299
- if (!codeVerifier) {
300
- throw new BetterAuthError("codeVerifier is required for Google");
301
- }
302
- return googleArctic.validateAuthorizationCode(code, codeVerifier);
340
+ validateAuthorizationCode: async (code, codeVerifier, redirectURI) => {
341
+ return validateAuthorizationCode({
342
+ code,
343
+ codeVerifier,
344
+ redirectURI: redirectURI || getRedirectURI("google", options.redirectURI),
345
+ options,
346
+ tokenEndpoint: "https://oauth2.googleapis.com/token"
347
+ });
303
348
  },
304
349
  async getUserInfo(token) {
305
350
  if (!token.idToken) {
306
351
  return null;
307
352
  }
308
- const user = parseJWT2(token.idToken())?.payload;
353
+ const user = parseJWT(token.idToken())?.payload;
309
354
  return {
310
355
  user: {
311
356
  id: user.sub,
@@ -319,19 +364,11 @@ var google = ({
319
364
  }
320
365
  };
321
366
  };
322
-
323
- // src/social-providers/spotify.ts
324
- import { betterFetch as betterFetch5 } from "@better-fetch/fetch";
325
- import { Spotify } from "arctic";
326
- var spotify = ({
327
- clientId,
328
- clientSecret,
329
- redirectURI
330
- }) => {
367
+ var spotify = (options) => {
331
368
  const spotifyArctic = new Spotify(
332
- clientId,
333
- clientSecret,
334
- getRedirectURI("spotify", redirectURI)
369
+ options.clientId,
370
+ options.clientSecret,
371
+ getRedirectURI("spotify", options.redirectURI)
335
372
  );
336
373
  return {
337
374
  id: "spotify",
@@ -340,14 +377,22 @@ var spotify = ({
340
377
  const _scopes = scopes || ["user-read-email"];
341
378
  return spotifyArctic.createAuthorizationURL(state, _scopes);
342
379
  },
343
- validateAuthorizationCode: spotifyArctic.validateAuthorizationCode,
380
+ validateAuthorizationCode: async (code, codeVerifier, redirectURI) => {
381
+ return validateAuthorizationCode({
382
+ code,
383
+ codeVerifier,
384
+ redirectURI: redirectURI || getRedirectURI("spotify", options.redirectURI),
385
+ options,
386
+ tokenEndpoint: "https://accounts.spotify.com/api/token"
387
+ });
388
+ },
344
389
  async getUserInfo(token) {
345
- const { data: profile, error } = await betterFetch5(
390
+ const { data: profile, error } = await betterFetch(
346
391
  "https://api.spotify.com/v1/me",
347
392
  {
348
393
  method: "GET",
349
394
  headers: {
350
- Authorization: `Bearer ${token.accessToken}`
395
+ Authorization: `Bearer ${token.accessToken()}`
351
396
  }
352
397
  }
353
398
  );
@@ -367,19 +412,11 @@ var spotify = ({
367
412
  }
368
413
  };
369
414
  };
370
-
371
- // src/social-providers/twitch.ts
372
- import { betterFetch as betterFetch6 } from "@better-fetch/fetch";
373
- import { Twitch } from "arctic";
374
- var twitch = ({
375
- clientId,
376
- clientSecret,
377
- redirectURI
378
- }) => {
415
+ var twitch = (options) => {
379
416
  const twitchArctic = new Twitch(
380
- clientId,
381
- clientSecret,
382
- getRedirectURI("twitch", redirectURI)
417
+ options.clientId,
418
+ options.clientSecret,
419
+ getRedirectURI("twitch", options.redirectURI)
383
420
  );
384
421
  return {
385
422
  id: "twitch",
@@ -388,14 +425,22 @@ var twitch = ({
388
425
  const _scopes = scopes || ["activity:write", "read"];
389
426
  return twitchArctic.createAuthorizationURL(state, _scopes);
390
427
  },
391
- validateAuthorizationCode: twitchArctic.validateAuthorizationCode,
428
+ validateAuthorizationCode: async (code, codeVerifier, redirectURI) => {
429
+ return validateAuthorizationCode({
430
+ code,
431
+ codeVerifier,
432
+ redirectURI: redirectURI || getRedirectURI("twitch", options.redirectURI),
433
+ options,
434
+ tokenEndpoint: "https://id.twitch.tv/oauth2/token"
435
+ });
436
+ },
392
437
  async getUserInfo(token) {
393
- const { data: profile, error } = await betterFetch6(
438
+ const { data: profile, error } = await betterFetch(
394
439
  "https://api.twitch.tv/helix/users",
395
440
  {
396
441
  method: "GET",
397
442
  headers: {
398
- Authorization: `Bearer ${token.accessToken}`
443
+ Authorization: `Bearer ${token.accessToken()}`
399
444
  }
400
445
  }
401
446
  );
@@ -415,19 +460,11 @@ var twitch = ({
415
460
  }
416
461
  };
417
462
  };
418
-
419
- // src/social-providers/twitter.ts
420
- import { betterFetch as betterFetch7 } from "@better-fetch/fetch";
421
- import { Twitter } from "arctic";
422
- var twitter = ({
423
- clientId,
424
- clientSecret,
425
- redirectURI
426
- }) => {
463
+ var twitter = (options) => {
427
464
  const twitterArctic = new Twitter(
428
- clientId,
429
- clientSecret,
430
- getRedirectURI("twitter", redirectURI)
465
+ options.clientId,
466
+ options.clientSecret,
467
+ getRedirectURI("twitter", options.redirectURI)
431
468
  );
432
469
  return {
433
470
  id: "twitter",
@@ -440,19 +477,22 @@ var twitter = ({
440
477
  _scopes
441
478
  );
442
479
  },
443
- validateAuthorizationCode: async (code, codeVerifier) => {
444
- if (!codeVerifier) {
445
- throw new BetterAuthError("codeVerifier is required for Twitter");
446
- }
447
- return twitterArctic.validateAuthorizationCode(code, codeVerifier);
480
+ validateAuthorizationCode: async (code, codeVerifier, redirectURI) => {
481
+ return validateAuthorizationCode({
482
+ code,
483
+ codeVerifier,
484
+ redirectURI: redirectURI || getRedirectURI("twitch", options.redirectURI),
485
+ options,
486
+ tokenEndpoint: "https://id.twitch.tv/oauth2/token"
487
+ });
448
488
  },
449
489
  async getUserInfo(token) {
450
- const { data: profile, error } = await betterFetch7(
490
+ const { data: profile, error } = await betterFetch(
451
491
  "https://api.x.com/2/users/me?user.fields=profile_image_url",
452
492
  {
453
493
  method: "GET",
454
494
  headers: {
455
- Authorization: `Bearer ${token.accessToken}`
495
+ Authorization: `Bearer ${token.accessToken()}`
456
496
  }
457
497
  }
458
498
  );
@@ -476,9 +516,6 @@ var twitter = ({
476
516
  };
477
517
  };
478
518
 
479
- // src/types/provider.ts
480
- import "arctic";
481
-
482
519
  // src/social-providers/index.ts
483
520
  var oAuthProviders = {
484
521
  apple,
@@ -491,16 +528,7 @@ var oAuthProviders = {
491
528
  twitter
492
529
  };
493
530
  var oAuthProviderList = Object.keys(oAuthProviders);
494
- export {
495
- apple,
496
- discord,
497
- facebook,
498
- github,
499
- google,
500
- oAuthProviderList,
501
- oAuthProviders,
502
- spotify,
503
- twitch,
504
- twitter
505
- };
531
+
532
+ export { apple, discord, facebook, github, google, oAuthProviderList, oAuthProviders, spotify, twitch, twitter };
533
+ //# sourceMappingURL=social.js.map
506
534
  //# sourceMappingURL=social.js.map