@nerdfolio/ba-guest-list 0.0.7 → 0.0.9

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/index.cjs CHANGED
@@ -25,6 +25,14 @@ __export(index_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(index_exports);
27
27
 
28
+ // src/client.ts
29
+ var guestListClient = () => {
30
+ return {
31
+ id: "guest-list",
32
+ $InferServerPlugin: {}
33
+ };
34
+ };
35
+
28
36
  // src/server.ts
29
37
  var import_api = require("better-auth/api");
30
38
  var import_cookies = require("better-auth/cookies");
@@ -32,10 +40,10 @@ var import_v4_mini = require("zod/v4-mini");
32
40
 
33
41
  // src/utils.ts
34
42
  var import_lodash_es = require("lodash-es");
35
- function getOrigin(url) {
43
+ function getHostName(url) {
36
44
  try {
37
45
  const parsedUrl = new URL(url);
38
- return parsedUrl.origin;
46
+ return parsedUrl.hostname;
39
47
  } catch (_error) {
40
48
  return null;
41
49
  }
@@ -65,7 +73,7 @@ var guestList = (options) => {
65
73
  return {
66
74
  id: "guest-list",
67
75
  endpoints: {
68
- signInGuest: (0, import_api.createAuthEndpoint)(
76
+ signInGuestList: (0, import_api.createAuthEndpoint)(
69
77
  "/sign-in/guest-list",
70
78
  {
71
79
  method: "POST",
@@ -118,7 +126,7 @@ var guestList = (options) => {
118
126
  message: options?.revealNames ? `Name not on list. Try: ${JSON.stringify(Object.keys(guestLookup))}` : ERROR_CODES.NAME_NOT_ON_GUEST_LIST
119
127
  });
120
128
  }
121
- const { emailDomainName = getOrigin(ctx.context.baseURL) } = options ?? {};
129
+ const { emailDomainName = getHostName(ctx.context.baseURL) } = options ?? {};
122
130
  const email = `${cleanedName.toLowerCase().replaceAll(/\s/g, "")}.onguestlist@${emailDomainName}`;
123
131
  const found = await ctx.context.internalAdapter.findUserByEmail(email);
124
132
  async function createNewUser() {
@@ -187,14 +195,6 @@ var guestList = (options) => {
187
195
  $ERROR_CODES: ERROR_CODES
188
196
  };
189
197
  };
190
-
191
- // src/client.ts
192
- var guestListClient = () => {
193
- return {
194
- id: "guest-list",
195
- $InferServerPlugin: {}
196
- };
197
- };
198
198
  // Annotate the CommonJS export names for ESM import in node:
199
199
  0 && (module.exports = {
200
200
  guestList,
package/dist/index.d.cts CHANGED
@@ -26,7 +26,7 @@ interface GuestListOptions {
26
26
  declare const guestList: (options?: GuestListOptions) => {
27
27
  id: "guest-list";
28
28
  endpoints: {
29
- signInGuest: {
29
+ signInGuestList: {
30
30
  <AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(inputCtx_0: {
31
31
  body: {
32
32
  name: string;
package/dist/index.d.ts CHANGED
@@ -26,7 +26,7 @@ interface GuestListOptions {
26
26
  declare const guestList: (options?: GuestListOptions) => {
27
27
  id: "guest-list";
28
28
  endpoints: {
29
- signInGuest: {
29
+ signInGuestList: {
30
30
  <AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(inputCtx_0: {
31
31
  body: {
32
32
  name: string;
package/dist/index.mjs CHANGED
@@ -1,3 +1,11 @@
1
+ // src/client.ts
2
+ var guestListClient = () => {
3
+ return {
4
+ id: "guest-list",
5
+ $InferServerPlugin: {}
6
+ };
7
+ };
8
+
1
9
  // src/server.ts
2
10
  import { APIError, createAuthEndpoint } from "better-auth/api";
3
11
  import { setSessionCookie } from "better-auth/cookies";
@@ -5,10 +13,10 @@ import { z } from "zod/v4-mini";
5
13
 
6
14
  // src/utils.ts
7
15
  import { capitalize } from "lodash-es";
8
- function getOrigin(url) {
16
+ function getHostName(url) {
9
17
  try {
10
18
  const parsedUrl = new URL(url);
11
- return parsedUrl.origin;
19
+ return parsedUrl.hostname;
12
20
  } catch (_error) {
13
21
  return null;
14
22
  }
@@ -38,7 +46,7 @@ var guestList = (options) => {
38
46
  return {
39
47
  id: "guest-list",
40
48
  endpoints: {
41
- signInGuest: createAuthEndpoint(
49
+ signInGuestList: createAuthEndpoint(
42
50
  "/sign-in/guest-list",
43
51
  {
44
52
  method: "POST",
@@ -91,7 +99,7 @@ var guestList = (options) => {
91
99
  message: options?.revealNames ? `Name not on list. Try: ${JSON.stringify(Object.keys(guestLookup))}` : ERROR_CODES.NAME_NOT_ON_GUEST_LIST
92
100
  });
93
101
  }
94
- const { emailDomainName = getOrigin(ctx.context.baseURL) } = options ?? {};
102
+ const { emailDomainName = getHostName(ctx.context.baseURL) } = options ?? {};
95
103
  const email = `${cleanedName.toLowerCase().replaceAll(/\s/g, "")}.onguestlist@${emailDomainName}`;
96
104
  const found = await ctx.context.internalAdapter.findUserByEmail(email);
97
105
  async function createNewUser() {
@@ -160,14 +168,6 @@ var guestList = (options) => {
160
168
  $ERROR_CODES: ERROR_CODES
161
169
  };
162
170
  };
163
-
164
- // src/client.ts
165
- var guestListClient = () => {
166
- return {
167
- id: "guest-list",
168
- $InferServerPlugin: {}
169
- };
170
- };
171
171
  export {
172
172
  guestList,
173
173
  guestListClient
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nerdfolio/ba-guest-list",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Similar to anonymous, but with a name that must be on a guest list. Useful for testing or demo with fixed logins",
5
5
  "repository": "https://github.com/nerdfolio/better-auth-goodies",
6
6
  "type": "module",
@@ -11,6 +11,9 @@
11
11
  "require": "./dist/index.cjs"
12
12
  }
13
13
  },
14
+ "files": [
15
+ "dist"
16
+ ],
14
17
  "keywords": [
15
18
  "better-auth",
16
19
  "better-auth plugin",
package/src/client.ts DELETED
@@ -1,9 +0,0 @@
1
- import type { BetterAuthClientPlugin } from "better-auth"
2
- import type { guestList } from "./server"
3
-
4
- export const guestListClient = () => {
5
- return {
6
- id: "guest-list",
7
- $InferServerPlugin: {} as ReturnType<typeof guestList>,
8
- } satisfies BetterAuthClientPlugin
9
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { guestList, type GuestListOptions } from "./server"
2
- export { guestListClient } from "./client"
package/src/server.ts DELETED
@@ -1,200 +0,0 @@
1
- import type { BetterAuthPlugin } from "better-auth"
2
- import { APIError, createAuthEndpoint } from "better-auth/api"
3
- import { setSessionCookie } from "better-auth/cookies"
4
- import { z } from "zod/v4-mini"
5
- import { formatName, getOrigin } from "./utils"
6
-
7
- type GuestWithRole = {
8
- name: string
9
- role: string //comma-separated string
10
- }
11
-
12
- export interface GuestListOptions {
13
- /**
14
- * List of accepted guest names
15
- */
16
- allowGuests: string[] | GuestWithRole[]
17
-
18
- /**
19
- * When true returns the list of guest names via the guestList.reveal() endpoint and via errors.
20
- * When false returns nothing.
21
- * @default false
22
- */
23
- revealNames?: boolean
24
-
25
- /**
26
- * Configure the domain name of the temporary email
27
- * address for the guest users in the database.
28
- * @default "baseURL"
29
- */
30
- emailDomainName?: string
31
- }
32
-
33
- export const guestList = (options?: GuestListOptions) => {
34
- const ERROR_CODES = {
35
- NAME_NOT_PROVIDED: "Guest name not provided",
36
- NAME_NOT_ON_GUEST_LIST: "Your name is not on the guest list",
37
- NAME_ONE_WORD_ONLY: "Please only use 1-word names",
38
- FAILED_TO_CREATE_USER: "Failed to create user",
39
- COULD_NOT_CREATE_SESSION: "Could not create session",
40
- } as const
41
-
42
- const guestLookup = Object.fromEntries(
43
- (options?.allowGuests ?? [])
44
- .map((entry) => (typeof entry === "string" ? { name: entry, role: "" } : entry))
45
- .filter((entry) => !!entry && entry.name)
46
- .map(({ name, role }) => [
47
- formatName(name),
48
- {
49
- name: formatName(name),
50
- role: (role ?? "")
51
- .split(",")
52
- .map((s) => s.trim())
53
- .join(","),
54
- },
55
- ])
56
- )
57
-
58
- return {
59
- id: "guest-list",
60
- endpoints: {
61
- signInGuest: createAuthEndpoint(
62
- "/sign-in/guest-list",
63
- {
64
- method: "POST",
65
- body: z.object({
66
- name: z.string(),
67
- }),
68
- metadata: {
69
- openapi: {
70
- description: "Sign in as a guest with name only",
71
- responses: {
72
- 200: {
73
- description: "Sign in as a guest successful",
74
- content: {
75
- "application/json": {
76
- schema: {
77
- type: "object",
78
- properties: {
79
- user: {
80
- $ref: "#/components/schemas/User",
81
- },
82
- session: {
83
- $ref: "#/components/schemas/Session",
84
- },
85
- },
86
- },
87
- },
88
- },
89
- },
90
- },
91
- },
92
- },
93
- },
94
- async (ctx) => {
95
- const { name } = ctx.body
96
-
97
- if (!name) {
98
- ctx.context.logger.error("Guest name not provided")
99
- throw new APIError("UNAUTHORIZED", {
100
- message: options?.revealNames
101
- ? `Guest name not provided. Try: ${JSON.stringify(Object.keys(guestLookup))}`
102
- : ERROR_CODES.NAME_NOT_PROVIDED,
103
- })
104
- }
105
-
106
- if (name.trim().split(/\s+/).length > 1) {
107
- ctx.context.logger.error("For simplicity, only one word names are allowed")
108
- throw new APIError("UNAUTHORIZED", {
109
- message: ERROR_CODES.NAME_ONE_WORD_ONLY,
110
- })
111
- }
112
-
113
- const cleanedName = formatName(name)
114
-
115
- if (!guestLookup[cleanedName]) {
116
- throw new APIError("UNAUTHORIZED", {
117
- message: options?.revealNames
118
- ? `Name not on list. Try: ${JSON.stringify(Object.keys(guestLookup))}`
119
- : ERROR_CODES.NAME_NOT_ON_GUEST_LIST,
120
- })
121
- }
122
-
123
- // generate email based the input name
124
- const { emailDomainName = getOrigin(ctx.context.baseURL) } = options ?? {}
125
- const email = `${cleanedName.toLowerCase().replaceAll(/\s/g, "")}.onguestlist@${emailDomainName}`
126
-
127
- const found = await ctx.context.internalAdapter.findUserByEmail(email)
128
-
129
- async function createNewUser() {
130
- const newUser = await ctx.context.internalAdapter.createUser(
131
- {
132
- email,
133
- emailVerified: false,
134
- name: cleanedName,
135
- role: guestLookup[cleanedName].role,
136
- createdAt: new Date(),
137
- updatedAt: new Date(),
138
- },
139
- ctx
140
- )
141
- if (!newUser) {
142
- throw ctx.error("INTERNAL_SERVER_ERROR", {
143
- message: ERROR_CODES.FAILED_TO_CREATE_USER,
144
- })
145
- }
146
-
147
- return newUser
148
- }
149
-
150
- const user = found ? found.user : await createNewUser()
151
-
152
- const session = await ctx.context.internalAdapter.createSession(user.id, ctx, true)
153
-
154
- if (!session) {
155
- return ctx.json(null, {
156
- status: 400,
157
- body: {
158
- message: ERROR_CODES.COULD_NOT_CREATE_SESSION,
159
- },
160
- })
161
- }
162
- await setSessionCookie(ctx, { session, user })
163
-
164
- return ctx.json({ token: session.token, user })
165
- }
166
- ),
167
-
168
- revealGuestList: createAuthEndpoint(
169
- "/sign-in/guest-list/reveal",
170
- {
171
- method: "GET",
172
- metadata: {
173
- openapi: {
174
- description: "Reveal guest list if 'revealNames' is enabled. Empty array otherwise",
175
- responses: {
176
- 200: {
177
- description: "List of allowed guest names or empty array",
178
- content: {
179
- "application/json": {
180
- schema: {
181
- type: "array",
182
- items: {
183
- type: "string",
184
- },
185
- },
186
- },
187
- },
188
- },
189
- },
190
- },
191
- },
192
- },
193
- async (ctx) => {
194
- return ctx.json(options?.revealNames ? Object.keys(guestLookup) : [])
195
- }
196
- ),
197
- },
198
- $ERROR_CODES: ERROR_CODES,
199
- } satisfies BetterAuthPlugin
200
- }
package/src/utils.ts DELETED
@@ -1,14 +0,0 @@
1
- import { capitalize } from "lodash-es"
2
-
3
- export function getOrigin(url: string) {
4
- try {
5
- const parsedUrl = new URL(url)
6
- return parsedUrl.origin
7
- } catch (_error) {
8
- return null
9
- }
10
- }
11
-
12
- export function formatName(name: string) {
13
- return capitalize(name.replaceAll(/\s/g, ""))
14
- }
package/tsup.config.ts DELETED
@@ -1,15 +0,0 @@
1
- import { type Options, defineConfig } from "tsup"
2
-
3
- const libCfg = {
4
- entry: ["src/index.ts"],
5
- splitting: false,
6
- target: "node22",
7
- format: ["esm", "cjs"],
8
- dts: true,
9
- clean: true,
10
- outExtension({ format }) {
11
- return { js: format === "esm" ? ".mjs" : format === "cjs" ? ".cjs" : ".js" }
12
- },
13
- } satisfies Options
14
-
15
- export default defineConfig(libCfg)