sign-in-with-stacks 0.3.0 → 0.4.0
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/better-auth-BZJrlwu8.js +284 -0
- package/dist/better-auth-BZJrlwu8.js.map +1 -0
- package/dist/client-BywC0Cfs.d.ts +114 -0
- package/dist/client-DTkU-AMP.js +20 -0
- package/dist/client-DTkU-AMP.js.map +1 -0
- package/dist/index.d.ts +9 -14
- package/dist/index.js +2 -3
- package/dist/plugins/better-auth/client.d.ts +2 -10
- package/dist/plugins/better-auth/client.js +1 -10
- package/dist/plugins/better-auth/index.d.ts +1 -2
- package/dist/plugins/better-auth/index.js +3 -4
- package/dist/plugins/index.d.ts +1 -2
- package/dist/plugins/index.js +3 -4
- package/dist/{verifySiwsMessage-j433CJQY.js → src-BvqyQzSI.js} +46 -27
- package/dist/src-BvqyQzSI.js.map +1 -0
- package/package.json +22 -13
- package/src/createSiwsMessage.ts +7 -10
- package/src/parseSiwsMessage.ts +42 -17
- package/src/plugins/better-auth/client.ts +6 -0
- package/src/plugins/better-auth/plugin.ts +222 -73
- package/src/plugins/better-auth/schema.ts +3 -0
- package/src/plugins/better-auth/types.ts +16 -0
- package/src/utils.ts +18 -3
- package/src/validateSiwsMessage.ts +15 -3
- package/src/verifySiwsMessage.ts +2 -1
- package/src/version.ts +3 -0
- package/dist/plugin-ChLeFAxQ.js +0 -218
- package/dist/plugin-ChLeFAxQ.js.map +0 -1
- package/dist/plugin-ClFcfxqg.d.ts +0 -77
- package/dist/plugins/better-auth/client.js.map +0 -1
- package/dist/verifySiwsMessage-j433CJQY.js.map +0 -1
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { i as generateSiwsNonce, n as parseSiwsMessage, o as getAddress, s as isAddress, t as verifySiwsMessage } from "./src-BvqyQzSI.js";
|
|
2
|
+
import { n as PACKAGE_VERSION } from "./client-DTkU-AMP.js";
|
|
3
|
+
import { createPlaceholderEmail } from "@better-auth/core/utils/email";
|
|
4
|
+
import { APIError, createAuthEndpoint, isAPIError } from "better-auth/api";
|
|
5
|
+
import { setSessionCookie } from "better-auth/cookies";
|
|
6
|
+
import { mergeSchema } from "better-auth/db";
|
|
7
|
+
import * as z from "zod";
|
|
8
|
+
//#region src/plugins/better-auth/schema.ts
|
|
9
|
+
const schema = { walletAddress: { fields: {
|
|
10
|
+
userId: {
|
|
11
|
+
type: "string",
|
|
12
|
+
references: {
|
|
13
|
+
model: "user",
|
|
14
|
+
field: "id"
|
|
15
|
+
},
|
|
16
|
+
required: true,
|
|
17
|
+
index: true
|
|
18
|
+
},
|
|
19
|
+
address: {
|
|
20
|
+
type: "string",
|
|
21
|
+
required: true
|
|
22
|
+
},
|
|
23
|
+
chainId: {
|
|
24
|
+
type: "number",
|
|
25
|
+
required: true,
|
|
26
|
+
bigint: true
|
|
27
|
+
},
|
|
28
|
+
isPrimary: {
|
|
29
|
+
type: "boolean",
|
|
30
|
+
defaultValue: false
|
|
31
|
+
},
|
|
32
|
+
createdAt: {
|
|
33
|
+
type: "date",
|
|
34
|
+
required: true
|
|
35
|
+
}
|
|
36
|
+
} } };
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/plugins/better-auth/plugin.ts
|
|
39
|
+
const SIWS_VERIFICATION_IDENTIFIER_PREFIX = "siws:";
|
|
40
|
+
const SIWS_NONCE_MAX_LENGTH = 250;
|
|
41
|
+
const SIWS_NONCE_ALPHANUMERIC_REGEX = /^[a-zA-Z0-9]+$/;
|
|
42
|
+
const SIWS_MAX_CHAIN_ID = 4294967295;
|
|
43
|
+
const isValidSiwsNonce = (nonce) => typeof nonce === "string" && nonce.length >= 8 && nonce.length <= SIWS_NONCE_MAX_LENGTH && SIWS_NONCE_ALPHANUMERIC_REGEX.test(nonce);
|
|
44
|
+
const siwsMessageMismatchError = () => new APIError("UNAUTHORIZED", {
|
|
45
|
+
message: "Unauthorized: SIWS message does not match the expected domain, address, chain ID, or nonce",
|
|
46
|
+
status: 401,
|
|
47
|
+
code: "UNAUTHORIZED_SIWS_MESSAGE_MISMATCH"
|
|
48
|
+
});
|
|
49
|
+
const siws = (options) => {
|
|
50
|
+
const verifyMessage = options.verifyMessage ?? (async (args) => {
|
|
51
|
+
const { nonce } = parseSiwsMessage(args.message);
|
|
52
|
+
return verifySiwsMessage({
|
|
53
|
+
message: args.message,
|
|
54
|
+
signature: args.signature,
|
|
55
|
+
address: args.address,
|
|
56
|
+
domain: options.domain,
|
|
57
|
+
nonce
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
return {
|
|
61
|
+
id: "siws",
|
|
62
|
+
version: PACKAGE_VERSION,
|
|
63
|
+
schema: mergeSchema(schema, options?.schema),
|
|
64
|
+
endpoints: {
|
|
65
|
+
nonce: createAuthEndpoint("/siws/nonce", {
|
|
66
|
+
method: "POST",
|
|
67
|
+
body: z.object({}).strict().optional()
|
|
68
|
+
}, async (ctx) => {
|
|
69
|
+
const nonce = options.getNonce ? await options.getNonce() : generateSiwsNonce();
|
|
70
|
+
if (!isValidSiwsNonce(nonce)) throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
71
|
+
message: `SIWS getNonce must return a nonce of 8-${SIWS_NONCE_MAX_LENGTH} alphanumeric characters.`,
|
|
72
|
+
status: 500,
|
|
73
|
+
code: "SIWS_INVALID_NONCE"
|
|
74
|
+
});
|
|
75
|
+
await ctx.context.internalAdapter.createVerificationValue({
|
|
76
|
+
identifier: `${SIWS_VERIFICATION_IDENTIFIER_PREFIX}${nonce}`,
|
|
77
|
+
value: nonce,
|
|
78
|
+
expiresAt: new Date(Date.now() + 9e5)
|
|
79
|
+
});
|
|
80
|
+
return ctx.json({ nonce });
|
|
81
|
+
}),
|
|
82
|
+
verify: createAuthEndpoint("/siws/verify", {
|
|
83
|
+
method: "POST",
|
|
84
|
+
body: z.object({
|
|
85
|
+
message: z.string().min(1),
|
|
86
|
+
signature: z.string().min(1),
|
|
87
|
+
email: z.email().optional()
|
|
88
|
+
}).strict().refine((data) => options.anonymous !== false || !!data.email, {
|
|
89
|
+
message: "Email is required when the anonymous plugin option is disabled.",
|
|
90
|
+
path: ["email"]
|
|
91
|
+
}),
|
|
92
|
+
requireRequest: true
|
|
93
|
+
}, async (ctx) => {
|
|
94
|
+
const { message, signature, email } = ctx.body;
|
|
95
|
+
const isAnon = options.anonymous ?? true;
|
|
96
|
+
if (!isAnon && !email) throw new APIError("BAD_REQUEST", {
|
|
97
|
+
message: "Email is required when anonymous is disabled.",
|
|
98
|
+
status: 400
|
|
99
|
+
});
|
|
100
|
+
try {
|
|
101
|
+
const parsedMessage = parseSiwsMessage(message);
|
|
102
|
+
const { address, chainId, nonce } = parsedMessage;
|
|
103
|
+
if (!address || !isAddress(address) || !isValidSiwsNonce(nonce) || typeof chainId !== "number" || !Number.isInteger(chainId) || chainId < 1 || chainId > SIWS_MAX_CHAIN_ID || parsedMessage.domain !== options.domain) throw siwsMessageMismatchError();
|
|
104
|
+
const walletAddress = getAddress(address);
|
|
105
|
+
const now = /* @__PURE__ */ new Date();
|
|
106
|
+
if (parsedMessage.expirationTime) {
|
|
107
|
+
if (Number.isNaN(parsedMessage.expirationTime.getTime()) || now >= parsedMessage.expirationTime) throw new APIError("UNAUTHORIZED", {
|
|
108
|
+
message: "Unauthorized: SIWS message has expired",
|
|
109
|
+
status: 401,
|
|
110
|
+
code: "UNAUTHORIZED_SIWS_MESSAGE_EXPIRED"
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (parsedMessage.notBefore) {
|
|
114
|
+
if (Number.isNaN(parsedMessage.notBefore.getTime()) || now < parsedMessage.notBefore) throw new APIError("UNAUTHORIZED", {
|
|
115
|
+
message: "Unauthorized: SIWS message is not yet valid",
|
|
116
|
+
status: 401,
|
|
117
|
+
code: "UNAUTHORIZED_SIWS_MESSAGE_NOT_YET_VALID"
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (!await ctx.context.internalAdapter.consumeVerificationValue(`${SIWS_VERIFICATION_IDENTIFIER_PREFIX}${nonce}`)) throw new APIError("UNAUTHORIZED", {
|
|
121
|
+
message: "Unauthorized: Invalid or expired nonce",
|
|
122
|
+
status: 401,
|
|
123
|
+
code: "UNAUTHORIZED_INVALID_OR_EXPIRED_NONCE"
|
|
124
|
+
});
|
|
125
|
+
if (!await verifyMessage({
|
|
126
|
+
message,
|
|
127
|
+
signature,
|
|
128
|
+
address: walletAddress,
|
|
129
|
+
chainId
|
|
130
|
+
})) throw new APIError("UNAUTHORIZED", {
|
|
131
|
+
message: "Unauthorized: Invalid SIWS signature",
|
|
132
|
+
status: 401
|
|
133
|
+
});
|
|
134
|
+
let user = null;
|
|
135
|
+
const existingWalletAddress = await ctx.context.adapter.findOne({
|
|
136
|
+
model: "walletAddress",
|
|
137
|
+
where: [{
|
|
138
|
+
field: "address",
|
|
139
|
+
operator: "eq",
|
|
140
|
+
value: walletAddress
|
|
141
|
+
}, {
|
|
142
|
+
field: "chainId",
|
|
143
|
+
operator: "eq",
|
|
144
|
+
value: chainId
|
|
145
|
+
}]
|
|
146
|
+
});
|
|
147
|
+
if (existingWalletAddress) user = await ctx.context.adapter.findOne({
|
|
148
|
+
model: "user",
|
|
149
|
+
where: [{
|
|
150
|
+
field: "id",
|
|
151
|
+
operator: "eq",
|
|
152
|
+
value: existingWalletAddress.userId
|
|
153
|
+
}]
|
|
154
|
+
});
|
|
155
|
+
else {
|
|
156
|
+
const anyWalletAddress = await ctx.context.adapter.findOne({
|
|
157
|
+
model: "walletAddress",
|
|
158
|
+
where: [{
|
|
159
|
+
field: "address",
|
|
160
|
+
operator: "eq",
|
|
161
|
+
value: walletAddress
|
|
162
|
+
}]
|
|
163
|
+
});
|
|
164
|
+
if (anyWalletAddress) user = await ctx.context.adapter.findOne({
|
|
165
|
+
model: "user",
|
|
166
|
+
where: [{
|
|
167
|
+
field: "id",
|
|
168
|
+
operator: "eq",
|
|
169
|
+
value: anyWalletAddress.userId
|
|
170
|
+
}]
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
if (!user) {
|
|
174
|
+
const normalizedEmail = email?.toLowerCase();
|
|
175
|
+
const walletEmail = options.emailDomainName ? `${walletAddress}@${options.emailDomainName}` : createPlaceholderEmail({
|
|
176
|
+
identifier: walletAddress,
|
|
177
|
+
namespace: "siws"
|
|
178
|
+
});
|
|
179
|
+
let userEmail = walletEmail;
|
|
180
|
+
let emailClaimIdentifier;
|
|
181
|
+
if (!isAnon && normalizedEmail) {
|
|
182
|
+
const identifier = `siws-email-claim-${normalizedEmail}`;
|
|
183
|
+
let reserved = false;
|
|
184
|
+
try {
|
|
185
|
+
reserved = await ctx.context.internalAdapter.reserveVerificationValue({
|
|
186
|
+
identifier,
|
|
187
|
+
value: walletAddress,
|
|
188
|
+
expiresAt: new Date(Date.now() + 6e4)
|
|
189
|
+
});
|
|
190
|
+
} catch {
|
|
191
|
+
reserved = false;
|
|
192
|
+
}
|
|
193
|
+
if (reserved) {
|
|
194
|
+
emailClaimIdentifier = identifier;
|
|
195
|
+
if (!await ctx.context.internalAdapter.findUserByEmail(normalizedEmail)) userEmail = normalizedEmail;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const { name, avatar } = await options.resolveProfile?.({ walletAddress }) ?? {};
|
|
199
|
+
const createSIWSUser = (newUserEmail) => ctx.context.internalAdapter.createUser({
|
|
200
|
+
name: name ?? walletAddress,
|
|
201
|
+
email: newUserEmail,
|
|
202
|
+
image: avatar ?? ""
|
|
203
|
+
}, { method: "siws" });
|
|
204
|
+
try {
|
|
205
|
+
user = await createSIWSUser(userEmail);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
if (userEmail !== normalizedEmail || !normalizedEmail) throw error;
|
|
208
|
+
if (!await ctx.context.internalAdapter.findUserByEmail(normalizedEmail)) throw error;
|
|
209
|
+
userEmail = walletEmail;
|
|
210
|
+
user = await createSIWSUser(userEmail);
|
|
211
|
+
} finally {
|
|
212
|
+
if (emailClaimIdentifier) await ctx.context.internalAdapter.consumeVerificationValue(emailClaimIdentifier).catch(() => {});
|
|
213
|
+
}
|
|
214
|
+
await ctx.context.adapter.create({
|
|
215
|
+
model: "walletAddress",
|
|
216
|
+
data: {
|
|
217
|
+
userId: user.id,
|
|
218
|
+
address: walletAddress,
|
|
219
|
+
chainId,
|
|
220
|
+
isPrimary: true,
|
|
221
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
await ctx.context.internalAdapter.createAccount({
|
|
225
|
+
userId: user.id,
|
|
226
|
+
providerId: "siws",
|
|
227
|
+
accountId: `${walletAddress}:${chainId}`,
|
|
228
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
229
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
230
|
+
});
|
|
231
|
+
} else if (!existingWalletAddress) {
|
|
232
|
+
await ctx.context.adapter.create({
|
|
233
|
+
model: "walletAddress",
|
|
234
|
+
data: {
|
|
235
|
+
userId: user.id,
|
|
236
|
+
address: walletAddress,
|
|
237
|
+
chainId,
|
|
238
|
+
isPrimary: false,
|
|
239
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
await ctx.context.internalAdapter.createAccount({
|
|
243
|
+
userId: user.id,
|
|
244
|
+
providerId: "siws",
|
|
245
|
+
accountId: `${walletAddress}:${chainId}`,
|
|
246
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
247
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
const session = await ctx.context.internalAdapter.createSession(user.id);
|
|
251
|
+
if (!session) throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
252
|
+
message: "Internal Server Error",
|
|
253
|
+
status: 500
|
|
254
|
+
});
|
|
255
|
+
await setSessionCookie(ctx, {
|
|
256
|
+
session,
|
|
257
|
+
user
|
|
258
|
+
});
|
|
259
|
+
return ctx.json({
|
|
260
|
+
token: session.token,
|
|
261
|
+
success: true,
|
|
262
|
+
user: {
|
|
263
|
+
id: user.id,
|
|
264
|
+
walletAddress,
|
|
265
|
+
chainId
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (isAPIError(error)) throw error;
|
|
270
|
+
throw new APIError("UNAUTHORIZED", {
|
|
271
|
+
message: "Something went wrong. Please try again later.",
|
|
272
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
273
|
+
status: 401
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
})
|
|
277
|
+
},
|
|
278
|
+
options
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
//#endregion
|
|
282
|
+
export { siws as t };
|
|
283
|
+
|
|
284
|
+
//# sourceMappingURL=better-auth-BZJrlwu8.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"better-auth-BZJrlwu8.js","names":[],"sources":["../src/plugins/better-auth/schema.ts","../src/plugins/better-auth/plugin.ts"],"sourcesContent":["import type { BetterAuthPluginDBSchema } from \"better-auth/db\";\n\nexport const schema = {\n walletAddress: {\n fields: {\n userId: {\n type: \"string\",\n references: {\n model: \"user\",\n field: \"id\",\n },\n required: true,\n index: true,\n },\n address: {\n type: \"string\",\n required: true,\n },\n chainId: {\n type: \"number\",\n required: true,\n bigint: true,\n },\n isPrimary: {\n type: \"boolean\",\n defaultValue: false,\n },\n createdAt: {\n type: \"date\",\n required: true,\n },\n },\n },\n} satisfies BetterAuthPluginDBSchema;\n\nexport type WalletAddressSchema = typeof schema;\n","import { createPlaceholderEmail } from \"@better-auth/core/utils/email\";\nimport type { BetterAuthPlugin, InferOptionSchema, User } from \"better-auth\";\nimport { APIError, createAuthEndpoint, isAPIError } from \"better-auth/api\";\nimport { setSessionCookie } from \"better-auth/cookies\";\nimport { mergeSchema } from \"better-auth/db\";\nimport * as z from \"zod\";\nimport { generateSiwsNonce, verifySiwsMessage } from \"../../index.js\";\nimport { parseSiwsMessage } from \"../../parseSiwsMessage.js\";\nimport { getAddress, isAddress } from \"../../utils.js\";\nimport { PACKAGE_VERSION } from \"../../version.js\";\nimport { schema, type WalletAddressSchema } from \"./schema.js\";\nimport type {\n ResolveProfileArgs,\n ResolveProfileResult,\n SIWSVerifyMessageArgs,\n WalletAddress,\n} from \"./types.js\";\n\ndeclare module \"@better-auth/core\" {\n interface BetterAuthPluginRegistry<AuthOptions, Options> {\n siws: {\n creator: typeof siws;\n };\n }\n}\n\nexport interface SIWSPluginOptions {\n // The domain name of your application (required for SIWS message generation)\n domain: string;\n // The email domain name for creating user accounts when not using anonymous mode. Defaults to a placeholder domain\n emailDomainName?: string | undefined;\n // Whether to allow anonymous sign-ins without requiring an email. Default is true\n anonymous?: boolean | undefined;\n // Function to generate a unique nonce for each sign-in attempt. You must implement this function to return a cryptographically secure random string. Must return a Promise<string>\n getNonce?: () => Promise<string>;\n // Function to verify the SIWS message signature. Defaults to the built-in Stacks verifier\n verifyMessage?:\n ((args: SIWSVerifyMessageArgs) => Promise<boolean>) | undefined;\n // Function to resolve a display name and avatar for a new wallet user\n resolveProfile?:\n ((args: ResolveProfileArgs) => Promise<ResolveProfileResult>) | undefined;\n // Custom schema for the plugin's walletAddress table\n schema?: InferOptionSchema<typeof schema> | undefined;\n}\n\nconst SIWS_VERIFICATION_IDENTIFIER_PREFIX = \"siws:\";\n// MySQL adapter schemas cap verification.identifier at 255 characters.\nconst VERIFICATION_IDENTIFIER_MAX_LENGTH = 255;\nconst SIWS_NONCE_MAX_LENGTH =\n VERIFICATION_IDENTIFIER_MAX_LENGTH -\n SIWS_VERIFICATION_IDENTIFIER_PREFIX.length;\nconst SIWS_NONCE_ALPHANUMERIC_REGEX = /^[a-zA-Z0-9]+$/;\nconst SIWS_MAX_CHAIN_ID = 4294967295;\n\nconst isValidSiwsNonce = (nonce: string | undefined): nonce is string =>\n typeof nonce === \"string\" &&\n nonce.length >= 8 &&\n nonce.length <= SIWS_NONCE_MAX_LENGTH &&\n SIWS_NONCE_ALPHANUMERIC_REGEX.test(nonce);\n\nconst siwsMessageMismatchError = () =>\n new APIError(\"UNAUTHORIZED\", {\n message:\n \"Unauthorized: SIWS message does not match the expected domain, address, chain ID, or nonce\",\n status: 401,\n code: \"UNAUTHORIZED_SIWS_MESSAGE_MISMATCH\",\n });\n\nexport const siws = (options: SIWSPluginOptions) => {\n const verifyMessage =\n options.verifyMessage ??\n (async (args: SIWSVerifyMessageArgs) => {\n const { nonce } = parseSiwsMessage(args.message);\n return verifySiwsMessage({\n message: args.message,\n signature: args.signature,\n address: args.address,\n domain: options.domain,\n nonce,\n });\n });\n\n return {\n id: \"siws\",\n version: PACKAGE_VERSION,\n schema: mergeSchema(schema, options?.schema) as WalletAddressSchema,\n endpoints: {\n nonce: createAuthEndpoint(\n \"/siws/nonce\",\n {\n method: \"POST\",\n body: z.object({}).strict().optional(),\n },\n async (ctx) => {\n const nonce = options.getNonce\n ? await options.getNonce()\n : generateSiwsNonce();\n\n if (!isValidSiwsNonce(nonce)) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `SIWS getNonce must return a nonce of 8-${SIWS_NONCE_MAX_LENGTH} alphanumeric characters.`,\n status: 500,\n code: \"SIWS_INVALID_NONCE\",\n });\n }\n\n // Store nonce with 15-minute expiration\n await ctx.context.internalAdapter.createVerificationValue({\n identifier: `${SIWS_VERIFICATION_IDENTIFIER_PREFIX}${nonce}`,\n value: nonce,\n expiresAt: new Date(Date.now() + 15 * 60 * 1000),\n });\n\n return ctx.json({ nonce });\n },\n ),\n\n verify: createAuthEndpoint(\n \"/siws/verify\",\n {\n method: \"POST\",\n body: z\n .object({\n message: z.string().min(1),\n signature: z.string().min(1),\n email: z.email().optional(),\n })\n .strict()\n .refine((data) => options.anonymous !== false || !!data.email, {\n message:\n \"Email is required when the anonymous plugin option is disabled.\",\n path: [\"email\"],\n }),\n requireRequest: true,\n },\n async (ctx) => {\n const { message, signature, email } = ctx.body;\n const isAnon = options.anonymous ?? true;\n\n if (!isAnon && !email) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Email is required when anonymous is disabled.\",\n status: 400,\n });\n }\n\n try {\n // The signed message is the source of truth for wallet identity:\n // address, chain ID, nonce, and time bounds are read from it rather\n // than from the request body.\n const parsedMessage = parseSiwsMessage(message);\n const { address, chainId, nonce } = parsedMessage;\n\n if (\n !address ||\n !isAddress(address) ||\n !isValidSiwsNonce(nonce) ||\n typeof chainId !== \"number\" ||\n !Number.isInteger(chainId) ||\n chainId < 1 ||\n chainId > SIWS_MAX_CHAIN_ID ||\n parsedMessage.domain !== options.domain\n ) {\n throw siwsMessageMismatchError();\n }\n\n // Canonicalize the signed address before using it as an identity:\n // c32check decoding folds case and the confusable characters\n // `O`/`0` and `I`/`L`/`1`, so equivalent spellings must resolve to\n // a single wallet record instead of creating duplicate users.\n const walletAddress = getAddress(address);\n\n const now = new Date();\n if (parsedMessage.expirationTime) {\n if (\n Number.isNaN(parsedMessage.expirationTime.getTime()) ||\n now >= parsedMessage.expirationTime\n ) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Unauthorized: SIWS message has expired\",\n status: 401,\n code: \"UNAUTHORIZED_SIWS_MESSAGE_EXPIRED\",\n });\n }\n }\n if (parsedMessage.notBefore) {\n if (\n Number.isNaN(parsedMessage.notBefore.getTime()) ||\n now < parsedMessage.notBefore\n ) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Unauthorized: SIWS message is not yet valid\",\n status: 401,\n code: \"UNAUTHORIZED_SIWS_MESSAGE_NOT_YET_VALID\",\n });\n }\n }\n\n // Atomically consume the single-use nonce before any signature\n // work. The first concurrent request wins; every racer gets null.\n // Expired nonces are treated as already consumed.\n const verification =\n await ctx.context.internalAdapter.consumeVerificationValue(\n `${SIWS_VERIFICATION_IDENTIFIER_PREFIX}${nonce}`,\n );\n\n if (!verification) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Unauthorized: Invalid or expired nonce\",\n status: 401,\n code: \"UNAUTHORIZED_INVALID_OR_EXPIRED_NONCE\",\n });\n }\n\n // Verify SIWS message with enhanced parameters\n const verified = await verifyMessage({\n message,\n signature,\n address: walletAddress,\n chainId,\n });\n\n if (!verified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Unauthorized: Invalid SIWS signature\",\n status: 401,\n });\n }\n\n // Look for existing user by their wallet addresses\n let user: User | null = null;\n\n // Check if there's a wallet address record for this exact address+chainId combination\n const existingWalletAddress: WalletAddress | null =\n await ctx.context.adapter.findOne({\n model: \"walletAddress\",\n where: [\n { field: \"address\", operator: \"eq\", value: walletAddress },\n { field: \"chainId\", operator: \"eq\", value: chainId },\n ],\n });\n\n if (existingWalletAddress) {\n // Get the user associated with this wallet address\n user = await ctx.context.adapter.findOne({\n model: \"user\",\n where: [\n {\n field: \"id\",\n operator: \"eq\",\n value: existingWalletAddress.userId,\n },\n ],\n });\n } else {\n // No exact match found, check if this address exists on any other chain\n const anyWalletAddress: WalletAddress | null =\n await ctx.context.adapter.findOne({\n model: \"walletAddress\",\n where: [\n { field: \"address\", operator: \"eq\", value: walletAddress },\n ],\n });\n\n if (anyWalletAddress) {\n // Same address exists on different chain, get that user\n user = await ctx.context.adapter.findOne({\n model: \"user\",\n where: [\n {\n field: \"id\",\n operator: \"eq\",\n value: anyWalletAddress.userId,\n },\n ],\n });\n }\n }\n\n // Create new user if none exists\n if (!user) {\n const normalizedEmail = email?.toLowerCase();\n const walletEmail = options.emailDomainName\n ? `${walletAddress}@${options.emailDomainName}`\n : createPlaceholderEmail({\n identifier: walletAddress,\n namespace: \"siws\",\n });\n // SIWS proves wallet control, not email ownership: bind the caller\n // email only when unclaimed and atomically reserved, else keep\n // the wallet-derived address. Silent fallback avoids an\n // enumeration oracle.\n let userEmail = walletEmail;\n let emailClaimIdentifier: string | undefined;\n if (!isAnon && normalizedEmail) {\n const identifier = `siws-email-claim-${normalizedEmail}`;\n let reserved = false;\n try {\n reserved =\n await ctx.context.internalAdapter.reserveVerificationValue({\n identifier,\n value: walletAddress,\n expiresAt: new Date(Date.now() + 60_000),\n });\n } catch {\n reserved = false;\n }\n if (reserved) {\n emailClaimIdentifier = identifier;\n const existingUser =\n await ctx.context.internalAdapter.findUserByEmail(\n normalizedEmail,\n );\n if (!existingUser) {\n userEmail = normalizedEmail;\n }\n }\n }\n const { name, avatar } =\n (await options.resolveProfile?.({ walletAddress })) ?? {};\n\n const createSIWSUser = (newUserEmail: string) =>\n ctx.context.internalAdapter.createUser(\n {\n name: name ?? walletAddress,\n email: newUserEmail,\n image: avatar ?? \"\",\n },\n { method: \"siws\" },\n );\n\n try {\n user = await createSIWSUser(userEmail);\n } catch (error) {\n if (userEmail !== normalizedEmail || !normalizedEmail) {\n throw error;\n }\n const claimedUser =\n await ctx.context.internalAdapter.findUserByEmail(\n normalizedEmail,\n );\n if (!claimedUser) {\n throw error;\n }\n userEmail = walletEmail;\n user = await createSIWSUser(userEmail);\n } finally {\n if (emailClaimIdentifier) {\n await ctx.context.internalAdapter\n .consumeVerificationValue(emailClaimIdentifier)\n .catch(() => {});\n }\n }\n\n // Create wallet address record\n await ctx.context.adapter.create<WalletAddress>({\n model: \"walletAddress\",\n data: {\n userId: user.id,\n address: walletAddress,\n chainId,\n isPrimary: true, // First address is primary\n createdAt: new Date(),\n },\n });\n\n // Create account record for wallet authentication\n await ctx.context.internalAdapter.createAccount({\n userId: user.id,\n providerId: \"siws\",\n accountId: `${walletAddress}:${chainId}`,\n createdAt: new Date(),\n updatedAt: new Date(),\n });\n } else {\n // User exists, but check if this specific address/chain combo exists\n if (!existingWalletAddress) {\n // Add this new chainId to existing user's addresses\n await ctx.context.adapter.create({\n model: \"walletAddress\",\n data: {\n userId: user.id,\n address: walletAddress,\n chainId,\n isPrimary: false, // Additional addresses are not primary by default\n createdAt: new Date(),\n },\n });\n\n // Create account record for this new wallet+chain combination\n await ctx.context.internalAdapter.createAccount({\n userId: user.id,\n providerId: \"siws\",\n accountId: `${walletAddress}:${chainId}`,\n createdAt: new Date(),\n updatedAt: new Date(),\n });\n }\n }\n\n const session = await ctx.context.internalAdapter.createSession(\n user.id,\n );\n\n if (!session) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Internal Server Error\",\n status: 500,\n });\n }\n\n await setSessionCookie(ctx, { session, user });\n\n return ctx.json({\n token: session.token,\n success: true,\n user: {\n id: user.id,\n walletAddress,\n chainId,\n },\n });\n } catch (error) {\n if (isAPIError(error)) throw error;\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Something went wrong. Please try again later.\",\n error: error instanceof Error ? error.message : \"Unknown error\",\n status: 401,\n });\n }\n },\n ),\n },\n options,\n } satisfies BetterAuthPlugin;\n};\n"],"mappings":";;;;;;;;AAEA,MAAa,SAAS,EACpB,eAAe,EACb,QAAQ;CACN,QAAQ;EACN,MAAM;EACN,YAAY;GACV,OAAO;GACP,OAAO;EACT;EACA,UAAU;EACV,OAAO;CACT;CACA,SAAS;EACP,MAAM;EACN,UAAU;CACZ;CACA,SAAS;EACP,MAAM;EACN,UAAU;EACV,QAAQ;CACV;CACA,WAAW;EACT,MAAM;EACN,cAAc;CAChB;CACA,WAAW;EACT,MAAM;EACN,UAAU;CACZ;AACF,EACF,EACF;;;ACYA,MAAM,sCAAsC;AAG5C,MAAM,wBACJ;AAEF,MAAM,gCAAgC;AACtC,MAAM,oBAAoB;AAE1B,MAAM,oBAAoB,UACxB,OAAO,UAAU,YACjB,MAAM,UAAU,KAChB,MAAM,UAAU,yBAChB,8BAA8B,KAAK,KAAK;AAE1C,MAAM,iCACJ,IAAI,SAAS,gBAAgB;CAC3B,SACE;CACF,QAAQ;CACR,MAAM;AACR,CAAC;AAEH,MAAa,QAAQ,YAA+B;CAClD,MAAM,gBACJ,QAAQ,kBACP,OAAO,SAAgC;EACtC,MAAM,EAAE,UAAU,iBAAiB,KAAK,OAAO;EAC/C,OAAO,kBAAkB;GACvB,SAAS,KAAK;GACd,WAAW,KAAK;GAChB,SAAS,KAAK;GACd,QAAQ,QAAQ;GAChB;EACF,CAAC;CACH;CAEF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,SAAS;MACT,OAAO,iBAAiB,QAAQ,MAAM,UAAU;MAChD,QAAQ;KACV,CAAC;IACH;GACF,CACF;EACF;EACA;CACF;AACF"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { InferOptionSchema } from "better-auth";
|
|
3
|
+
//#region src/plugins/better-auth/schema.d.ts
|
|
4
|
+
declare const schema: {
|
|
5
|
+
walletAddress: {
|
|
6
|
+
fields: {
|
|
7
|
+
userId: {
|
|
8
|
+
type: "string";
|
|
9
|
+
references: {
|
|
10
|
+
model: string;
|
|
11
|
+
field: string;
|
|
12
|
+
};
|
|
13
|
+
required: true;
|
|
14
|
+
index: true;
|
|
15
|
+
};
|
|
16
|
+
address: {
|
|
17
|
+
type: "string";
|
|
18
|
+
required: true;
|
|
19
|
+
};
|
|
20
|
+
chainId: {
|
|
21
|
+
type: "number";
|
|
22
|
+
required: true;
|
|
23
|
+
bigint: true;
|
|
24
|
+
};
|
|
25
|
+
isPrimary: {
|
|
26
|
+
type: "boolean";
|
|
27
|
+
defaultValue: false;
|
|
28
|
+
};
|
|
29
|
+
createdAt: {
|
|
30
|
+
type: "date";
|
|
31
|
+
required: true;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
type WalletAddressSchema = typeof schema;
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/plugins/better-auth/types.d.ts
|
|
39
|
+
interface SIWSVerifyMessageArgs {
|
|
40
|
+
message: string;
|
|
41
|
+
signature: string;
|
|
42
|
+
address: string;
|
|
43
|
+
chainId: number;
|
|
44
|
+
}
|
|
45
|
+
interface ResolveProfileArgs {
|
|
46
|
+
walletAddress: string;
|
|
47
|
+
}
|
|
48
|
+
interface ResolveProfileResult {
|
|
49
|
+
name: string;
|
|
50
|
+
avatar: string;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/plugins/better-auth/plugin.d.ts
|
|
54
|
+
declare module "@better-auth/core" {
|
|
55
|
+
interface BetterAuthPluginRegistry<AuthOptions, Options> {
|
|
56
|
+
siws: {
|
|
57
|
+
creator: typeof siws;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
interface SIWSPluginOptions {
|
|
62
|
+
domain: string;
|
|
63
|
+
emailDomainName?: string | undefined;
|
|
64
|
+
anonymous?: boolean | undefined;
|
|
65
|
+
getNonce?: () => Promise<string>;
|
|
66
|
+
verifyMessage?: ((args: SIWSVerifyMessageArgs) => Promise<boolean>) | undefined;
|
|
67
|
+
resolveProfile?: ((args: ResolveProfileArgs) => Promise<ResolveProfileResult>) | undefined;
|
|
68
|
+
schema?: InferOptionSchema<typeof schema> | undefined;
|
|
69
|
+
}
|
|
70
|
+
declare const siws: (options: SIWSPluginOptions) => {
|
|
71
|
+
id: "siws";
|
|
72
|
+
version: string;
|
|
73
|
+
schema: WalletAddressSchema;
|
|
74
|
+
endpoints: {
|
|
75
|
+
nonce: import("better-auth").StrictEndpoint<"/siws/nonce", {
|
|
76
|
+
method: "POST";
|
|
77
|
+
body: z.ZodOptional<z.ZodObject<{}, z.core.$strict>>;
|
|
78
|
+
}, {
|
|
79
|
+
nonce: string;
|
|
80
|
+
}>;
|
|
81
|
+
verify: import("better-auth").StrictEndpoint<"/siws/verify", {
|
|
82
|
+
method: "POST";
|
|
83
|
+
body: z.ZodObject<{
|
|
84
|
+
message: z.ZodString;
|
|
85
|
+
signature: z.ZodString;
|
|
86
|
+
email: z.ZodOptional<z.ZodEmail>;
|
|
87
|
+
}, z.core.$strict>;
|
|
88
|
+
requireRequest: true;
|
|
89
|
+
}, {
|
|
90
|
+
token: string;
|
|
91
|
+
success: boolean;
|
|
92
|
+
user: {
|
|
93
|
+
id: string;
|
|
94
|
+
walletAddress: string;
|
|
95
|
+
chainId: number;
|
|
96
|
+
};
|
|
97
|
+
}>;
|
|
98
|
+
};
|
|
99
|
+
options: SIWSPluginOptions;
|
|
100
|
+
};
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/plugins/better-auth/client.d.ts
|
|
103
|
+
declare const siwsClient: () => {
|
|
104
|
+
id: "siws";
|
|
105
|
+
version: string;
|
|
106
|
+
$InferServerPlugin: ReturnType<typeof siws>;
|
|
107
|
+
pathMethods: {
|
|
108
|
+
"/siws/nonce": "POST";
|
|
109
|
+
"/siws/verify": "POST";
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
//#endregion
|
|
113
|
+
export { SIWSPluginOptions as n, siws as r, siwsClient as t };
|
|
114
|
+
//# sourceMappingURL=client-BywC0Cfs.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#endregion
|
|
2
|
+
//#region src/version.ts
|
|
3
|
+
const PACKAGE_VERSION = "0.4.0";
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/plugins/better-auth/client.ts
|
|
6
|
+
const siwsClient = () => {
|
|
7
|
+
return {
|
|
8
|
+
id: "siws",
|
|
9
|
+
version: PACKAGE_VERSION,
|
|
10
|
+
$InferServerPlugin: {},
|
|
11
|
+
pathMethods: {
|
|
12
|
+
"/siws/nonce": "POST",
|
|
13
|
+
"/siws/verify": "POST"
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { PACKAGE_VERSION as n, siwsClient as t };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=client-DTkU-AMP.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-DTkU-AMP.js","names":["pkg.version"],"sources":["../package.json","../src/version.ts","../src/plugins/better-auth/client.ts"],"sourcesContent":["","import pkg from \"../package.json\" with { type: \"json\" };\n\nexport const PACKAGE_VERSION = pkg.version;\n","import type { BetterAuthClientPlugin } from \"better-auth\";\nimport { PACKAGE_VERSION } from \"../../version.js\";\nimport type { siws } from \"./plugin.js\";\n\nexport const siwsClient = () => {\n return {\n id: \"siws\",\n version: PACKAGE_VERSION,\n $InferServerPlugin: {} as ReturnType<typeof siws>,\n pathMethods: {\n \"/siws/nonce\": \"POST\",\n \"/siws/verify\": \"POST\",\n },\n } satisfies BetterAuthClientPlugin;\n};\n"],"mappings":";;ACEA,MAAa,kBAAkBA;;;ACE/B,MAAa,mBAAmB;CAC9B,OAAO;EACL,IAAI;EACJ,SAAS;EACT,oBAAoB,CAAC;EACrB,aAAa;GACX,eAAe;GACf,gBAAgB;EAClB;CACF;AACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -24,9 +24,7 @@ type InvalidAddressErrorType = InvalidAddressError & {
|
|
|
24
24
|
name: "InvalidAddressError";
|
|
25
25
|
};
|
|
26
26
|
declare class InvalidAddressError extends BaseError {
|
|
27
|
-
constructor({
|
|
28
|
-
address
|
|
29
|
-
}: {
|
|
27
|
+
constructor({ address }: {
|
|
30
28
|
address: string;
|
|
31
29
|
});
|
|
32
30
|
}
|
|
@@ -36,10 +34,7 @@ type SiwsInvalidMessageFieldErrorType = SiwsInvalidMessageFieldError & {
|
|
|
36
34
|
name: "SiwsInvalidMessageFieldError";
|
|
37
35
|
};
|
|
38
36
|
declare class SiwsInvalidMessageFieldError extends BaseError {
|
|
39
|
-
constructor({
|
|
40
|
-
field,
|
|
41
|
-
metaMessages
|
|
42
|
-
}: {
|
|
37
|
+
constructor({ field, metaMessages }: {
|
|
43
38
|
field: string;
|
|
44
39
|
metaMessages?: string[] | undefined;
|
|
45
40
|
});
|
|
@@ -54,8 +49,8 @@ declare class SiwsInvalidMessageFieldError extends BaseError {
|
|
|
54
49
|
* Prettify<{ a: string } & { b: string } & { c: number, d: bigint }>
|
|
55
50
|
* => { a: string, b: string, c: number, d: bigint }
|
|
56
51
|
*/
|
|
57
|
-
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
58
|
-
type ExactPartial<type> = { [key in keyof type]?: type[key] | undefined };
|
|
52
|
+
type Prettify<T> = { [K in keyof T]: T[K]; } & {};
|
|
53
|
+
type ExactPartial<type> = { [key in keyof type]?: type[key] | undefined; };
|
|
59
54
|
/**
|
|
60
55
|
* @description SIP-X message fields
|
|
61
56
|
*
|
|
@@ -135,7 +130,7 @@ type CreateSiwsMessageErrorType = InvalidAddressErrorType | SiwsInvalidMessageFi
|
|
|
135
130
|
*
|
|
136
131
|
* @see https://github.com/stacksgov/sips/pull/70
|
|
137
132
|
*/
|
|
138
|
-
declare function createSiwsMessage(parameters: CreateSiwsMessageParameters): CreateSiwsMessageReturnType;
|
|
133
|
+
export declare function createSiwsMessage(parameters: CreateSiwsMessageParameters): CreateSiwsMessageReturnType;
|
|
139
134
|
//#endregion
|
|
140
135
|
//#region src/generateSiwsNonce.d.ts
|
|
141
136
|
/**
|
|
@@ -148,7 +143,7 @@ declare function createSiwsMessage(parameters: CreateSiwsMessageParameters): Cre
|
|
|
148
143
|
*
|
|
149
144
|
* @returns A randomly generated SIP-X nonce.
|
|
150
145
|
*/
|
|
151
|
-
declare function generateSiwsNonce(): string;
|
|
146
|
+
export declare function generateSiwsNonce(): string;
|
|
152
147
|
//#endregion
|
|
153
148
|
//#region src/validateSiwsMessage.d.ts
|
|
154
149
|
type ValidateSiwsMessageParameters = {
|
|
@@ -185,7 +180,7 @@ type ValidateSiwsMessageReturnType = boolean;
|
|
|
185
180
|
*
|
|
186
181
|
* @see https://github.com/stacksgov/sips/pull/70
|
|
187
182
|
*/
|
|
188
|
-
declare function validateSiwsMessage(parameters: ValidateSiwsMessageParameters): ValidateSiwsMessageReturnType;
|
|
183
|
+
export declare function validateSiwsMessage(parameters: ValidateSiwsMessageParameters): ValidateSiwsMessageReturnType;
|
|
189
184
|
//#endregion
|
|
190
185
|
//#region src/verifySiwsMessage.d.ts
|
|
191
186
|
type VerifySiwsMessageParameters = Prettify<Pick<ValidateSiwsMessageParameters, "address" | "domain" | "nonce" | "scheme" | "time"> & {
|
|
@@ -205,7 +200,7 @@ type VerifySiwsMessageReturnType = boolean;
|
|
|
205
200
|
* @param parameters - {@link VerifySiwsMessageParameters}
|
|
206
201
|
* @returns Whether or not the signature is valid. {@link VerifySiwsMessageReturnType}
|
|
207
202
|
*/
|
|
208
|
-
declare function verifySiwsMessage(parameters: VerifySiwsMessageParameters): VerifySiwsMessageReturnType;
|
|
203
|
+
export declare function verifySiwsMessage(parameters: VerifySiwsMessageParameters): VerifySiwsMessageReturnType;
|
|
209
204
|
//#endregion
|
|
210
|
-
export {
|
|
205
|
+
export type { CreateSiwsMessageErrorType, CreateSiwsMessageParameters, CreateSiwsMessageReturnType, ValidateSiwsMessageParameters, ValidateSiwsMessageReturnType, VerifySiwsMessageParameters, VerifySiwsMessageReturnType };
|
|
211
206
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export { createSiwsMessage, generateSiwsNonce, validateSiwsMessage, verifySiwsMessage };
|
|
1
|
+
import { a as createSiwsMessage, i as generateSiwsNonce, r as validateSiwsMessage, t as verifySiwsMessage } from "./src-BvqyQzSI.js";
|
|
2
|
+
export { createSiwsMessage, generateSiwsNonce, validateSiwsMessage, verifySiwsMessage };
|
|
@@ -1,10 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/plugins/better-auth/client.d.ts
|
|
4
|
-
declare const siwsClient: () => {
|
|
5
|
-
id: "siws";
|
|
6
|
-
$InferServerPlugin: ReturnType<typeof siws>;
|
|
7
|
-
};
|
|
8
|
-
//#endregion
|
|
9
|
-
export { siwsClient };
|
|
10
|
-
//# sourceMappingURL=client.d.ts.map
|
|
1
|
+
import { t as siwsClient } from "../../client-BywC0Cfs.js";
|
|
2
|
+
export { siwsClient };
|
|
@@ -1,11 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
const siwsClient = () => {
|
|
3
|
-
return {
|
|
4
|
-
id: "siws",
|
|
5
|
-
$InferServerPlugin: {}
|
|
6
|
-
};
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
//#endregion
|
|
1
|
+
import { t as siwsClient } from "../../client-DTkU-AMP.js";
|
|
10
2
|
export { siwsClient };
|
|
11
|
-
//# sourceMappingURL=client.js.map
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { siwsClient } from "
|
|
2
|
-
import { t as siws } from "../../
|
|
3
|
-
|
|
4
|
-
export { siws, siwsClient };
|
|
1
|
+
import { t as siwsClient } from "../../client-DTkU-AMP.js";
|
|
2
|
+
import { t as siws } from "../../better-auth-BZJrlwu8.js";
|
|
3
|
+
export { siws, siwsClient };
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { n as siws, t as
|
|
2
|
-
import { siwsClient } from "./better-auth/client.js";
|
|
1
|
+
import { n as SIWSPluginOptions, r as siws, t as siwsClient } from "../client-BywC0Cfs.js";
|
|
3
2
|
import "./better-auth/index.js";
|
|
4
3
|
export { SIWSPluginOptions, siws, siwsClient };
|
package/dist/plugins/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { siwsClient } from "
|
|
2
|
-
import { t as siws } from "../
|
|
3
|
-
|
|
4
|
-
export { siws, siwsClient };
|
|
1
|
+
import { t as siwsClient } from "../client-DTkU-AMP.js";
|
|
2
|
+
import { t as siws } from "../better-auth-BZJrlwu8.js";
|
|
3
|
+
export { siws, siwsClient };
|