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/api.js
CHANGED
|
@@ -1,19 +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
|
-
|
|
16
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";
|
|
17
16
|
async function hs256(secretKey, message) {
|
|
18
17
|
const enc = new TextEncoder();
|
|
19
18
|
const algorithm = { name: "HMAC", hash: "SHA-256" };
|
|
@@ -31,6 +30,13 @@ async function hs256(secretKey, message) {
|
|
|
31
30
|
);
|
|
32
31
|
return btoa(String.fromCharCode(...new Uint8Array(signature)));
|
|
33
32
|
}
|
|
33
|
+
|
|
34
|
+
// src/api/call.ts
|
|
35
|
+
import {
|
|
36
|
+
createEndpointCreator,
|
|
37
|
+
createMiddleware,
|
|
38
|
+
createMiddlewareCreator
|
|
39
|
+
} from "better-call";
|
|
34
40
|
var optionsMiddleware = createMiddleware(async () => {
|
|
35
41
|
return {};
|
|
36
42
|
});
|
|
@@ -90,6 +96,30 @@ var csrfMiddleware = createAuthMiddleware(
|
|
|
90
96
|
}
|
|
91
97
|
);
|
|
92
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
|
+
|
|
93
123
|
// src/utils/base-url.ts
|
|
94
124
|
function checkHasPath(url) {
|
|
95
125
|
try {
|
|
@@ -110,6 +140,9 @@ function withPath(url, path = "/api/auth") {
|
|
|
110
140
|
return `${url}${path}`;
|
|
111
141
|
}
|
|
112
142
|
function getBaseURL(url, path) {
|
|
143
|
+
if (url) {
|
|
144
|
+
return withPath(url, path);
|
|
145
|
+
}
|
|
113
146
|
const env = process?.env || {};
|
|
114
147
|
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 || (env.BASE_URL !== "/" ? env.BASE_URL : void 0);
|
|
115
148
|
if (fromEnv) {
|
|
@@ -120,142 +153,9 @@ function getBaseURL(url, path) {
|
|
|
120
153
|
}
|
|
121
154
|
return void 0;
|
|
122
155
|
}
|
|
123
|
-
async function setSessionCookie(ctx, sessionToken, dontRememberMe, overrides) {
|
|
124
|
-
const options = ctx.context.authCookies.sessionToken.options;
|
|
125
|
-
options.maxAge = dontRememberMe ? void 0 : options.maxAge;
|
|
126
|
-
await ctx.setSignedCookie(
|
|
127
|
-
ctx.context.authCookies.sessionToken.name,
|
|
128
|
-
sessionToken,
|
|
129
|
-
ctx.context.secret,
|
|
130
|
-
options
|
|
131
|
-
);
|
|
132
|
-
if (dontRememberMe) {
|
|
133
|
-
await ctx.setSignedCookie(
|
|
134
|
-
ctx.context.authCookies.dontRememberToken.name,
|
|
135
|
-
"true",
|
|
136
|
-
ctx.context.secret,
|
|
137
|
-
ctx.context.authCookies.dontRememberToken.options
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
function deleteSessionCookie(ctx) {
|
|
142
|
-
ctx.setCookie(ctx.context.authCookies.sessionToken.name, "", {
|
|
143
|
-
maxAge: 0
|
|
144
|
-
});
|
|
145
|
-
ctx.setCookie(ctx.context.authCookies.dontRememberToken.name, "", {
|
|
146
|
-
maxAge: 0
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
156
|
|
|
150
|
-
// src/utils
|
|
151
|
-
|
|
152
|
-
const date = /* @__PURE__ */ new Date();
|
|
153
|
-
return new Date(date.getTime() + (unit === "sec" ? span * 1e3 : span));
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
// src/utils/get-request-ip.ts
|
|
157
|
-
function getIp(req) {
|
|
158
|
-
const testIP = "127.0.0.1";
|
|
159
|
-
if (process.env.NODE_ENV === "test") {
|
|
160
|
-
return testIP;
|
|
161
|
-
}
|
|
162
|
-
const headers = [
|
|
163
|
-
"x-client-ip",
|
|
164
|
-
"x-forwarded-for",
|
|
165
|
-
"cf-connecting-ip",
|
|
166
|
-
"fastly-client-ip",
|
|
167
|
-
"x-real-ip",
|
|
168
|
-
"x-cluster-client-ip",
|
|
169
|
-
"x-forwarded",
|
|
170
|
-
"forwarded-for",
|
|
171
|
-
"forwarded"
|
|
172
|
-
];
|
|
173
|
-
for (const header of headers) {
|
|
174
|
-
const value = req.headers.get(header);
|
|
175
|
-
if (typeof value === "string") {
|
|
176
|
-
const ip = value.split(",")[0].trim();
|
|
177
|
-
if (ip) return ip;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
return null;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// src/utils/hide-metadata.ts
|
|
184
|
-
var HIDE_METADATA = {
|
|
185
|
-
isAction: false
|
|
186
|
-
};
|
|
187
|
-
var generateId = (size) => {
|
|
188
|
-
return nanoid(size);
|
|
189
|
-
};
|
|
190
|
-
var consola = createConsola({
|
|
191
|
-
formatOptions: {
|
|
192
|
-
date: false,
|
|
193
|
-
colors: true,
|
|
194
|
-
compact: true
|
|
195
|
-
},
|
|
196
|
-
defaults: {
|
|
197
|
-
tag: "Better Auth"
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
var createLogger = (options) => {
|
|
201
|
-
return {
|
|
202
|
-
log: (...args) => {
|
|
203
|
-
consola.log("", ...args);
|
|
204
|
-
},
|
|
205
|
-
error: (...args) => {
|
|
206
|
-
consola.error("", ...args);
|
|
207
|
-
},
|
|
208
|
-
warn: (...args) => {
|
|
209
|
-
consola.warn("", ...args);
|
|
210
|
-
},
|
|
211
|
-
info: (...args) => {
|
|
212
|
-
consola.info("", ...args);
|
|
213
|
-
},
|
|
214
|
-
debug: (...args) => {
|
|
215
|
-
consola.debug("", ...args);
|
|
216
|
-
},
|
|
217
|
-
box: (...args) => {
|
|
218
|
-
consola.box("", ...args);
|
|
219
|
-
},
|
|
220
|
-
success: (...args) => {
|
|
221
|
-
consola.success("", ...args);
|
|
222
|
-
},
|
|
223
|
-
break: (...args) => {
|
|
224
|
-
console.log("\n");
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
};
|
|
228
|
-
var logger = createLogger();
|
|
229
|
-
function generateState(callbackURL, currentURL, dontRememberMe) {
|
|
230
|
-
const code = generateState$1();
|
|
231
|
-
const state = JSON.stringify({
|
|
232
|
-
code,
|
|
233
|
-
callbackURL,
|
|
234
|
-
currentURL,
|
|
235
|
-
dontRememberMe
|
|
236
|
-
});
|
|
237
|
-
return { state, code };
|
|
238
|
-
}
|
|
239
|
-
function parseState(state) {
|
|
240
|
-
const data = z.object({
|
|
241
|
-
code: z.string(),
|
|
242
|
-
callbackURL: z.string().optional(),
|
|
243
|
-
currentURL: z.string().optional(),
|
|
244
|
-
dontRememberMe: z.boolean().optional()
|
|
245
|
-
}).safeParse(JSON.parse(state));
|
|
246
|
-
return data;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// src/error/better-auth-error.ts
|
|
250
|
-
var BetterAuthError = class extends Error {
|
|
251
|
-
constructor(message, cause) {
|
|
252
|
-
super(message);
|
|
253
|
-
this.name = "BetterAuthError";
|
|
254
|
-
this.message = message;
|
|
255
|
-
this.cause = cause;
|
|
256
|
-
this.stack = "";
|
|
257
|
-
}
|
|
258
|
-
};
|
|
157
|
+
// src/social-providers/utils.ts
|
|
158
|
+
import { betterFetch } from "@better-fetch/fetch";
|
|
259
159
|
function getRedirectURI(providerId, redirectURI) {
|
|
260
160
|
return redirectURI || `${getBaseURL()}/callback/${providerId}`;
|
|
261
161
|
}
|
|
@@ -327,6 +227,9 @@ var apple = (options) => {
|
|
|
327
227
|
}
|
|
328
228
|
};
|
|
329
229
|
};
|
|
230
|
+
|
|
231
|
+
// src/social-providers/discord.ts
|
|
232
|
+
import { betterFetch as betterFetch3 } from "@better-fetch/fetch";
|
|
330
233
|
var discord = (options) => {
|
|
331
234
|
return {
|
|
332
235
|
id: "discord",
|
|
@@ -350,7 +253,7 @@ var discord = (options) => {
|
|
|
350
253
|
});
|
|
351
254
|
},
|
|
352
255
|
async getUserInfo(token) {
|
|
353
|
-
const { data: profile, error: error2 } = await
|
|
256
|
+
const { data: profile, error: error2 } = await betterFetch3(
|
|
354
257
|
"https://discord.com/api/users/@me",
|
|
355
258
|
{
|
|
356
259
|
headers: {
|
|
@@ -381,6 +284,10 @@ var discord = (options) => {
|
|
|
381
284
|
}
|
|
382
285
|
};
|
|
383
286
|
};
|
|
287
|
+
|
|
288
|
+
// src/social-providers/facebook.ts
|
|
289
|
+
import { betterFetch as betterFetch4 } from "@better-fetch/fetch";
|
|
290
|
+
import { Facebook } from "arctic";
|
|
384
291
|
var facebook = (options) => {
|
|
385
292
|
const facebookArctic = new Facebook(
|
|
386
293
|
options.clientId,
|
|
@@ -404,7 +311,7 @@ var facebook = (options) => {
|
|
|
404
311
|
});
|
|
405
312
|
},
|
|
406
313
|
async getUserInfo(token) {
|
|
407
|
-
const { data: profile, error: error2 } = await
|
|
314
|
+
const { data: profile, error: error2 } = await betterFetch4(
|
|
408
315
|
"https://graph.facebook.com/me",
|
|
409
316
|
{
|
|
410
317
|
auth: {
|
|
@@ -428,6 +335,10 @@ var facebook = (options) => {
|
|
|
428
335
|
}
|
|
429
336
|
};
|
|
430
337
|
};
|
|
338
|
+
|
|
339
|
+
// src/social-providers/github.ts
|
|
340
|
+
import { betterFetch as betterFetch5 } from "@better-fetch/fetch";
|
|
341
|
+
import { GitHub } from "arctic";
|
|
431
342
|
var github = ({
|
|
432
343
|
clientId,
|
|
433
344
|
clientSecret,
|
|
@@ -449,7 +360,7 @@ var github = ({
|
|
|
449
360
|
return await githubArctic.validateAuthorizationCode(state);
|
|
450
361
|
},
|
|
451
362
|
async getUserInfo(token) {
|
|
452
|
-
const { data: profile, error: error2 } = await
|
|
363
|
+
const { data: profile, error: error2 } = await betterFetch5(
|
|
453
364
|
"https://api.github.com/user",
|
|
454
365
|
{
|
|
455
366
|
auth: {
|
|
@@ -463,7 +374,7 @@ var github = ({
|
|
|
463
374
|
}
|
|
464
375
|
let emailVerified = false;
|
|
465
376
|
if (!profile.email) {
|
|
466
|
-
const { data, error: error3 } = await
|
|
377
|
+
const { data, error: error3 } = await betterFetch5("https://api.github.com/user/emails", {
|
|
467
378
|
auth: {
|
|
468
379
|
type: "Bearer",
|
|
469
380
|
token: token.accessToken()
|
|
@@ -489,6 +400,54 @@ var github = ({
|
|
|
489
400
|
}
|
|
490
401
|
};
|
|
491
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
|
|
492
451
|
var google = (options) => {
|
|
493
452
|
const googleArctic = new Google(
|
|
494
453
|
options.clientId,
|
|
@@ -529,7 +488,7 @@ var google = (options) => {
|
|
|
529
488
|
if (!token.idToken) {
|
|
530
489
|
return null;
|
|
531
490
|
}
|
|
532
|
-
const user =
|
|
491
|
+
const user = parseJWT2(token.idToken())?.payload;
|
|
533
492
|
return {
|
|
534
493
|
user: {
|
|
535
494
|
id: user.sub,
|
|
@@ -543,6 +502,10 @@ var google = (options) => {
|
|
|
543
502
|
}
|
|
544
503
|
};
|
|
545
504
|
};
|
|
505
|
+
|
|
506
|
+
// src/social-providers/spotify.ts
|
|
507
|
+
import { betterFetch as betterFetch6 } from "@better-fetch/fetch";
|
|
508
|
+
import { Spotify } from "arctic";
|
|
546
509
|
var spotify = (options) => {
|
|
547
510
|
const spotifyArctic = new Spotify(
|
|
548
511
|
options.clientId,
|
|
@@ -566,7 +529,7 @@ var spotify = (options) => {
|
|
|
566
529
|
});
|
|
567
530
|
},
|
|
568
531
|
async getUserInfo(token) {
|
|
569
|
-
const { data: profile, error: error2 } = await
|
|
532
|
+
const { data: profile, error: error2 } = await betterFetch6(
|
|
570
533
|
"https://api.spotify.com/v1/me",
|
|
571
534
|
{
|
|
572
535
|
method: "GET",
|
|
@@ -591,6 +554,10 @@ var spotify = (options) => {
|
|
|
591
554
|
}
|
|
592
555
|
};
|
|
593
556
|
};
|
|
557
|
+
|
|
558
|
+
// src/social-providers/twitch.ts
|
|
559
|
+
import { betterFetch as betterFetch7 } from "@better-fetch/fetch";
|
|
560
|
+
import { Twitch } from "arctic";
|
|
594
561
|
var twitch = (options) => {
|
|
595
562
|
const twitchArctic = new Twitch(
|
|
596
563
|
options.clientId,
|
|
@@ -613,7 +580,7 @@ var twitch = (options) => {
|
|
|
613
580
|
});
|
|
614
581
|
},
|
|
615
582
|
async getUserInfo(token) {
|
|
616
|
-
const { data: profile, error: error2 } = await
|
|
583
|
+
const { data: profile, error: error2 } = await betterFetch7(
|
|
617
584
|
"https://api.twitch.tv/helix/users",
|
|
618
585
|
{
|
|
619
586
|
method: "GET",
|
|
@@ -638,6 +605,10 @@ var twitch = (options) => {
|
|
|
638
605
|
}
|
|
639
606
|
};
|
|
640
607
|
};
|
|
608
|
+
|
|
609
|
+
// src/social-providers/twitter.ts
|
|
610
|
+
import { betterFetch as betterFetch8 } from "@better-fetch/fetch";
|
|
611
|
+
import { Twitter } from "arctic";
|
|
641
612
|
var twitter = (options) => {
|
|
642
613
|
const twitterArctic = new Twitter(
|
|
643
614
|
options.clientId,
|
|
@@ -665,7 +636,7 @@ var twitter = (options) => {
|
|
|
665
636
|
});
|
|
666
637
|
},
|
|
667
638
|
async getUserInfo(token) {
|
|
668
|
-
const { data: profile, error: error2 } = await
|
|
639
|
+
const { data: profile, error: error2 } = await betterFetch8(
|
|
669
640
|
"https://api.x.com/2/users/me?user.fields=profile_image_url",
|
|
670
641
|
{
|
|
671
642
|
method: "GET",
|
|
@@ -694,6 +665,9 @@ var twitter = (options) => {
|
|
|
694
665
|
};
|
|
695
666
|
};
|
|
696
667
|
|
|
668
|
+
// src/types/provider.ts
|
|
669
|
+
import "arctic";
|
|
670
|
+
|
|
697
671
|
// src/social-providers/index.ts
|
|
698
672
|
var oAuthProviders = {
|
|
699
673
|
apple,
|
|
@@ -706,6 +680,99 @@ var oAuthProviders = {
|
|
|
706
680
|
twitter
|
|
707
681
|
};
|
|
708
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
|
+
async function setSessionCookie(ctx, sessionToken, dontRememberMe, overrides) {
|
|
719
|
+
const options = ctx.context.authCookies.sessionToken.options;
|
|
720
|
+
options.maxAge = dontRememberMe ? void 0 : options.maxAge;
|
|
721
|
+
await ctx.setSignedCookie(
|
|
722
|
+
ctx.context.authCookies.sessionToken.name,
|
|
723
|
+
sessionToken,
|
|
724
|
+
ctx.context.secret,
|
|
725
|
+
options
|
|
726
|
+
);
|
|
727
|
+
if (dontRememberMe) {
|
|
728
|
+
await ctx.setSignedCookie(
|
|
729
|
+
ctx.context.authCookies.dontRememberToken.name,
|
|
730
|
+
"true",
|
|
731
|
+
ctx.context.secret,
|
|
732
|
+
ctx.context.authCookies.dontRememberToken.options
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
function deleteSessionCookie(ctx) {
|
|
737
|
+
ctx.setCookie(ctx.context.authCookies.sessionToken.name, "", {
|
|
738
|
+
maxAge: 0
|
|
739
|
+
});
|
|
740
|
+
ctx.setCookie(ctx.context.authCookies.dontRememberToken.name, "", {
|
|
741
|
+
maxAge: 0
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
// src/api/routes/session.ts
|
|
746
|
+
import { z as z3 } from "zod";
|
|
747
|
+
|
|
748
|
+
// src/utils/get-request-ip.ts
|
|
749
|
+
function getIp(req) {
|
|
750
|
+
const testIP = "127.0.0.1";
|
|
751
|
+
if (process.env.NODE_ENV === "test") {
|
|
752
|
+
return testIP;
|
|
753
|
+
}
|
|
754
|
+
const headers = [
|
|
755
|
+
"x-client-ip",
|
|
756
|
+
"x-forwarded-for",
|
|
757
|
+
"cf-connecting-ip",
|
|
758
|
+
"fastly-client-ip",
|
|
759
|
+
"x-real-ip",
|
|
760
|
+
"x-cluster-client-ip",
|
|
761
|
+
"x-forwarded",
|
|
762
|
+
"forwarded-for",
|
|
763
|
+
"forwarded"
|
|
764
|
+
];
|
|
765
|
+
for (const header of headers) {
|
|
766
|
+
const value = req.headers.get(header);
|
|
767
|
+
if (typeof value === "string") {
|
|
768
|
+
const ip = value.split(",")[0].trim();
|
|
769
|
+
if (ip) return ip;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
return null;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// src/api/routes/session.ts
|
|
709
776
|
function getRequestUniqueKey(ctx, token) {
|
|
710
777
|
if (!ctx.request) {
|
|
711
778
|
return "";
|
|
@@ -798,7 +865,7 @@ var getSessionFromCtx = async (ctx) => {
|
|
|
798
865
|
var sessionMiddleware = createAuthMiddleware(async (ctx) => {
|
|
799
866
|
const session = await getSessionFromCtx(ctx);
|
|
800
867
|
if (!session?.session) {
|
|
801
|
-
throw new
|
|
868
|
+
throw new APIError2("UNAUTHORIZED");
|
|
802
869
|
}
|
|
803
870
|
return {
|
|
804
871
|
session
|
|
@@ -833,8 +900,8 @@ var revokeSession = createAuthEndpoint(
|
|
|
833
900
|
"/user/revoke-session",
|
|
834
901
|
{
|
|
835
902
|
method: "POST",
|
|
836
|
-
body:
|
|
837
|
-
id:
|
|
903
|
+
body: z3.object({
|
|
904
|
+
id: z3.string()
|
|
838
905
|
}),
|
|
839
906
|
use: [sessionMiddleware],
|
|
840
907
|
requireHeaders: true
|
|
@@ -887,26 +954,26 @@ var signInOAuth = createAuthEndpoint(
|
|
|
887
954
|
{
|
|
888
955
|
method: "POST",
|
|
889
956
|
requireHeaders: true,
|
|
890
|
-
query:
|
|
957
|
+
query: z4.object({
|
|
891
958
|
/**
|
|
892
959
|
* Redirect to the current URL after the
|
|
893
960
|
* user has signed in.
|
|
894
961
|
*/
|
|
895
|
-
currentURL:
|
|
962
|
+
currentURL: z4.string().optional()
|
|
896
963
|
}).optional(),
|
|
897
|
-
body:
|
|
964
|
+
body: z4.object({
|
|
898
965
|
/**
|
|
899
966
|
* Callback URL to redirect to after the user has signed in.
|
|
900
967
|
*/
|
|
901
|
-
callbackURL:
|
|
968
|
+
callbackURL: z4.string().optional(),
|
|
902
969
|
/**
|
|
903
970
|
* OAuth2 provider to use`
|
|
904
971
|
*/
|
|
905
|
-
provider:
|
|
972
|
+
provider: z4.enum(oAuthProviderList),
|
|
906
973
|
/**
|
|
907
974
|
* If this is true the session will only be valid for the current browser session
|
|
908
975
|
*/
|
|
909
|
-
dontRememberMe:
|
|
976
|
+
dontRememberMe: z4.boolean().default(false).optional()
|
|
910
977
|
})
|
|
911
978
|
},
|
|
912
979
|
async (c) => {
|
|
@@ -920,7 +987,7 @@ var signInOAuth = createAuthEndpoint(
|
|
|
920
987
|
provider: c.body.provider
|
|
921
988
|
}
|
|
922
989
|
);
|
|
923
|
-
throw new
|
|
990
|
+
throw new APIError3("NOT_FOUND", {
|
|
924
991
|
message: "Provider not found"
|
|
925
992
|
});
|
|
926
993
|
}
|
|
@@ -960,7 +1027,7 @@ var signInOAuth = createAuthEndpoint(
|
|
|
960
1027
|
redirect: true
|
|
961
1028
|
};
|
|
962
1029
|
} catch (e) {
|
|
963
|
-
throw new
|
|
1030
|
+
throw new APIError3("INTERNAL_SERVER_ERROR");
|
|
964
1031
|
}
|
|
965
1032
|
}
|
|
966
1033
|
);
|
|
@@ -968,15 +1035,15 @@ var signInEmail = createAuthEndpoint(
|
|
|
968
1035
|
"/sign-in/email",
|
|
969
1036
|
{
|
|
970
1037
|
method: "POST",
|
|
971
|
-
body:
|
|
972
|
-
email:
|
|
973
|
-
password:
|
|
974
|
-
callbackURL:
|
|
1038
|
+
body: z4.object({
|
|
1039
|
+
email: z4.string().email(),
|
|
1040
|
+
password: z4.string(),
|
|
1041
|
+
callbackURL: z4.string().optional(),
|
|
975
1042
|
/**
|
|
976
1043
|
* If this is true the session will only be valid for the current browser session
|
|
977
1044
|
* @default false
|
|
978
1045
|
*/
|
|
979
|
-
dontRememberMe:
|
|
1046
|
+
dontRememberMe: z4.boolean().default(false).optional()
|
|
980
1047
|
})
|
|
981
1048
|
},
|
|
982
1049
|
async (ctx) => {
|
|
@@ -984,7 +1051,7 @@ var signInEmail = createAuthEndpoint(
|
|
|
984
1051
|
ctx.context.logger.error(
|
|
985
1052
|
"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!"
|
|
986
1053
|
);
|
|
987
|
-
throw new
|
|
1054
|
+
throw new APIError3("BAD_REQUEST", {
|
|
988
1055
|
message: "Email and password is not enabled"
|
|
989
1056
|
});
|
|
990
1057
|
}
|
|
@@ -995,9 +1062,9 @@ var signInEmail = createAuthEndpoint(
|
|
|
995
1062
|
);
|
|
996
1063
|
}
|
|
997
1064
|
const { email, password } = ctx.body;
|
|
998
|
-
const checkEmail =
|
|
1065
|
+
const checkEmail = z4.string().email().safeParse(email);
|
|
999
1066
|
if (!checkEmail.success) {
|
|
1000
|
-
throw new
|
|
1067
|
+
throw new APIError3("BAD_REQUEST", {
|
|
1001
1068
|
message: "Invalid email"
|
|
1002
1069
|
});
|
|
1003
1070
|
}
|
|
@@ -1005,7 +1072,7 @@ var signInEmail = createAuthEndpoint(
|
|
|
1005
1072
|
if (!user) {
|
|
1006
1073
|
await ctx.context.password.hash(password);
|
|
1007
1074
|
ctx.context.logger.error("User not found", { email });
|
|
1008
|
-
throw new
|
|
1075
|
+
throw new APIError3("UNAUTHORIZED", {
|
|
1009
1076
|
message: "Invalid email or password"
|
|
1010
1077
|
});
|
|
1011
1078
|
}
|
|
@@ -1014,14 +1081,14 @@ var signInEmail = createAuthEndpoint(
|
|
|
1014
1081
|
);
|
|
1015
1082
|
if (!credentialAccount) {
|
|
1016
1083
|
ctx.context.logger.error("Credential account not found", { email });
|
|
1017
|
-
throw new
|
|
1084
|
+
throw new APIError3("UNAUTHORIZED", {
|
|
1018
1085
|
message: "Invalid email or password"
|
|
1019
1086
|
});
|
|
1020
1087
|
}
|
|
1021
1088
|
const currentPassword = credentialAccount?.password;
|
|
1022
1089
|
if (!currentPassword) {
|
|
1023
1090
|
ctx.context.logger.error("Password not found", { email });
|
|
1024
|
-
throw new
|
|
1091
|
+
throw new APIError3("UNAUTHORIZED", {
|
|
1025
1092
|
message: "Unexpected error"
|
|
1026
1093
|
});
|
|
1027
1094
|
}
|
|
@@ -1031,7 +1098,7 @@ var signInEmail = createAuthEndpoint(
|
|
|
1031
1098
|
);
|
|
1032
1099
|
if (!validPassword) {
|
|
1033
1100
|
ctx.context.logger.error("Invalid password");
|
|
1034
|
-
throw new
|
|
1101
|
+
throw new APIError3("UNAUTHORIZED", {
|
|
1035
1102
|
message: "Invalid email or password"
|
|
1036
1103
|
});
|
|
1037
1104
|
}
|
|
@@ -1042,7 +1109,7 @@ var signInEmail = createAuthEndpoint(
|
|
|
1042
1109
|
);
|
|
1043
1110
|
if (!session) {
|
|
1044
1111
|
ctx.context.logger.error("Failed to create session");
|
|
1045
|
-
throw new
|
|
1112
|
+
throw new APIError3("INTERNAL_SERVER_ERROR");
|
|
1046
1113
|
}
|
|
1047
1114
|
await setSessionCookie(ctx, session.id, ctx.body.dontRememberMe);
|
|
1048
1115
|
return ctx.json({
|
|
@@ -1053,46 +1120,64 @@ var signInEmail = createAuthEndpoint(
|
|
|
1053
1120
|
});
|
|
1054
1121
|
}
|
|
1055
1122
|
);
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1123
|
+
|
|
1124
|
+
// src/api/routes/callback.ts
|
|
1125
|
+
import { APIError as APIError4 } from "better-call";
|
|
1126
|
+
import { z as z6 } from "zod";
|
|
1127
|
+
|
|
1128
|
+
// src/db/schema.ts
|
|
1129
|
+
import { z as z5 } from "zod";
|
|
1130
|
+
var accountSchema = z5.object({
|
|
1131
|
+
id: z5.string(),
|
|
1132
|
+
providerId: z5.string(),
|
|
1133
|
+
accountId: z5.string(),
|
|
1134
|
+
userId: z5.string(),
|
|
1135
|
+
accessToken: z5.string().nullable().optional(),
|
|
1136
|
+
refreshToken: z5.string().nullable().optional(),
|
|
1137
|
+
idToken: z5.string().nullable().optional(),
|
|
1064
1138
|
/**
|
|
1065
1139
|
* Access token expires at
|
|
1066
1140
|
*/
|
|
1067
|
-
expiresAt:
|
|
1141
|
+
expiresAt: z5.date().nullable().optional(),
|
|
1068
1142
|
/**
|
|
1069
1143
|
* Password is only stored in the credential provider
|
|
1070
1144
|
*/
|
|
1071
|
-
password:
|
|
1145
|
+
password: z5.string().optional().nullable()
|
|
1072
1146
|
});
|
|
1073
|
-
var userSchema =
|
|
1074
|
-
id:
|
|
1075
|
-
email:
|
|
1076
|
-
emailVerified:
|
|
1077
|
-
name:
|
|
1078
|
-
image:
|
|
1079
|
-
createdAt:
|
|
1080
|
-
updatedAt:
|
|
1147
|
+
var userSchema = z5.object({
|
|
1148
|
+
id: z5.string(),
|
|
1149
|
+
email: z5.string().transform((val) => val.toLowerCase()),
|
|
1150
|
+
emailVerified: z5.boolean().default(false),
|
|
1151
|
+
name: z5.string(),
|
|
1152
|
+
image: z5.string().optional(),
|
|
1153
|
+
createdAt: z5.date().default(/* @__PURE__ */ new Date()),
|
|
1154
|
+
updatedAt: z5.date().default(/* @__PURE__ */ new Date())
|
|
1081
1155
|
});
|
|
1082
|
-
|
|
1083
|
-
id:
|
|
1084
|
-
userId:
|
|
1085
|
-
expiresAt:
|
|
1086
|
-
ipAddress:
|
|
1087
|
-
userAgent:
|
|
1156
|
+
var sessionSchema = z5.object({
|
|
1157
|
+
id: z5.string(),
|
|
1158
|
+
userId: z5.string(),
|
|
1159
|
+
expiresAt: z5.date(),
|
|
1160
|
+
ipAddress: z5.string().optional(),
|
|
1161
|
+
userAgent: z5.string().optional()
|
|
1088
1162
|
});
|
|
1089
|
-
|
|
1090
|
-
id:
|
|
1091
|
-
value:
|
|
1092
|
-
expiresAt:
|
|
1093
|
-
identifier:
|
|
1163
|
+
var verificationSchema = z5.object({
|
|
1164
|
+
id: z5.string(),
|
|
1165
|
+
value: z5.string(),
|
|
1166
|
+
expiresAt: z5.date(),
|
|
1167
|
+
identifier: z5.string()
|
|
1094
1168
|
});
|
|
1095
1169
|
|
|
1170
|
+
// src/utils/id.ts
|
|
1171
|
+
import { nanoid } from "nanoid";
|
|
1172
|
+
var generateId = (size) => {
|
|
1173
|
+
return nanoid(size);
|
|
1174
|
+
};
|
|
1175
|
+
|
|
1176
|
+
// src/utils/hide-metadata.ts
|
|
1177
|
+
var HIDE_METADATA = {
|
|
1178
|
+
isAction: false
|
|
1179
|
+
};
|
|
1180
|
+
|
|
1096
1181
|
// src/utils/getAccount.ts
|
|
1097
1182
|
function getAccountTokens(tokens) {
|
|
1098
1183
|
const accessToken = tokens.accessToken();
|
|
@@ -1114,10 +1199,10 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1114
1199
|
"/callback/:id",
|
|
1115
1200
|
{
|
|
1116
1201
|
method: "GET",
|
|
1117
|
-
query:
|
|
1118
|
-
state:
|
|
1119
|
-
code:
|
|
1120
|
-
error:
|
|
1202
|
+
query: z6.object({
|
|
1203
|
+
state: z6.string(),
|
|
1204
|
+
code: z6.string().optional(),
|
|
1205
|
+
error: z6.string().optional()
|
|
1121
1206
|
}),
|
|
1122
1207
|
metadata: HIDE_METADATA
|
|
1123
1208
|
},
|
|
@@ -1245,7 +1330,7 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1245
1330
|
}
|
|
1246
1331
|
}
|
|
1247
1332
|
if (!userId && !id)
|
|
1248
|
-
throw new
|
|
1333
|
+
throw new APIError4("INTERNAL_SERVER_ERROR", {
|
|
1249
1334
|
message: "Unable to create user"
|
|
1250
1335
|
});
|
|
1251
1336
|
try {
|
|
@@ -1275,13 +1360,16 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1275
1360
|
throw c.redirect(callbackURL);
|
|
1276
1361
|
}
|
|
1277
1362
|
);
|
|
1363
|
+
|
|
1364
|
+
// src/api/routes/sign-out.ts
|
|
1365
|
+
import { z as z7 } from "zod";
|
|
1278
1366
|
var signOut = createAuthEndpoint(
|
|
1279
1367
|
"/sign-out",
|
|
1280
1368
|
{
|
|
1281
1369
|
method: "POST",
|
|
1282
|
-
body:
|
|
1283
|
-
|
|
1284
|
-
callbackURL:
|
|
1370
|
+
body: z7.optional(
|
|
1371
|
+
z7.object({
|
|
1372
|
+
callbackURL: z7.string().optional()
|
|
1285
1373
|
})
|
|
1286
1374
|
)
|
|
1287
1375
|
},
|
|
@@ -1303,22 +1391,28 @@ var signOut = createAuthEndpoint(
|
|
|
1303
1391
|
});
|
|
1304
1392
|
}
|
|
1305
1393
|
);
|
|
1394
|
+
|
|
1395
|
+
// src/api/routes/forget-password.ts
|
|
1396
|
+
import { TimeSpan as TimeSpan2 } from "oslo";
|
|
1397
|
+
import { createJWT, parseJWT as parseJWT3 } from "oslo/jwt";
|
|
1398
|
+
import { validateJWT } from "oslo/jwt";
|
|
1399
|
+
import { z as z8 } from "zod";
|
|
1306
1400
|
var forgetPassword = createAuthEndpoint(
|
|
1307
1401
|
"/forget-password",
|
|
1308
1402
|
{
|
|
1309
1403
|
method: "POST",
|
|
1310
|
-
body:
|
|
1404
|
+
body: z8.object({
|
|
1311
1405
|
/**
|
|
1312
1406
|
* The email address of the user to send a password reset email to.
|
|
1313
1407
|
*/
|
|
1314
|
-
email:
|
|
1408
|
+
email: z8.string().email(),
|
|
1315
1409
|
/**
|
|
1316
1410
|
* The URL to redirect the user to reset their password.
|
|
1317
1411
|
* If the token isn't valid or expired, it'll be redirected with a query parameter `?
|
|
1318
1412
|
* error=INVALID_TOKEN`. If the token is valid, it'll be redirected with a query parameter `?
|
|
1319
1413
|
* token=VALID_TOKEN
|
|
1320
1414
|
*/
|
|
1321
|
-
redirectTo:
|
|
1415
|
+
redirectTo: z8.string()
|
|
1322
1416
|
})
|
|
1323
1417
|
},
|
|
1324
1418
|
async (ctx) => {
|
|
@@ -1356,7 +1450,7 @@ var forgetPassword = createAuthEndpoint(
|
|
|
1356
1450
|
redirectTo: ctx.body.redirectTo
|
|
1357
1451
|
},
|
|
1358
1452
|
{
|
|
1359
|
-
expiresIn: new
|
|
1453
|
+
expiresIn: new TimeSpan2(1, "h"),
|
|
1360
1454
|
issuer: "better-auth",
|
|
1361
1455
|
subject: "forget-password",
|
|
1362
1456
|
audiences: [user.user.email],
|
|
@@ -1381,9 +1475,9 @@ var forgetPasswordCallback = createAuthEndpoint(
|
|
|
1381
1475
|
async (ctx) => {
|
|
1382
1476
|
const { token } = ctx.params;
|
|
1383
1477
|
let decodedToken;
|
|
1384
|
-
const schema =
|
|
1385
|
-
email:
|
|
1386
|
-
redirectTo:
|
|
1478
|
+
const schema = z8.object({
|
|
1479
|
+
email: z8.string(),
|
|
1480
|
+
redirectTo: z8.string()
|
|
1387
1481
|
});
|
|
1388
1482
|
try {
|
|
1389
1483
|
decodedToken = await validateJWT(
|
|
@@ -1395,7 +1489,7 @@ var forgetPasswordCallback = createAuthEndpoint(
|
|
|
1395
1489
|
throw Error("Token expired");
|
|
1396
1490
|
}
|
|
1397
1491
|
} catch (e) {
|
|
1398
|
-
const decoded =
|
|
1492
|
+
const decoded = parseJWT3(token);
|
|
1399
1493
|
const jwt = schema.safeParse(decoded?.payload);
|
|
1400
1494
|
if (jwt.success) {
|
|
1401
1495
|
throw ctx.redirect(`${jwt.data?.redirectTo}?error=invalid_token`);
|
|
@@ -1411,12 +1505,12 @@ var resetPassword = createAuthEndpoint(
|
|
|
1411
1505
|
"/reset-password",
|
|
1412
1506
|
{
|
|
1413
1507
|
method: "POST",
|
|
1414
|
-
query:
|
|
1415
|
-
currentURL:
|
|
1508
|
+
query: z8.object({
|
|
1509
|
+
currentURL: z8.string()
|
|
1416
1510
|
}).optional(),
|
|
1417
|
-
body:
|
|
1418
|
-
newPassword:
|
|
1419
|
-
callbackURL:
|
|
1511
|
+
body: z8.object({
|
|
1512
|
+
newPassword: z8.string(),
|
|
1513
|
+
callbackURL: z8.string().optional()
|
|
1420
1514
|
})
|
|
1421
1515
|
},
|
|
1422
1516
|
async (ctx) => {
|
|
@@ -1443,7 +1537,7 @@ var resetPassword = createAuthEndpoint(
|
|
|
1443
1537
|
Buffer.from(ctx.context.secret),
|
|
1444
1538
|
token
|
|
1445
1539
|
);
|
|
1446
|
-
const email =
|
|
1540
|
+
const email = z8.string().email().parse(jwt.payload.email);
|
|
1447
1541
|
const user = await ctx.context.internalAdapter.findUserByEmail(email);
|
|
1448
1542
|
if (!user) {
|
|
1449
1543
|
return ctx.json(
|
|
@@ -1523,15 +1617,20 @@ var resetPassword = createAuthEndpoint(
|
|
|
1523
1617
|
}
|
|
1524
1618
|
}
|
|
1525
1619
|
);
|
|
1620
|
+
|
|
1621
|
+
// src/api/routes/verify-email.ts
|
|
1622
|
+
import { TimeSpan as TimeSpan3 } from "oslo";
|
|
1623
|
+
import { createJWT as createJWT2, validateJWT as validateJWT2 } from "oslo/jwt";
|
|
1624
|
+
import { z as z9 } from "zod";
|
|
1526
1625
|
async function createEmailVerificationToken(secret, email) {
|
|
1527
|
-
const token = await
|
|
1626
|
+
const token = await createJWT2(
|
|
1528
1627
|
"HS256",
|
|
1529
1628
|
Buffer.from(secret),
|
|
1530
1629
|
{
|
|
1531
1630
|
email: email.toLowerCase()
|
|
1532
1631
|
},
|
|
1533
1632
|
{
|
|
1534
|
-
expiresIn: new
|
|
1633
|
+
expiresIn: new TimeSpan3(1, "h"),
|
|
1535
1634
|
issuer: "better-auth",
|
|
1536
1635
|
subject: "verify-email",
|
|
1537
1636
|
audiences: [email],
|
|
@@ -1544,12 +1643,12 @@ var sendVerificationEmail = createAuthEndpoint(
|
|
|
1544
1643
|
"/send-verification-email",
|
|
1545
1644
|
{
|
|
1546
1645
|
method: "POST",
|
|
1547
|
-
query:
|
|
1548
|
-
currentURL:
|
|
1646
|
+
query: z9.object({
|
|
1647
|
+
currentURL: z9.string().optional()
|
|
1549
1648
|
}).optional(),
|
|
1550
|
-
body:
|
|
1551
|
-
email:
|
|
1552
|
-
callbackURL:
|
|
1649
|
+
body: z9.object({
|
|
1650
|
+
email: z9.string().email(),
|
|
1651
|
+
callbackURL: z9.string().optional()
|
|
1553
1652
|
})
|
|
1554
1653
|
},
|
|
1555
1654
|
async (ctx) => {
|
|
@@ -1582,16 +1681,16 @@ var verifyEmail = createAuthEndpoint(
|
|
|
1582
1681
|
"/verify-email",
|
|
1583
1682
|
{
|
|
1584
1683
|
method: "GET",
|
|
1585
|
-
query:
|
|
1586
|
-
token:
|
|
1587
|
-
callbackURL:
|
|
1684
|
+
query: z9.object({
|
|
1685
|
+
token: z9.string(),
|
|
1686
|
+
callbackURL: z9.string().optional()
|
|
1588
1687
|
})
|
|
1589
1688
|
},
|
|
1590
1689
|
async (ctx) => {
|
|
1591
1690
|
const { token } = ctx.query;
|
|
1592
1691
|
let jwt;
|
|
1593
1692
|
try {
|
|
1594
|
-
jwt = await
|
|
1693
|
+
jwt = await validateJWT2("HS256", Buffer.from(ctx.context.secret), token);
|
|
1595
1694
|
} catch (e) {
|
|
1596
1695
|
ctx.context.logger.error("Failed to verify email", e);
|
|
1597
1696
|
return ctx.json(null, {
|
|
@@ -1602,8 +1701,8 @@ var verifyEmail = createAuthEndpoint(
|
|
|
1602
1701
|
}
|
|
1603
1702
|
});
|
|
1604
1703
|
}
|
|
1605
|
-
const schema =
|
|
1606
|
-
email:
|
|
1704
|
+
const schema = z9.object({
|
|
1705
|
+
email: z9.string().email()
|
|
1607
1706
|
});
|
|
1608
1707
|
const parsed = schema.parse(jwt.payload);
|
|
1609
1708
|
const user = await ctx.context.internalAdapter.findUserByEmail(
|
|
@@ -1635,6 +1734,9 @@ var verifyEmail = createAuthEndpoint(
|
|
|
1635
1734
|
}
|
|
1636
1735
|
);
|
|
1637
1736
|
|
|
1737
|
+
// src/api/routes/update-user.ts
|
|
1738
|
+
import { z as z10 } from "zod";
|
|
1739
|
+
|
|
1638
1740
|
// src/crypto/random.ts
|
|
1639
1741
|
function byteToBinary(byte) {
|
|
1640
1742
|
return byte.toString(2).padStart(8, "0");
|
|
@@ -1697,9 +1799,9 @@ var updateUser = createAuthEndpoint(
|
|
|
1697
1799
|
"/user/update",
|
|
1698
1800
|
{
|
|
1699
1801
|
method: "POST",
|
|
1700
|
-
body:
|
|
1701
|
-
name:
|
|
1702
|
-
image:
|
|
1802
|
+
body: z10.object({
|
|
1803
|
+
name: z10.string().optional(),
|
|
1804
|
+
image: z10.string().optional()
|
|
1703
1805
|
}),
|
|
1704
1806
|
use: [sessionMiddleware]
|
|
1705
1807
|
},
|
|
@@ -1723,20 +1825,20 @@ var changePassword = createAuthEndpoint(
|
|
|
1723
1825
|
"/user/change-password",
|
|
1724
1826
|
{
|
|
1725
1827
|
method: "POST",
|
|
1726
|
-
body:
|
|
1828
|
+
body: z10.object({
|
|
1727
1829
|
/**
|
|
1728
1830
|
* The new password to set
|
|
1729
1831
|
*/
|
|
1730
|
-
newPassword:
|
|
1832
|
+
newPassword: z10.string(),
|
|
1731
1833
|
/**
|
|
1732
1834
|
* The current password of the user
|
|
1733
1835
|
*/
|
|
1734
|
-
currentPassword:
|
|
1836
|
+
currentPassword: z10.string(),
|
|
1735
1837
|
/**
|
|
1736
1838
|
* revoke all sessions that are not the
|
|
1737
1839
|
* current one logged in by the user
|
|
1738
1840
|
*/
|
|
1739
|
-
revokeOtherSessions:
|
|
1841
|
+
revokeOtherSessions: z10.boolean().optional()
|
|
1740
1842
|
}),
|
|
1741
1843
|
use: [sessionMiddleware]
|
|
1742
1844
|
},
|
|
@@ -1806,11 +1908,11 @@ var setPassword = createAuthEndpoint(
|
|
|
1806
1908
|
"/user/set-password",
|
|
1807
1909
|
{
|
|
1808
1910
|
method: "POST",
|
|
1809
|
-
body:
|
|
1911
|
+
body: z10.object({
|
|
1810
1912
|
/**
|
|
1811
1913
|
* The new password to set
|
|
1812
1914
|
*/
|
|
1813
|
-
newPassword:
|
|
1915
|
+
newPassword: z10.string()
|
|
1814
1916
|
}),
|
|
1815
1917
|
use: [sessionMiddleware]
|
|
1816
1918
|
},
|
|
@@ -1860,8 +1962,8 @@ var deleteUser = createAuthEndpoint(
|
|
|
1860
1962
|
"/user/delete",
|
|
1861
1963
|
{
|
|
1862
1964
|
method: "POST",
|
|
1863
|
-
body:
|
|
1864
|
-
password:
|
|
1965
|
+
body: z10.object({
|
|
1966
|
+
password: z10.string()
|
|
1865
1967
|
}),
|
|
1866
1968
|
use: [sessionMiddleware]
|
|
1867
1969
|
},
|
|
@@ -2039,19 +2141,22 @@ var ok = createAuthEndpoint(
|
|
|
2039
2141
|
});
|
|
2040
2142
|
}
|
|
2041
2143
|
);
|
|
2144
|
+
|
|
2145
|
+
// src/api/routes/sign-up.ts
|
|
2146
|
+
import { z as z11 } from "zod";
|
|
2042
2147
|
var signUpEmail = createAuthEndpoint(
|
|
2043
2148
|
"/sign-up/email",
|
|
2044
2149
|
{
|
|
2045
2150
|
method: "POST",
|
|
2046
|
-
query:
|
|
2047
|
-
currentURL:
|
|
2151
|
+
query: z11.object({
|
|
2152
|
+
currentURL: z11.string().optional()
|
|
2048
2153
|
}).optional(),
|
|
2049
|
-
body:
|
|
2050
|
-
name:
|
|
2051
|
-
email:
|
|
2052
|
-
password:
|
|
2053
|
-
image:
|
|
2054
|
-
callbackURL:
|
|
2154
|
+
body: z11.object({
|
|
2155
|
+
name: z11.string(),
|
|
2156
|
+
email: z11.string(),
|
|
2157
|
+
password: z11.string(),
|
|
2158
|
+
image: z11.string().optional(),
|
|
2159
|
+
callbackURL: z11.string().optional()
|
|
2055
2160
|
})
|
|
2056
2161
|
},
|
|
2057
2162
|
async (ctx) => {
|
|
@@ -2064,7 +2169,7 @@ var signUpEmail = createAuthEndpoint(
|
|
|
2064
2169
|
});
|
|
2065
2170
|
}
|
|
2066
2171
|
const { name, email, password, image } = ctx.body;
|
|
2067
|
-
const isValidEmail =
|
|
2172
|
+
const isValidEmail = z11.string().email().safeParse(email);
|
|
2068
2173
|
if (!isValidEmail.success) {
|
|
2069
2174
|
return ctx.json(null, {
|
|
2070
2175
|
status: 400,
|
|
@@ -2166,6 +2271,9 @@ var signUpEmail = createAuthEndpoint(
|
|
|
2166
2271
|
}
|
|
2167
2272
|
);
|
|
2168
2273
|
|
|
2274
|
+
// src/api/index.ts
|
|
2275
|
+
import chalk from "chalk";
|
|
2276
|
+
|
|
2169
2277
|
// src/api/rate-limiter.ts
|
|
2170
2278
|
function shouldRateLimit(max, window2, rateLimitData) {
|
|
2171
2279
|
const now = Date.now();
|
|
@@ -2463,7 +2571,7 @@ var router = (ctx, options) => {
|
|
|
2463
2571
|
onError(e) {
|
|
2464
2572
|
const log = options.logger?.verboseLogging ? logger : void 0;
|
|
2465
2573
|
if (options.logger?.disabled !== true) {
|
|
2466
|
-
if (e instanceof
|
|
2574
|
+
if (e instanceof APIError5) {
|
|
2467
2575
|
if (e.status === "INTERNAL_SERVER_ERROR") {
|
|
2468
2576
|
logger.error(e);
|
|
2469
2577
|
}
|
|
@@ -2504,5 +2612,35 @@ var router = (ctx, options) => {
|
|
|
2504
2612
|
}
|
|
2505
2613
|
});
|
|
2506
2614
|
};
|
|
2507
|
-
|
|
2508
|
-
|
|
2615
|
+
export {
|
|
2616
|
+
callbackOAuth,
|
|
2617
|
+
changePassword,
|
|
2618
|
+
createAuthEndpoint,
|
|
2619
|
+
createAuthMiddleware,
|
|
2620
|
+
createEmailVerificationToken,
|
|
2621
|
+
csrfMiddleware,
|
|
2622
|
+
deleteUser,
|
|
2623
|
+
error,
|
|
2624
|
+
forgetPassword,
|
|
2625
|
+
forgetPasswordCallback,
|
|
2626
|
+
getCSRFToken,
|
|
2627
|
+
getEndpoints,
|
|
2628
|
+
getSession,
|
|
2629
|
+
getSessionFromCtx,
|
|
2630
|
+
listSessions,
|
|
2631
|
+
ok,
|
|
2632
|
+
optionsMiddleware,
|
|
2633
|
+
resetPassword,
|
|
2634
|
+
revokeSession,
|
|
2635
|
+
revokeSessions,
|
|
2636
|
+
router,
|
|
2637
|
+
sendVerificationEmail,
|
|
2638
|
+
sessionMiddleware,
|
|
2639
|
+
setPassword,
|
|
2640
|
+
signInEmail,
|
|
2641
|
+
signInOAuth,
|
|
2642
|
+
signOut,
|
|
2643
|
+
signUpEmail,
|
|
2644
|
+
updateUser,
|
|
2645
|
+
verifyEmail
|
|
2646
|
+
};
|