better-auth 1.1.18-beta.3 → 1.1.18

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.
@@ -26,12 +26,18 @@ var schema = {
26
26
  };
27
27
 
28
28
  // src/plugins/username/index.ts
29
+ function defaultUsernameValidator(username2) {
30
+ return /^[a-zA-Z0-9_]+$/.test(username2);
31
+ }
29
32
  var username = (options) => {
30
33
  const ERROR_CODES = {
31
34
  INVALID_USERNAME_OR_PASSWORD: "invalid username or password",
32
35
  EMAIL_NOT_VERIFIED: "email not verified",
33
36
  UNEXPECTED_ERROR: "unexpected error",
34
- USERNAME_IS_ALREADY_TAKEN: "username is already taken. please try another."
37
+ USERNAME_IS_ALREADY_TAKEN: "username is already taken. please try another.",
38
+ USERNAME_TOO_SHORT: "username is too short",
39
+ USERNAME_TOO_LONG: "username is too long",
40
+ INVALID_USERNAME: "username is invalid"
35
41
  };
36
42
  return {
37
43
  id: "username",
@@ -79,6 +85,36 @@ var username = (options) => {
79
85
  }
80
86
  },
81
87
  async (ctx) => {
88
+ if (!ctx.body.username || !ctx.body.password) {
89
+ ctx.context.logger.error("Username or password not found");
90
+ throw new APIError("UNAUTHORIZED", {
91
+ message: ERROR_CODES.INVALID_USERNAME_OR_PASSWORD
92
+ });
93
+ }
94
+ const minUsernameLength = options?.minUsernameLength || 3;
95
+ const maxUsernameLength = options?.maxUsernameLength || 30;
96
+ if (ctx.body.username.length < minUsernameLength) {
97
+ ctx.context.logger.error("Username too short", {
98
+ username: ctx.body.username
99
+ });
100
+ throw new APIError("UNPROCESSABLE_ENTITY", {
101
+ message: ERROR_CODES.USERNAME_TOO_SHORT
102
+ });
103
+ }
104
+ if (ctx.body.username.length > maxUsernameLength) {
105
+ ctx.context.logger.error("Username too long", {
106
+ username: ctx.body.username
107
+ });
108
+ throw new APIError("UNPROCESSABLE_ENTITY", {
109
+ message: ERROR_CODES.USERNAME_TOO_LONG
110
+ });
111
+ }
112
+ const validator = options?.usernameValidator || defaultUsernameValidator;
113
+ if (!validator(ctx.body.username)) {
114
+ throw new APIError("UNPROCESSABLE_ENTITY", {
115
+ message: ERROR_CODES.INVALID_USERNAME
116
+ });
117
+ }
82
118
  const user = await ctx.context.adapter.findOne({
83
119
  model: "user",
84
120
  where: [
@@ -175,11 +211,30 @@ var username = (options) => {
175
211
  before: [
176
212
  {
177
213
  matcher(context) {
178
- return context.path === "/sign-up/email";
214
+ return context.path === "/sign-up/email" || context.path === "/update-user";
179
215
  },
180
216
  async handler(ctx) {
181
217
  const username2 = ctx.body.username;
182
218
  if (username2) {
219
+ const minUsernameLength = options?.minUsernameLength || 3;
220
+ const maxUsernameLength = options?.maxUsernameLength || 30;
221
+ if (username2.length < minUsernameLength) {
222
+ throw new APIError("UNPROCESSABLE_ENTITY", {
223
+ message: ERROR_CODES.USERNAME_TOO_SHORT
224
+ });
225
+ }
226
+ if (username2.length > maxUsernameLength) {
227
+ throw new APIError("UNPROCESSABLE_ENTITY", {
228
+ message: ERROR_CODES.USERNAME_TOO_LONG
229
+ });
230
+ }
231
+ const validator = options?.usernameValidator || defaultUsernameValidator;
232
+ const valid = await validator(username2);
233
+ if (!valid) {
234
+ throw new APIError("UNPROCESSABLE_ENTITY", {
235
+ message: ERROR_CODES.INVALID_USERNAME
236
+ });
237
+ }
183
238
  const user = await ctx.context.adapter.findOne({
184
239
  model: "user",
185
240
  where: [
@@ -28,12 +28,18 @@ var schema = {
28
28
  };
29
29
 
30
30
  // src/plugins/username/index.ts
31
+ function defaultUsernameValidator(username2) {
32
+ return /^[a-zA-Z0-9_]+$/.test(username2);
33
+ }
31
34
  var username = (options) => {
32
35
  const ERROR_CODES = {
33
36
  INVALID_USERNAME_OR_PASSWORD: "invalid username or password",
34
37
  EMAIL_NOT_VERIFIED: "email not verified",
35
38
  UNEXPECTED_ERROR: "unexpected error",
36
- USERNAME_IS_ALREADY_TAKEN: "username is already taken. please try another."
39
+ USERNAME_IS_ALREADY_TAKEN: "username is already taken. please try another.",
40
+ USERNAME_TOO_SHORT: "username is too short",
41
+ USERNAME_TOO_LONG: "username is too long",
42
+ INVALID_USERNAME: "username is invalid"
37
43
  };
38
44
  return {
39
45
  id: "username",
@@ -81,6 +87,36 @@ var username = (options) => {
81
87
  }
82
88
  },
83
89
  async (ctx) => {
90
+ if (!ctx.body.username || !ctx.body.password) {
91
+ ctx.context.logger.error("Username or password not found");
92
+ throw new betterCall.APIError("UNAUTHORIZED", {
93
+ message: ERROR_CODES.INVALID_USERNAME_OR_PASSWORD
94
+ });
95
+ }
96
+ const minUsernameLength = options?.minUsernameLength || 3;
97
+ const maxUsernameLength = options?.maxUsernameLength || 30;
98
+ if (ctx.body.username.length < minUsernameLength) {
99
+ ctx.context.logger.error("Username too short", {
100
+ username: ctx.body.username
101
+ });
102
+ throw new betterCall.APIError("UNPROCESSABLE_ENTITY", {
103
+ message: ERROR_CODES.USERNAME_TOO_SHORT
104
+ });
105
+ }
106
+ if (ctx.body.username.length > maxUsernameLength) {
107
+ ctx.context.logger.error("Username too long", {
108
+ username: ctx.body.username
109
+ });
110
+ throw new betterCall.APIError("UNPROCESSABLE_ENTITY", {
111
+ message: ERROR_CODES.USERNAME_TOO_LONG
112
+ });
113
+ }
114
+ const validator = options?.usernameValidator || defaultUsernameValidator;
115
+ if (!validator(ctx.body.username)) {
116
+ throw new betterCall.APIError("UNPROCESSABLE_ENTITY", {
117
+ message: ERROR_CODES.INVALID_USERNAME
118
+ });
119
+ }
84
120
  const user = await ctx.context.adapter.findOne({
85
121
  model: "user",
86
122
  where: [
@@ -177,11 +213,30 @@ var username = (options) => {
177
213
  before: [
178
214
  {
179
215
  matcher(context) {
180
- return context.path === "/sign-up/email";
216
+ return context.path === "/sign-up/email" || context.path === "/update-user";
181
217
  },
182
218
  async handler(ctx) {
183
219
  const username2 = ctx.body.username;
184
220
  if (username2) {
221
+ const minUsernameLength = options?.minUsernameLength || 3;
222
+ const maxUsernameLength = options?.maxUsernameLength || 30;
223
+ if (username2.length < minUsernameLength) {
224
+ throw new betterCall.APIError("UNPROCESSABLE_ENTITY", {
225
+ message: ERROR_CODES.USERNAME_TOO_SHORT
226
+ });
227
+ }
228
+ if (username2.length > maxUsernameLength) {
229
+ throw new betterCall.APIError("UNPROCESSABLE_ENTITY", {
230
+ message: ERROR_CODES.USERNAME_TOO_LONG
231
+ });
232
+ }
233
+ const validator = options?.usernameValidator || defaultUsernameValidator;
234
+ const valid = await validator(username2);
235
+ if (!valid) {
236
+ throw new betterCall.APIError("UNPROCESSABLE_ENTITY", {
237
+ message: ERROR_CODES.INVALID_USERNAME
238
+ });
239
+ }
185
240
  const user = await ctx.context.adapter.findOne({
186
241
  model: "user",
187
242
  where: [
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkG4LNWL5L_cjs = require('../chunk-G4LNWL5L.cjs');
3
+ var chunkTSWYQ5PN_cjs = require('../chunk-TSWYQ5PN.cjs');
4
4
  require('../chunk-ZBKCS3KP.cjs');
5
5
  require('../chunk-CQNPMJJ3.cjs');
6
6
  require('../chunk-NU4SXYJ5.cjs');
@@ -21,5 +21,5 @@ require('../chunk-PEZRSDZS.cjs');
21
21
 
22
22
  Object.defineProperty(exports, "username", {
23
23
  enumerable: true,
24
- get: function () { return chunkG4LNWL5L_cjs.username; }
24
+ get: function () { return chunkTSWYQ5PN_cjs.username; }
25
25
  });
@@ -26,6 +26,24 @@ declare const schema: {
26
26
 
27
27
  type UsernameOptions = {
28
28
  schema?: InferOptionSchema<typeof schema>;
29
+ /**
30
+ * The minimum length of the username
31
+ *
32
+ * @default 3
33
+ */
34
+ minUsernameLength?: number;
35
+ /**
36
+ * The maximum length of the username
37
+ *
38
+ * @default 30
39
+ */
40
+ maxUsernameLength?: number;
41
+ /**
42
+ * A function to validate the username
43
+ *
44
+ * By default, the username should only contain alphanumeric characters and underscores
45
+ */
46
+ usernameValidator?: (username: string) => boolean | Promise<boolean>;
29
47
  };
30
48
  declare const username: (options?: UsernameOptions) => {
31
49
  id: "username";
@@ -26,6 +26,24 @@ declare const schema: {
26
26
 
27
27
  type UsernameOptions = {
28
28
  schema?: InferOptionSchema<typeof schema>;
29
+ /**
30
+ * The minimum length of the username
31
+ *
32
+ * @default 3
33
+ */
34
+ minUsernameLength?: number;
35
+ /**
36
+ * The maximum length of the username
37
+ *
38
+ * @default 30
39
+ */
40
+ maxUsernameLength?: number;
41
+ /**
42
+ * A function to validate the username
43
+ *
44
+ * By default, the username should only contain alphanumeric characters and underscores
45
+ */
46
+ usernameValidator?: (username: string) => boolean | Promise<boolean>;
29
47
  };
30
48
  declare const username: (options?: UsernameOptions) => {
31
49
  id: "username";
@@ -1,4 +1,4 @@
1
- export { username } from '../chunk-FS4Q5N2E.js';
1
+ export { username } from '../chunk-PB7U7ZCO.js';
2
2
  import '../chunk-WMXBA6LX.js';
3
3
  import '../chunk-KALC2G3Y.js';
4
4
  import '../chunk-VXDYNCOX.js';
package/dist/plugins.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  var chunkENJRVUKL_cjs = require('./chunk-ENJRVUKL.cjs');
4
4
  var chunkXRNGFRNU_cjs = require('./chunk-XRNGFRNU.cjs');
5
- var chunkG4LNWL5L_cjs = require('./chunk-G4LNWL5L.cjs');
5
+ var chunkTSWYQ5PN_cjs = require('./chunk-TSWYQ5PN.cjs');
6
6
  require('./chunk-ZBKCS3KP.cjs');
7
7
  var chunkSDEOOYUI_cjs = require('./chunk-SDEOOYUI.cjs');
8
8
  var chunkVR4QGIHG_cjs = require('./chunk-VR4QGIHG.cjs');
@@ -185,7 +185,7 @@ Object.defineProperty(exports, "twoFactor", {
185
185
  });
186
186
  Object.defineProperty(exports, "username", {
187
187
  enumerable: true,
188
- get: function () { return chunkG4LNWL5L_cjs.username; }
188
+ get: function () { return chunkTSWYQ5PN_cjs.username; }
189
189
  });
190
190
  Object.defineProperty(exports, "jwt", {
191
191
  enumerable: true,
package/dist/plugins.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export { phoneNumber } from './chunk-T5QCBET4.js';
2
2
  export { twoFactor } from './chunk-G5PHMKSZ.js';
3
- export { username } from './chunk-FS4Q5N2E.js';
3
+ export { username } from './chunk-PB7U7ZCO.js';
4
4
  import './chunk-WMXBA6LX.js';
5
5
  export { jwt } from './chunk-5D5P7O3K.js';
6
6
  export { magicLink } from './chunk-QG36GJF4.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-auth",
3
- "version": "1.1.18-beta.3",
3
+ "version": "1.1.18",
4
4
  "description": "The most comprehensive authentication library for TypeScript.",
5
5
  "type": "module",
6
6
  "repository": {