@nerdfolio/ba-guest-list 0.0.1 → 0.0.3
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/README.md +15 -8
- package/dist/index.cjs +24 -10
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.mjs +21 -10
- package/package.json +4 -7
- package/src/client.ts +1 -4
- package/src/index.ts +2 -205
- package/src/server.ts +200 -0
- package/src/utils.ts +6 -0
- package/tsup.config.ts +1 -1
- package/dist/client.cjs +0 -38
- package/dist/client.d.cts +0 -10
- package/dist/client.d.ts +0 -10
- package/dist/client.mjs +0 -13
package/README.md
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
# Better Auth Guest List
|
|
2
|
-
Plugin to provide fixed guest list functionality. Intended use is for development testing and possibly demos with
|
|
3
|
-
know login names, e.g. login as "Alice" or "Bob".
|
|
4
2
|
|
|
5
|
-
|
|
3
|
+
Plugin to provide fixed guest list login functionality for [better-auth](https://www.better-auth.com).
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
Intended use is for development testing and possibly demos with known login names, e.g. login as "Alice" or "Bob". You can use it with a single-input form or binding a name to a submit button.
|
|
6
|
+
|
|
7
|
+
The fixed guest list is defined on the server-side with optional roles so this plugin can also be used for testing roles.
|
|
8
|
+
You can optionally reveal the server guest list to the client (useful for demo login scenarios).
|
|
9
|
+
|
|
10
|
+
THIS IS NOT MEANT FOR SECURE PRODUCTION APP!
|
|
11
|
+
|
|
12
|
+
# How It Works
|
|
13
|
+
|
|
14
|
+
This plugin does not add any field to the schema as its intended use is temporary for testing and demos.
|
|
15
|
+
|
|
16
|
+
Internally, the guest name is transformed into an email via a fixed template, e.g. `tom.onguestlist@emaildomain` and that is the way that user will be looked up. For simplicity, the guest names are restricted to be 1-word names.
|
|
9
17
|
|
|
10
|
-
As this plugin is only intended to be a temporary aid to development and demo, it does not add any field to the schema. Internally, the guest name is transformed into an email via a fixed template, e.g. `tom.onguestlist@emaildomain` and that is the way that user will be looked up.
|
|
11
18
|
|
|
12
19
|
# Installation
|
|
13
20
|
|
|
@@ -46,7 +53,7 @@ export const auth = betterAuth({
|
|
|
46
53
|
|
|
47
54
|
Options:
|
|
48
55
|
|
|
49
|
-
`allowGuests`: can be an array of names or array of `{name: string, role?: string}`.
|
|
56
|
+
`allowGuests`: can be an array of names or array of `{name: string, role?: string}`. Role follows better-auth convention as a comma-separated string of actual roles.
|
|
50
57
|
|
|
51
58
|
`revealNames`: is a boolean. When enabled, the client will be able to retrieve the guest names via `client.signIn.guestList.reveal()`. Names may also be returned in api errors during logins. When undefined or disabled, the `reveal()` endpoint will just return `null` and names will not be sent in error messages.
|
|
52
59
|
|
|
@@ -57,7 +64,7 @@ Options:
|
|
|
57
64
|
|
|
58
65
|
```typescript
|
|
59
66
|
|
|
60
|
-
import { guestListClient } from "@nerdfolio/ba-guest-list
|
|
67
|
+
import { guestListClient } from "@nerdfolio/ba-guest-list"
|
|
61
68
|
|
|
62
69
|
export const authClient = createAuthClient({
|
|
63
70
|
plugins: [
|
package/dist/index.cjs
CHANGED
|
@@ -20,15 +20,18 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
guestList: () => guestList
|
|
23
|
+
guestList: () => guestList,
|
|
24
|
+
guestListClient: () => guestListClient
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/server.ts
|
|
26
29
|
var import_api = require("better-auth/api");
|
|
27
30
|
var import_cookies = require("better-auth/cookies");
|
|
28
|
-
var import_lodash_es = require("lodash-es");
|
|
29
31
|
var import_v4_mini = require("zod/v4-mini");
|
|
30
32
|
|
|
31
33
|
// src/utils.ts
|
|
34
|
+
var import_lodash_es = require("lodash-es");
|
|
32
35
|
function getOrigin(url) {
|
|
33
36
|
try {
|
|
34
37
|
const parsedUrl = new URL(url);
|
|
@@ -37,11 +40,11 @@ function getOrigin(url) {
|
|
|
37
40
|
return null;
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
|
-
|
|
41
|
-
// src/index.ts
|
|
42
43
|
function formatName(name) {
|
|
43
44
|
return (0, import_lodash_es.capitalize)(name.replaceAll(/\s/g, ""));
|
|
44
45
|
}
|
|
46
|
+
|
|
47
|
+
// src/server.ts
|
|
45
48
|
var guestList = (options) => {
|
|
46
49
|
const ERROR_CODES = {
|
|
47
50
|
NAME_NOT_PROVIDED: "Guest name not provided",
|
|
@@ -51,10 +54,13 @@ var guestList = (options) => {
|
|
|
51
54
|
COULD_NOT_CREATE_SESSION: "Could not create session"
|
|
52
55
|
};
|
|
53
56
|
const guestLookup = Object.fromEntries(
|
|
54
|
-
(options?.allowGuests ?? []).map((entry) => typeof entry === "string" ? { name: entry, role: "" } : entry).filter((entry) => !!entry && entry.name).map(({ name, role }) => [
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
(options?.allowGuests ?? []).map((entry) => typeof entry === "string" ? { name: entry, role: "" } : entry).filter((entry) => !!entry && entry.name).map(({ name, role }) => [
|
|
58
|
+
formatName(name),
|
|
59
|
+
{
|
|
60
|
+
name: formatName(name),
|
|
61
|
+
role: (role ?? "").split(",").map((s) => s.trim()).join(",")
|
|
62
|
+
}
|
|
63
|
+
])
|
|
58
64
|
);
|
|
59
65
|
return {
|
|
60
66
|
id: "guest-list",
|
|
@@ -178,11 +184,19 @@ var guestList = (options) => {
|
|
|
178
184
|
}
|
|
179
185
|
)
|
|
180
186
|
},
|
|
181
|
-
//schema: mergeSchema(schema, options?.schema),
|
|
182
187
|
$ERROR_CODES: ERROR_CODES
|
|
183
188
|
};
|
|
184
189
|
};
|
|
190
|
+
|
|
191
|
+
// src/client.ts
|
|
192
|
+
var guestListClient = () => {
|
|
193
|
+
return {
|
|
194
|
+
id: "guest-list",
|
|
195
|
+
$InferServerPlugin: {}
|
|
196
|
+
};
|
|
197
|
+
};
|
|
185
198
|
// Annotate the CommonJS export names for ESM import in node:
|
|
186
199
|
0 && (module.exports = {
|
|
187
|
-
guestList
|
|
200
|
+
guestList,
|
|
201
|
+
guestListClient
|
|
188
202
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -172,4 +172,9 @@ declare const guestList: (options?: GuestListOptions) => {
|
|
|
172
172
|
};
|
|
173
173
|
};
|
|
174
174
|
|
|
175
|
-
|
|
175
|
+
declare const guestListClient: () => {
|
|
176
|
+
id: "guest-list";
|
|
177
|
+
$InferServerPlugin: ReturnType<typeof guestList>;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export { type GuestListOptions, guestList, guestListClient };
|
package/dist/index.d.ts
CHANGED
|
@@ -172,4 +172,9 @@ declare const guestList: (options?: GuestListOptions) => {
|
|
|
172
172
|
};
|
|
173
173
|
};
|
|
174
174
|
|
|
175
|
-
|
|
175
|
+
declare const guestListClient: () => {
|
|
176
|
+
id: "guest-list";
|
|
177
|
+
$InferServerPlugin: ReturnType<typeof guestList>;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export { type GuestListOptions, guestList, guestListClient };
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/server.ts
|
|
2
2
|
import { APIError, createAuthEndpoint } from "better-auth/api";
|
|
3
3
|
import { setSessionCookie } from "better-auth/cookies";
|
|
4
|
-
import { capitalize } from "lodash-es";
|
|
5
4
|
import { z } from "zod/v4-mini";
|
|
6
5
|
|
|
7
6
|
// src/utils.ts
|
|
7
|
+
import { capitalize } from "lodash-es";
|
|
8
8
|
function getOrigin(url) {
|
|
9
9
|
try {
|
|
10
10
|
const parsedUrl = new URL(url);
|
|
@@ -13,11 +13,11 @@ function getOrigin(url) {
|
|
|
13
13
|
return null;
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
// src/index.ts
|
|
18
16
|
function formatName(name) {
|
|
19
17
|
return capitalize(name.replaceAll(/\s/g, ""));
|
|
20
18
|
}
|
|
19
|
+
|
|
20
|
+
// src/server.ts
|
|
21
21
|
var guestList = (options) => {
|
|
22
22
|
const ERROR_CODES = {
|
|
23
23
|
NAME_NOT_PROVIDED: "Guest name not provided",
|
|
@@ -27,10 +27,13 @@ var guestList = (options) => {
|
|
|
27
27
|
COULD_NOT_CREATE_SESSION: "Could not create session"
|
|
28
28
|
};
|
|
29
29
|
const guestLookup = Object.fromEntries(
|
|
30
|
-
(options?.allowGuests ?? []).map((entry) => typeof entry === "string" ? { name: entry, role: "" } : entry).filter((entry) => !!entry && entry.name).map(({ name, role }) => [
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
(options?.allowGuests ?? []).map((entry) => typeof entry === "string" ? { name: entry, role: "" } : entry).filter((entry) => !!entry && entry.name).map(({ name, role }) => [
|
|
31
|
+
formatName(name),
|
|
32
|
+
{
|
|
33
|
+
name: formatName(name),
|
|
34
|
+
role: (role ?? "").split(",").map((s) => s.trim()).join(",")
|
|
35
|
+
}
|
|
36
|
+
])
|
|
34
37
|
);
|
|
35
38
|
return {
|
|
36
39
|
id: "guest-list",
|
|
@@ -154,10 +157,18 @@ var guestList = (options) => {
|
|
|
154
157
|
}
|
|
155
158
|
)
|
|
156
159
|
},
|
|
157
|
-
//schema: mergeSchema(schema, options?.schema),
|
|
158
160
|
$ERROR_CODES: ERROR_CODES
|
|
159
161
|
};
|
|
160
162
|
};
|
|
163
|
+
|
|
164
|
+
// src/client.ts
|
|
165
|
+
var guestListClient = () => {
|
|
166
|
+
return {
|
|
167
|
+
id: "guest-list",
|
|
168
|
+
$InferServerPlugin: {}
|
|
169
|
+
};
|
|
170
|
+
};
|
|
161
171
|
export {
|
|
162
|
-
guestList
|
|
172
|
+
guestList,
|
|
173
|
+
guestListClient
|
|
163
174
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nerdfolio/ba-guest-list",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
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
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -8,18 +8,15 @@
|
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"import": "./dist/index.mjs",
|
|
10
10
|
"require": "./dist/index.cjs"
|
|
11
|
-
},
|
|
12
|
-
"./client": {
|
|
13
|
-
"types": "./dist/client.d.ts",
|
|
14
|
-
"import": "./dist/client.mjs",
|
|
15
|
-
"require": "./dist/client.cjs"
|
|
16
11
|
}
|
|
17
12
|
},
|
|
18
13
|
"keywords": [
|
|
19
14
|
"better-auth",
|
|
20
15
|
"better-auth plugin",
|
|
21
16
|
"guest login",
|
|
22
|
-
"demo login"
|
|
17
|
+
"demo login",
|
|
18
|
+
"anonymous login",
|
|
19
|
+
"auth testing"
|
|
23
20
|
],
|
|
24
21
|
"author": "taivo@github",
|
|
25
22
|
"license": "ISC",
|
package/src/client.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import type { BetterAuthClientPlugin } from "better-auth"
|
|
2
|
-
import type { guestList } from "
|
|
2
|
+
import type { guestList } from "./server"
|
|
3
3
|
|
|
4
4
|
export const guestListClient = () => {
|
|
5
5
|
return {
|
|
6
6
|
id: "guest-list",
|
|
7
7
|
$InferServerPlugin: {} as ReturnType<typeof guestList>,
|
|
8
|
-
// pathMethods: {
|
|
9
|
-
// "/sign-in/guest-list": "POST",
|
|
10
|
-
// },
|
|
11
8
|
} satisfies BetterAuthClientPlugin
|
|
12
9
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,205 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { setSessionCookie } from "better-auth/cookies"
|
|
4
|
-
import { capitalize } from "lodash-es"
|
|
5
|
-
import { z } from "zod/v4-mini"
|
|
6
|
-
import { getOrigin } from "./utils"
|
|
7
|
-
|
|
8
|
-
type GuestWithRole = {
|
|
9
|
-
name: string,
|
|
10
|
-
role: string //comma-separated string
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface GuestListOptions {
|
|
14
|
-
/**
|
|
15
|
-
* List of accepted guest names
|
|
16
|
-
*/
|
|
17
|
-
allowGuests: string[] | GuestWithRole[]
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* When true returns the list of guest names via the guestList.reveal() endpoint and via errors.
|
|
21
|
-
* When false returns nothing.
|
|
22
|
-
* @default false
|
|
23
|
-
*/
|
|
24
|
-
revealNames?: boolean
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Configure the domain name of the temporary email
|
|
28
|
-
* address for the guest users in the database.
|
|
29
|
-
* @default "baseURL"
|
|
30
|
-
*/
|
|
31
|
-
emailDomainName?: string
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Custom schema for the anonymous plugin
|
|
35
|
-
*/
|
|
36
|
-
// schema?: InferOptionSchema<typeof schema>
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function formatName(name: string) {
|
|
40
|
-
return capitalize(name.replaceAll(/\s/g, ""))
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const guestList = (options?: GuestListOptions) => {
|
|
44
|
-
const ERROR_CODES = {
|
|
45
|
-
NAME_NOT_PROVIDED: "Guest name not provided",
|
|
46
|
-
NAME_NOT_ON_GUEST_LIST: "Your name is not on the guest list",
|
|
47
|
-
NAME_ONE_WORD_ONLY: "Please only use 1-word names",
|
|
48
|
-
FAILED_TO_CREATE_USER: "Failed to create user",
|
|
49
|
-
COULD_NOT_CREATE_SESSION: "Could not create session",
|
|
50
|
-
} as const
|
|
51
|
-
|
|
52
|
-
const guestLookup = Object.fromEntries(
|
|
53
|
-
(options?.allowGuests ?? [])
|
|
54
|
-
.map((entry) => typeof entry === "string" ? { name: entry, role: "" } : entry)
|
|
55
|
-
.filter(entry => !!entry && entry.name)
|
|
56
|
-
.map(({ name, role }) => [formatName(name), {
|
|
57
|
-
name: formatName(name),
|
|
58
|
-
role: (role ?? "").split(",").map(s => s.trim()).join(",")
|
|
59
|
-
}])
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
id: "guest-list",
|
|
64
|
-
endpoints: {
|
|
65
|
-
signInGuest: createAuthEndpoint(
|
|
66
|
-
"/sign-in/guest-list",
|
|
67
|
-
{
|
|
68
|
-
method: "POST",
|
|
69
|
-
body: z.object({
|
|
70
|
-
name: z.string(),
|
|
71
|
-
}),
|
|
72
|
-
metadata: {
|
|
73
|
-
openapi: {
|
|
74
|
-
description: "Sign in as a guest with name only",
|
|
75
|
-
responses: {
|
|
76
|
-
200: {
|
|
77
|
-
description: "Sign in as a guest successful",
|
|
78
|
-
content: {
|
|
79
|
-
"application/json": {
|
|
80
|
-
schema: {
|
|
81
|
-
type: "object",
|
|
82
|
-
properties: {
|
|
83
|
-
user: {
|
|
84
|
-
$ref: "#/components/schemas/User",
|
|
85
|
-
},
|
|
86
|
-
session: {
|
|
87
|
-
$ref: "#/components/schemas/Session",
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
async (ctx) => {
|
|
99
|
-
const { name } = ctx.body
|
|
100
|
-
|
|
101
|
-
if (!name) {
|
|
102
|
-
ctx.context.logger.error("Guest name not provided")
|
|
103
|
-
throw new APIError("UNAUTHORIZED", {
|
|
104
|
-
message: options?.revealNames
|
|
105
|
-
? `Guest name not provided. Try: ${JSON.stringify(Object.keys(guestLookup))}` : ERROR_CODES.NAME_NOT_PROVIDED,
|
|
106
|
-
})
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (name.trim().split(/\s+/).length > 1) {
|
|
110
|
-
ctx.context.logger.error("For simplicity, only one word names are allowed")
|
|
111
|
-
throw new APIError("UNAUTHORIZED", {
|
|
112
|
-
message: ERROR_CODES.NAME_ONE_WORD_ONLY,
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const cleanedName = formatName(name)
|
|
117
|
-
|
|
118
|
-
if (!guestLookup[cleanedName]) {
|
|
119
|
-
throw new APIError("UNAUTHORIZED", {
|
|
120
|
-
message: options?.revealNames
|
|
121
|
-
? `Name not on list. Try: ${JSON.stringify(Object.keys(guestLookup))}`
|
|
122
|
-
: ERROR_CODES.NAME_NOT_ON_GUEST_LIST,
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// generate email based the input name
|
|
127
|
-
const { emailDomainName = getOrigin(ctx.context.baseURL) } = options ?? {}
|
|
128
|
-
const email = `${cleanedName.toLowerCase().replaceAll(/\s/g, "")}.onguestlist@${emailDomainName}`
|
|
129
|
-
|
|
130
|
-
const found = await ctx.context.internalAdapter.findUserByEmail(email)
|
|
131
|
-
|
|
132
|
-
async function createNewUser() {
|
|
133
|
-
const newUser = await ctx.context.internalAdapter.createUser(
|
|
134
|
-
{
|
|
135
|
-
email,
|
|
136
|
-
emailVerified: false,
|
|
137
|
-
name: cleanedName,
|
|
138
|
-
role: guestLookup[cleanedName].role,
|
|
139
|
-
createdAt: new Date(),
|
|
140
|
-
updatedAt: new Date(),
|
|
141
|
-
},
|
|
142
|
-
ctx
|
|
143
|
-
)
|
|
144
|
-
if (!newUser) {
|
|
145
|
-
throw ctx.error("INTERNAL_SERVER_ERROR", {
|
|
146
|
-
message: ERROR_CODES.FAILED_TO_CREATE_USER,
|
|
147
|
-
})
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return newUser
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const user = found ? found.user : await createNewUser()
|
|
154
|
-
|
|
155
|
-
const session = await ctx.context.internalAdapter.createSession(user.id, ctx, true)
|
|
156
|
-
|
|
157
|
-
if (!session) {
|
|
158
|
-
return ctx.json(null, {
|
|
159
|
-
status: 400,
|
|
160
|
-
body: {
|
|
161
|
-
message: ERROR_CODES.COULD_NOT_CREATE_SESSION,
|
|
162
|
-
},
|
|
163
|
-
})
|
|
164
|
-
}
|
|
165
|
-
await setSessionCookie(ctx, { session, user })
|
|
166
|
-
|
|
167
|
-
return ctx.json({ token: session.token, user })
|
|
168
|
-
}
|
|
169
|
-
),
|
|
170
|
-
|
|
171
|
-
revealGuestList: createAuthEndpoint(
|
|
172
|
-
"/sign-in/guest-list/reveal",
|
|
173
|
-
{
|
|
174
|
-
method: "GET",
|
|
175
|
-
metadata: {
|
|
176
|
-
openapi: {
|
|
177
|
-
description: "Reveal guest list if 'revealNames' is enabled. Empty array otherwise",
|
|
178
|
-
responses: {
|
|
179
|
-
200: {
|
|
180
|
-
description: "List of allowed guest names or empty array",
|
|
181
|
-
content: {
|
|
182
|
-
"application/json": {
|
|
183
|
-
schema: {
|
|
184
|
-
type: "array",
|
|
185
|
-
items: {
|
|
186
|
-
type: "string",
|
|
187
|
-
},
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
},
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
async (ctx) => {
|
|
197
|
-
return ctx.json(options?.revealNames ? Object.keys(guestLookup) : [])
|
|
198
|
-
}
|
|
199
|
-
)
|
|
200
|
-
},
|
|
201
|
-
|
|
202
|
-
//schema: mergeSchema(schema, options?.schema),
|
|
203
|
-
$ERROR_CODES: ERROR_CODES,
|
|
204
|
-
} satisfies BetterAuthPlugin
|
|
205
|
-
}
|
|
1
|
+
export { guestList, type GuestListOptions } from "./server"
|
|
2
|
+
export { guestListClient } from "./client"
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
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
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { capitalize } from "lodash-es"
|
|
2
|
+
|
|
1
3
|
export function getOrigin(url: string) {
|
|
2
4
|
try {
|
|
3
5
|
const parsedUrl = new URL(url)
|
|
@@ -5,4 +7,8 @@ export function getOrigin(url: string) {
|
|
|
5
7
|
} catch (_error) {
|
|
6
8
|
return null
|
|
7
9
|
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function formatName(name: string) {
|
|
13
|
+
return capitalize(name.replaceAll(/\s/g, ""))
|
|
8
14
|
}
|
package/tsup.config.ts
CHANGED
package/dist/client.cjs
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/client.ts
|
|
21
|
-
var client_exports = {};
|
|
22
|
-
__export(client_exports, {
|
|
23
|
-
guestListClient: () => guestListClient
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(client_exports);
|
|
26
|
-
var guestListClient = () => {
|
|
27
|
-
return {
|
|
28
|
-
id: "guest-list",
|
|
29
|
-
$InferServerPlugin: {}
|
|
30
|
-
// pathMethods: {
|
|
31
|
-
// "/sign-in/guest-list": "POST",
|
|
32
|
-
// },
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
-
0 && (module.exports = {
|
|
37
|
-
guestListClient
|
|
38
|
-
});
|
package/dist/client.d.cts
DELETED
package/dist/client.d.ts
DELETED