@nuxtjs/better-auth 0.0.0 → 0.1.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 +67 -0
- package/dist/module.d.mts +47 -0
- package/dist/module.json +12 -0
- package/dist/module.mjs +1468 -0
- package/dist/runtime/app/components/BetterAuthState.d.vue.ts +20 -0
- package/dist/runtime/app/components/BetterAuthState.vue +9 -0
- package/dist/runtime/app/components/BetterAuthState.vue.d.ts +20 -0
- package/dist/runtime/app/composables/runWithSessionRefresh.d.ts +1 -0
- package/dist/runtime/app/composables/runWithSessionRefresh.js +12 -0
- package/dist/runtime/app/composables/useAction.d.ts +2 -0
- package/dist/runtime/app/composables/useAction.js +6 -0
- package/dist/runtime/app/composables/useAuthAsyncData.d.ts +6 -0
- package/dist/runtime/app/composables/useAuthAsyncData.js +16 -0
- package/dist/runtime/app/composables/useAuthClient.d.ts +9 -0
- package/dist/runtime/app/composables/useAuthClient.js +35 -0
- package/dist/runtime/app/composables/useAuthClientAction.d.ts +3 -0
- package/dist/runtime/app/composables/useAuthClientAction.js +15 -0
- package/dist/runtime/app/composables/useAuthRequestFetch.d.ts +11 -0
- package/dist/runtime/app/composables/useAuthRequestFetch.js +4 -0
- package/dist/runtime/app/composables/useSignIn.d.ts +16 -0
- package/dist/runtime/app/composables/useSignIn.js +8 -0
- package/dist/runtime/app/composables/useSignUp.d.ts +5 -0
- package/dist/runtime/app/composables/useSignUp.js +8 -0
- package/dist/runtime/app/composables/useUserSession.d.ts +23 -0
- package/dist/runtime/app/composables/useUserSession.js +247 -0
- package/dist/runtime/app/composables/useUserSessionState.d.ts +3 -0
- package/dist/runtime/app/composables/useUserSessionState.js +4 -0
- package/dist/runtime/app/internal/auth-action-error.d.ts +3 -0
- package/dist/runtime/app/internal/auth-action-error.js +33 -0
- package/dist/runtime/app/internal/auth-action-handles.d.ts +21 -0
- package/dist/runtime/app/internal/auth-action-handles.js +105 -0
- package/dist/runtime/app/internal/redirect-helpers.d.ts +4 -0
- package/dist/runtime/app/internal/redirect-helpers.js +37 -0
- package/dist/runtime/app/internal/session-fetch.d.ts +12 -0
- package/dist/runtime/app/internal/session-fetch.js +56 -0
- package/dist/runtime/app/internal/utils.d.ts +1 -0
- package/dist/runtime/app/internal/utils.js +3 -0
- package/dist/runtime/app/internal/vue-safe-auth-proxy.d.ts +3 -0
- package/dist/runtime/app/internal/vue-safe-auth-proxy.js +68 -0
- package/dist/runtime/app/internal/wrap-auth-method.d.ts +18 -0
- package/dist/runtime/app/internal/wrap-auth-method.js +69 -0
- package/dist/runtime/app/middleware/auth.global.d.ts +13 -0
- package/dist/runtime/app/middleware/auth.global.js +104 -0
- package/dist/runtime/app/pages/__better-auth-devtools.d.vue.ts +3 -0
- package/dist/runtime/app/pages/__better-auth-devtools.vue +380 -0
- package/dist/runtime/app/pages/__better-auth-devtools.vue.d.ts +3 -0
- package/dist/runtime/app/plugins/session.client.d.ts +2 -0
- package/dist/runtime/app/plugins/session.client.js +16 -0
- package/dist/runtime/app/plugins/session.server.d.ts +2 -0
- package/dist/runtime/app/plugins/session.server.js +24 -0
- package/dist/runtime/composables.d.ts +12 -0
- package/dist/runtime/composables.js +10 -0
- package/dist/runtime/config.d.ts +100 -0
- package/dist/runtime/config.js +13 -0
- package/dist/runtime/internal/auth-route-rules.d.ts +1 -0
- package/dist/runtime/internal/auth-route-rules.js +24 -0
- package/dist/runtime/server/api/_better-auth/_schema.d.ts +4 -0
- package/dist/runtime/server/api/_better-auth/_schema.js +12 -0
- package/dist/runtime/server/api/_better-auth/accounts.get.d.ts +2 -0
- package/dist/runtime/server/api/_better-auth/accounts.get.js +29 -0
- package/dist/runtime/server/api/_better-auth/config.get.d.ts +2 -0
- package/dist/runtime/server/api/_better-auth/config.get.js +58 -0
- package/dist/runtime/server/api/_better-auth/sessions.delete.d.ts +2 -0
- package/dist/runtime/server/api/_better-auth/sessions.delete.js +23 -0
- package/dist/runtime/server/api/_better-auth/sessions.get.d.ts +2 -0
- package/dist/runtime/server/api/_better-auth/sessions.get.js +44 -0
- package/dist/runtime/server/api/_better-auth/users.get.d.ts +2 -0
- package/dist/runtime/server/api/_better-auth/users.get.js +35 -0
- package/dist/runtime/server/api/auth/[...all].d.ts +2 -0
- package/dist/runtime/server/api/auth/[...all].js +6 -0
- package/dist/runtime/server/internal/nitro-compat.d.ts +1 -0
- package/dist/runtime/server/internal/nitro-compat.js +1 -0
- package/dist/runtime/server/internal/nitro2.d.ts +8 -0
- package/dist/runtime/server/internal/nitro2.js +29 -0
- package/dist/runtime/server/internal/nitro3.d.ts +10 -0
- package/dist/runtime/server/internal/nitro3.js +60 -0
- package/dist/runtime/server/middleware/route-access.d.ts +2 -0
- package/dist/runtime/server/middleware/route-access.js +29 -0
- package/dist/runtime/server/tsconfig.json +12 -0
- package/dist/runtime/server/utils/auth.d.ts +20 -0
- package/dist/runtime/server/utils/auth.js +246 -0
- package/dist/runtime/server/utils/custom-secondary-storage.d.ts +6 -0
- package/dist/runtime/server/utils/custom-secondary-storage.js +8 -0
- package/dist/runtime/server/utils/session.d.ts +8 -0
- package/dist/runtime/server/utils/session.js +280 -0
- package/dist/runtime/server/virtual-modules.d.ts +27 -0
- package/dist/runtime/types/augment.d.ts +56 -0
- package/dist/runtime/types/augment.js +0 -0
- package/dist/runtime/types.d.ts +21 -0
- package/dist/runtime/types.js +0 -0
- package/dist/runtime/utils/match-user.d.ts +2 -0
- package/dist/runtime/utils/match-user.js +13 -0
- package/dist/types.d.mts +11 -0
- package/package.json +111 -10
- package/index.js +0 -1
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { matchesUser } from "../../utils/match-user.js";
|
|
2
|
+
import { createAuthError, splitCookiesString } from "../internal/nitro-compat.js";
|
|
3
|
+
import { serverAuth } from "./auth.js";
|
|
4
|
+
const requestSessionLoadKey = Symbol.for("nuxt-better-auth.requestSessionLoad");
|
|
5
|
+
const signingAlgorithm = { name: "HMAC", hash: "SHA-256" };
|
|
6
|
+
const cookiePairSeparatorRE = /;\s*/;
|
|
7
|
+
const fallbackRequestSessionContext = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
function getRequestSessionContext(event) {
|
|
9
|
+
const eventWithContext = event;
|
|
10
|
+
if (eventWithContext.context && typeof eventWithContext.context === "object")
|
|
11
|
+
return eventWithContext.context;
|
|
12
|
+
let context = fallbackRequestSessionContext.get(event);
|
|
13
|
+
if (!context) {
|
|
14
|
+
context = {};
|
|
15
|
+
fallbackRequestSessionContext.set(event, context);
|
|
16
|
+
}
|
|
17
|
+
return context;
|
|
18
|
+
}
|
|
19
|
+
function getIncomingRequestHeaders(event) {
|
|
20
|
+
const requestHeaders = event.req?.headers;
|
|
21
|
+
return requestHeaders instanceof Headers ? requestHeaders : event.headers;
|
|
22
|
+
}
|
|
23
|
+
function getRequestHeaders(event) {
|
|
24
|
+
return getRequestSessionContext(event).requestHeaders ?? getIncomingRequestHeaders(event);
|
|
25
|
+
}
|
|
26
|
+
function loadSession(event) {
|
|
27
|
+
const auth = serverAuth(event);
|
|
28
|
+
return auth.api.getSession({ headers: getRequestHeaders(event) });
|
|
29
|
+
}
|
|
30
|
+
function loadFreshSession(event) {
|
|
31
|
+
const auth = serverAuth(event);
|
|
32
|
+
return auth.api.getSession({
|
|
33
|
+
headers: getRequestHeaders(event),
|
|
34
|
+
query: { disableCookieCache: true },
|
|
35
|
+
returnHeaders: true
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function getServerAuthContext(event) {
|
|
39
|
+
const auth = serverAuth(event);
|
|
40
|
+
return auth.$context;
|
|
41
|
+
}
|
|
42
|
+
function getCookieName(name, prefix) {
|
|
43
|
+
if (prefix === "secure")
|
|
44
|
+
return name.startsWith("__Secure-") ? name : `__Secure-${name}`;
|
|
45
|
+
if (prefix === "host")
|
|
46
|
+
return name.startsWith("__Host-") ? name : `__Host-${name}`;
|
|
47
|
+
if (prefix)
|
|
48
|
+
return void 0;
|
|
49
|
+
return name;
|
|
50
|
+
}
|
|
51
|
+
function serializeCookieHeader(name, value, attributes = {}, valueIsEncoded = false) {
|
|
52
|
+
const cookieName = getCookieName(name, attributes.prefix);
|
|
53
|
+
if (!cookieName)
|
|
54
|
+
throw new Error(`Unsupported cookie prefix: ${attributes.prefix}`);
|
|
55
|
+
const cookieValue = valueIsEncoded ? value : encodeURIComponent(value);
|
|
56
|
+
const cookie = [`${cookieName}=${cookieValue}`];
|
|
57
|
+
const options = { ...attributes };
|
|
58
|
+
if (cookieName.startsWith("__Secure-") && !options.secure)
|
|
59
|
+
options.secure = true;
|
|
60
|
+
if (cookieName.startsWith("__Host-")) {
|
|
61
|
+
options.secure = true;
|
|
62
|
+
options.path = "/";
|
|
63
|
+
delete options.domain;
|
|
64
|
+
}
|
|
65
|
+
if (typeof options.maxAge === "number" && options.maxAge >= 0)
|
|
66
|
+
cookie.push(`Max-Age=${Math.floor(options.maxAge)}`);
|
|
67
|
+
if (options.domain && options.prefix !== "host")
|
|
68
|
+
cookie.push(`Domain=${options.domain}`);
|
|
69
|
+
if (options.path)
|
|
70
|
+
cookie.push(`Path=${options.path}`);
|
|
71
|
+
if (options.expires)
|
|
72
|
+
cookie.push(`Expires=${options.expires.toUTCString()}`);
|
|
73
|
+
if (options.httpOnly)
|
|
74
|
+
cookie.push("HttpOnly");
|
|
75
|
+
if (options.partitioned)
|
|
76
|
+
options.secure = true;
|
|
77
|
+
if (options.secure)
|
|
78
|
+
cookie.push("Secure");
|
|
79
|
+
if (options.sameSite) {
|
|
80
|
+
const normalizedSameSite = options.sameSite.charAt(0).toUpperCase() + options.sameSite.slice(1);
|
|
81
|
+
cookie.push(`SameSite=${normalizedSameSite}`);
|
|
82
|
+
}
|
|
83
|
+
if (options.partitioned)
|
|
84
|
+
cookie.push("Partitioned");
|
|
85
|
+
return cookie.join("; ");
|
|
86
|
+
}
|
|
87
|
+
function serializeCookie(name, value, attributes = {}) {
|
|
88
|
+
return serializeCookieHeader(name, value, attributes);
|
|
89
|
+
}
|
|
90
|
+
async function signCookieValue(value, secret) {
|
|
91
|
+
const key = await crypto.subtle.importKey(
|
|
92
|
+
"raw",
|
|
93
|
+
new TextEncoder().encode(secret),
|
|
94
|
+
signingAlgorithm,
|
|
95
|
+
false,
|
|
96
|
+
["sign", "verify"]
|
|
97
|
+
);
|
|
98
|
+
const signature = await crypto.subtle.sign(
|
|
99
|
+
signingAlgorithm.name,
|
|
100
|
+
key,
|
|
101
|
+
new TextEncoder().encode(value)
|
|
102
|
+
);
|
|
103
|
+
return encodeURIComponent(`${value}.${btoa(String.fromCharCode(...new Uint8Array(signature)))}`);
|
|
104
|
+
}
|
|
105
|
+
async function serializeSignedCookie(name, value, secret, attributes = {}) {
|
|
106
|
+
return serializeCookieHeader(name, await signCookieValue(value, secret), attributes, true);
|
|
107
|
+
}
|
|
108
|
+
function appendCookieHeader(event, header) {
|
|
109
|
+
const nodeResponse = event.node?.res;
|
|
110
|
+
if (nodeResponse?.setHeader) {
|
|
111
|
+
const current = nodeResponse.getHeader?.("set-cookie");
|
|
112
|
+
if (Array.isArray(current))
|
|
113
|
+
nodeResponse.setHeader("set-cookie", [...current, header]);
|
|
114
|
+
else if (typeof current === "string")
|
|
115
|
+
nodeResponse.setHeader("set-cookie", [current, header]);
|
|
116
|
+
else
|
|
117
|
+
nodeResponse.setHeader("set-cookie", [header]);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const eventWithResponse = event;
|
|
121
|
+
const responseHeaders = eventWithResponse.res?.headers ?? eventWithResponse.response?.headers;
|
|
122
|
+
responseHeaders?.append("set-cookie", header);
|
|
123
|
+
}
|
|
124
|
+
function getSetCookieHeaders(headers) {
|
|
125
|
+
const getSetCookie = headers.getSetCookie;
|
|
126
|
+
const cookies = getSetCookie?.call(headers);
|
|
127
|
+
if (cookies?.length)
|
|
128
|
+
return cookies.flatMap((cookie) => splitCookiesString(cookie));
|
|
129
|
+
const header = headers.get("set-cookie");
|
|
130
|
+
return header ? splitCookiesString(header) : [];
|
|
131
|
+
}
|
|
132
|
+
function appendSetCookieHeaders(event, headers) {
|
|
133
|
+
for (const header of getSetCookieHeaders(headers))
|
|
134
|
+
appendCookieHeader(event, header);
|
|
135
|
+
}
|
|
136
|
+
function parseRequestCookies(cookieHeader) {
|
|
137
|
+
const cookies = /* @__PURE__ */ new Map();
|
|
138
|
+
if (!cookieHeader)
|
|
139
|
+
return cookies;
|
|
140
|
+
for (const pair of cookieHeader.split(cookiePairSeparatorRE)) {
|
|
141
|
+
if (!pair)
|
|
142
|
+
continue;
|
|
143
|
+
const separatorIndex = pair.indexOf("=");
|
|
144
|
+
if (separatorIndex < 0)
|
|
145
|
+
continue;
|
|
146
|
+
cookies.set(pair.slice(0, separatorIndex), pair.slice(separatorIndex + 1));
|
|
147
|
+
}
|
|
148
|
+
return cookies;
|
|
149
|
+
}
|
|
150
|
+
function serializeRequestCookies(cookies) {
|
|
151
|
+
if (!cookies.size)
|
|
152
|
+
return null;
|
|
153
|
+
return Array.from(cookies.entries()).map(([name, value]) => `${name}=${value}`).join("; ");
|
|
154
|
+
}
|
|
155
|
+
function extractResponseCookieValue(header) {
|
|
156
|
+
const separatorIndex = header.indexOf(";");
|
|
157
|
+
const cookiePair = separatorIndex >= 0 ? header.slice(0, separatorIndex) : header;
|
|
158
|
+
return cookiePair.slice(cookiePair.indexOf("=") + 1);
|
|
159
|
+
}
|
|
160
|
+
function getChunkedCookieNames(event, cookieName) {
|
|
161
|
+
const cookieNames = /* @__PURE__ */ new Set([cookieName]);
|
|
162
|
+
for (const name of parseRequestCookies(getIncomingRequestHeaders(event).get("cookie")).keys()) {
|
|
163
|
+
if (name.startsWith(`${cookieName}.`))
|
|
164
|
+
cookieNames.add(name);
|
|
165
|
+
}
|
|
166
|
+
return Array.from(cookieNames);
|
|
167
|
+
}
|
|
168
|
+
function expireCookie(event, cookieName, attributes) {
|
|
169
|
+
appendCookieHeader(event, serializeCookie(cookieName, "", {
|
|
170
|
+
...attributes,
|
|
171
|
+
expires: /* @__PURE__ */ new Date(0),
|
|
172
|
+
maxAge: 0
|
|
173
|
+
}));
|
|
174
|
+
}
|
|
175
|
+
function expireCookies(event, cookie) {
|
|
176
|
+
for (const cookieName of getChunkedCookieNames(event, cookie.name))
|
|
177
|
+
expireCookie(event, cookieName, cookie.attributes);
|
|
178
|
+
}
|
|
179
|
+
function updateRequestHeaders(event, sessionCookie, clearedCookieNames) {
|
|
180
|
+
const requestContext = getRequestSessionContext(event);
|
|
181
|
+
const incomingHeaders = getIncomingRequestHeaders(event);
|
|
182
|
+
const requestHeaders = new Headers(incomingHeaders);
|
|
183
|
+
const cookies = parseRequestCookies(incomingHeaders.get("cookie"));
|
|
184
|
+
for (const name of clearedCookieNames)
|
|
185
|
+
cookies.delete(name);
|
|
186
|
+
const sessionTokenName = sessionCookie.slice(0, sessionCookie.indexOf("="));
|
|
187
|
+
cookies.set(sessionTokenName, extractResponseCookieValue(sessionCookie));
|
|
188
|
+
const nextCookieHeader = serializeRequestCookies(cookies);
|
|
189
|
+
if (nextCookieHeader)
|
|
190
|
+
requestHeaders.set("cookie", nextCookieHeader);
|
|
191
|
+
else
|
|
192
|
+
requestHeaders.delete("cookie");
|
|
193
|
+
requestContext.requestHeaders = requestHeaders;
|
|
194
|
+
}
|
|
195
|
+
export async function getRequestSession(event) {
|
|
196
|
+
const context = getRequestSessionContext(event);
|
|
197
|
+
if (context.requestSession !== void 0)
|
|
198
|
+
return context.requestSession;
|
|
199
|
+
const inFlight = context[requestSessionLoadKey];
|
|
200
|
+
if (inFlight)
|
|
201
|
+
return inFlight;
|
|
202
|
+
const load = loadSession(event);
|
|
203
|
+
context[requestSessionLoadKey] = load;
|
|
204
|
+
try {
|
|
205
|
+
const session = await load;
|
|
206
|
+
context.requestSession = session;
|
|
207
|
+
return session;
|
|
208
|
+
} finally {
|
|
209
|
+
delete context[requestSessionLoadKey];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
export async function getUserSession(event) {
|
|
213
|
+
const context = getRequestSessionContext(event);
|
|
214
|
+
if (context.requestSession !== void 0)
|
|
215
|
+
return context.requestSession;
|
|
216
|
+
const inFlight = context[requestSessionLoadKey];
|
|
217
|
+
if (inFlight)
|
|
218
|
+
return inFlight;
|
|
219
|
+
return loadSession(event);
|
|
220
|
+
}
|
|
221
|
+
export async function refreshSessionCookieCache(event) {
|
|
222
|
+
const context = getRequestSessionContext(event);
|
|
223
|
+
const inFlight = context[requestSessionLoadKey];
|
|
224
|
+
if (inFlight)
|
|
225
|
+
await inFlight.catch(() => void 0);
|
|
226
|
+
delete context.requestSession;
|
|
227
|
+
const load = loadFreshSession(event).then(({ headers, response }) => {
|
|
228
|
+
appendSetCookieHeaders(event, headers);
|
|
229
|
+
context.requestSession = response;
|
|
230
|
+
return response;
|
|
231
|
+
});
|
|
232
|
+
context[requestSessionLoadKey] = load;
|
|
233
|
+
try {
|
|
234
|
+
return await load;
|
|
235
|
+
} finally {
|
|
236
|
+
if (context[requestSessionLoadKey] === load)
|
|
237
|
+
delete context[requestSessionLoadKey];
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
export async function setSessionCookie(event, token) {
|
|
241
|
+
const context = await getServerAuthContext(event);
|
|
242
|
+
const sessionCookie = await serializeSignedCookie(
|
|
243
|
+
context.authCookies.sessionToken.name,
|
|
244
|
+
token,
|
|
245
|
+
context.secret,
|
|
246
|
+
{
|
|
247
|
+
...context.authCookies.sessionToken.attributes,
|
|
248
|
+
maxAge: context.sessionConfig.expiresIn
|
|
249
|
+
}
|
|
250
|
+
);
|
|
251
|
+
appendCookieHeader(event, sessionCookie);
|
|
252
|
+
expireCookies(event, context.authCookies.sessionData);
|
|
253
|
+
expireCookies(event, context.authCookies.dontRememberToken);
|
|
254
|
+
const requestContext = getRequestSessionContext(event);
|
|
255
|
+
delete requestContext.requestSession;
|
|
256
|
+
delete requestContext[requestSessionLoadKey];
|
|
257
|
+
updateRequestHeaders(event, sessionCookie, [
|
|
258
|
+
...getChunkedCookieNames(event, context.authCookies.sessionData.name),
|
|
259
|
+
...getChunkedCookieNames(event, context.authCookies.dontRememberToken.name)
|
|
260
|
+
]);
|
|
261
|
+
}
|
|
262
|
+
export async function createSession(event, userId) {
|
|
263
|
+
const context = await getServerAuthContext(event);
|
|
264
|
+
return context.internalAdapter.createSession?.(userId, false);
|
|
265
|
+
}
|
|
266
|
+
export async function requireUserSession(event, options) {
|
|
267
|
+
const session = await getRequestSession(event);
|
|
268
|
+
if (!session)
|
|
269
|
+
throw createAuthError(401, "Authentication required");
|
|
270
|
+
if (options?.user) {
|
|
271
|
+
if (!matchesUser(session.user, options.user))
|
|
272
|
+
throw createAuthError(403, "Access denied");
|
|
273
|
+
}
|
|
274
|
+
if (options?.rule) {
|
|
275
|
+
const allowed = await options.rule({ user: session.user, session: session.session });
|
|
276
|
+
if (!allowed)
|
|
277
|
+
throw createAuthError(403, "Access denied");
|
|
278
|
+
}
|
|
279
|
+
return session;
|
|
280
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare module '#auth/database' {
|
|
2
|
+
export const db: any
|
|
3
|
+
export function createDatabase(...args: any[]): any
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
declare module '#imports' {
|
|
7
|
+
export function getRouteRules(event: any): any
|
|
8
|
+
export function useRuntimeConfig(): any
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare module '#auth/secondary-storage' {
|
|
12
|
+
export function createSecondaryStorage(...args: any[]): any
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare module '#auth/schema' {
|
|
16
|
+
export const schema: any
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare module '#auth/server' {
|
|
20
|
+
const createServerAuth: any
|
|
21
|
+
export default createServerAuth
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare module '@nuxthub/db' {
|
|
25
|
+
export const db: any
|
|
26
|
+
export const schema: any
|
|
27
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ComputedRef, Ref } from 'vue';
|
|
2
|
+
export interface AuthUser {
|
|
3
|
+
id: string;
|
|
4
|
+
createdAt: Date;
|
|
5
|
+
updatedAt: Date;
|
|
6
|
+
email: string;
|
|
7
|
+
emailVerified: boolean;
|
|
8
|
+
name: string;
|
|
9
|
+
image?: string | null;
|
|
10
|
+
}
|
|
11
|
+
export interface AuthSession {
|
|
12
|
+
id: string;
|
|
13
|
+
createdAt: Date;
|
|
14
|
+
updatedAt: Date;
|
|
15
|
+
userId: string;
|
|
16
|
+
expiresAt: Date;
|
|
17
|
+
token: string;
|
|
18
|
+
ipAddress?: string | null;
|
|
19
|
+
userAgent?: string | null;
|
|
20
|
+
}
|
|
21
|
+
export interface ServerAuthContext {
|
|
22
|
+
runtimeConfig: Record<string, unknown>;
|
|
23
|
+
db: unknown;
|
|
24
|
+
requestOrigin?: string;
|
|
25
|
+
}
|
|
26
|
+
export interface AuthSocialProviderRegistry {
|
|
27
|
+
}
|
|
28
|
+
export interface UserSessionComposable {
|
|
29
|
+
user: Ref<AuthUser | null>;
|
|
30
|
+
session: Ref<AuthSession | null>;
|
|
31
|
+
loggedIn: ComputedRef<boolean>;
|
|
32
|
+
ready: ComputedRef<boolean>;
|
|
33
|
+
fetchSession: (options?: {
|
|
34
|
+
headers?: HeadersInit;
|
|
35
|
+
force?: boolean;
|
|
36
|
+
}) => Promise<void>;
|
|
37
|
+
waitForSession: () => Promise<void>;
|
|
38
|
+
signOut: (options?: {
|
|
39
|
+
onSuccess?: () => void | Promise<void>;
|
|
40
|
+
}) => Promise<void>;
|
|
41
|
+
updateUser: (updates: Partial<AuthUser>) => Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
export type UserMatch<T> = {
|
|
44
|
+
[K in keyof T]?: T[K] | T[K][];
|
|
45
|
+
};
|
|
46
|
+
export interface AppSession {
|
|
47
|
+
user: AuthUser;
|
|
48
|
+
session: AuthSession;
|
|
49
|
+
}
|
|
50
|
+
export interface RequireSessionOptions {
|
|
51
|
+
user?: UserMatch<AuthUser>;
|
|
52
|
+
rule?: (ctx: {
|
|
53
|
+
user: AuthUser;
|
|
54
|
+
session: AuthSession;
|
|
55
|
+
}) => boolean | Promise<boolean>;
|
|
56
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { AuthSocialProviderRegistry, AuthUser, UserMatch } from '#nuxt-better-auth';
|
|
2
|
+
export type { AppSession, AuthSession, AuthSocialProviderRegistry, AuthUser, RequireSessionOptions, ServerAuthContext, UserMatch, UserSessionComposable } from './types/augment.js';
|
|
3
|
+
export type AuthSocialProviderId = AuthSocialProviderRegistry extends {
|
|
4
|
+
ids: infer T;
|
|
5
|
+
} ? Extract<T, string> : never;
|
|
6
|
+
export type { Auth, InferPluginTypes, InferSessionFromClient as InferSession, InferUserFromClient as InferUser } from 'better-auth';
|
|
7
|
+
export interface AuthActionError {
|
|
8
|
+
message: string;
|
|
9
|
+
code?: string;
|
|
10
|
+
status?: number;
|
|
11
|
+
raw: unknown;
|
|
12
|
+
}
|
|
13
|
+
export type AuthMode = 'guest' | 'user';
|
|
14
|
+
export type AuthMeta = false | AuthMode | {
|
|
15
|
+
only?: AuthMode;
|
|
16
|
+
redirectTo?: string;
|
|
17
|
+
user?: UserMatch<AuthUser>;
|
|
18
|
+
};
|
|
19
|
+
export type AuthRouteRules = Record<string, unknown> & {
|
|
20
|
+
auth?: AuthMeta;
|
|
21
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function matchesUser(user, match) {
|
|
2
|
+
for (const [key, expected] of Object.entries(match)) {
|
|
3
|
+
const actual = user[key];
|
|
4
|
+
if (Array.isArray(expected)) {
|
|
5
|
+
if (!expected.includes(actual))
|
|
6
|
+
return false;
|
|
7
|
+
} else {
|
|
8
|
+
if (actual !== expected)
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return true;
|
|
13
|
+
}
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { NuxtModule } from '@nuxt/schema'
|
|
2
|
+
|
|
3
|
+
import type { default as Module } from './module.mjs'
|
|
4
|
+
|
|
5
|
+
export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
|
|
6
|
+
|
|
7
|
+
export { type BetterAuthModuleOptions, type defineClientAuth, type defineServerAuth } from '../dist/runtime/config.js'
|
|
8
|
+
|
|
9
|
+
export { type AppSession, type Auth, type AuthActionError, type AuthMeta, type AuthMode, type AuthRouteRules, type AuthSession, type AuthSocialProviderId, type AuthUser, type InferSession, type InferUser, type RequireSessionOptions, type ServerAuthContext, type UserMatch } from '../dist/runtime/types.js'
|
|
10
|
+
|
|
11
|
+
export { default } from './module.mjs'
|
package/package.json
CHANGED
|
@@ -1,14 +1,115 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxtjs/better-auth",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.3",
|
|
5
|
+
"description": "Nuxt module for Better Auth integration with NuxtHub, route protection, session management, and role-based access",
|
|
6
|
+
"author": "onmax",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://better-auth.nuxt.dev",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/nuxt-modules/better-auth"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/types.d.mts",
|
|
16
|
+
"import": "./dist/module.mjs"
|
|
17
|
+
},
|
|
18
|
+
"./composables": {
|
|
19
|
+
"types": "./dist/runtime/composables.d.ts",
|
|
20
|
+
"import": "./dist/runtime/composables.js"
|
|
21
|
+
},
|
|
22
|
+
"./config": {
|
|
23
|
+
"types": "./dist/runtime/config.d.ts",
|
|
24
|
+
"import": "./dist/runtime/config.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"main": "./dist/module.mjs",
|
|
28
|
+
"typesVersions": {
|
|
29
|
+
"*": {
|
|
30
|
+
".": [
|
|
31
|
+
"./dist/types.d.mts"
|
|
32
|
+
],
|
|
33
|
+
"composables": [
|
|
34
|
+
"./dist/runtime/composables.d.ts"
|
|
35
|
+
],
|
|
36
|
+
"config": [
|
|
37
|
+
"./dist/runtime/config.d.ts"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist"
|
|
43
|
+
],
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@nuxthub/core": ">=0.10.5",
|
|
46
|
+
"better-auth": ">=1.7.0-rc.5 <2"
|
|
47
|
+
},
|
|
48
|
+
"peerDependenciesMeta": {
|
|
49
|
+
"@nuxthub/core": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@nuxt/kit": "^4.3.1",
|
|
55
|
+
"auth": "1.7.0-rc.5",
|
|
56
|
+
"defu": "^6.1.4",
|
|
57
|
+
"h3": "^1.15.11",
|
|
58
|
+
"jiti": "^2.6.1",
|
|
59
|
+
"pathe": "^2.0.3",
|
|
60
|
+
"radix3": "^1.1.2",
|
|
61
|
+
"std-env": "^4.0.0",
|
|
62
|
+
"ufo": "^1.6.4"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@antfu/eslint-config": "^9.0.0",
|
|
66
|
+
"@libsql/client": "^0.17.3",
|
|
67
|
+
"@libsql/linux-x64-gnu": "0.5.29",
|
|
68
|
+
"@nuxt/devtools": "^3.2.3",
|
|
69
|
+
"@nuxt/devtools-kit": "^3.2.4",
|
|
70
|
+
"@nuxt/module-builder": "^1.0.2",
|
|
71
|
+
"@nuxt/schema": "^4.3.1",
|
|
72
|
+
"@nuxt/test-utils": "^4.0.0",
|
|
73
|
+
"@nuxthub/core": "^0.10.6",
|
|
74
|
+
"@pinia/nuxt": "^0.11.3",
|
|
75
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
76
|
+
"@types/node": "latest",
|
|
77
|
+
"better-auth": "1.7.0-rc.5",
|
|
78
|
+
"better-call": "1.4.0",
|
|
79
|
+
"better-sqlite3": "^12.6.2",
|
|
80
|
+
"consola": "^3.4.2",
|
|
81
|
+
"drizzle-kit": "^0.31.10",
|
|
82
|
+
"drizzle-orm": "^0.45.1",
|
|
83
|
+
"eslint": "^10.0.3",
|
|
84
|
+
"nitropack": "2.13.1",
|
|
85
|
+
"nuxt": "^4.3.1",
|
|
86
|
+
"pinia": "^3.0.4",
|
|
87
|
+
"pkg-pr-new": "0.0.87",
|
|
88
|
+
"postgres": "^3.4.9",
|
|
89
|
+
"tinyexec": "^1.1.1",
|
|
90
|
+
"typescript": "~5.9.3",
|
|
91
|
+
"unstorage": "^1.17.5",
|
|
92
|
+
"vitest": "^4.0.18",
|
|
93
|
+
"vitest-package-exports": "^1.2.0",
|
|
94
|
+
"vue": "3.5.29",
|
|
95
|
+
"vue-tsc": "^3.2.7",
|
|
96
|
+
"wrangler": "4.72.0",
|
|
97
|
+
"yaml": "^2.8.3"
|
|
98
|
+
},
|
|
99
|
+
"scripts": {
|
|
100
|
+
"build": "pnpm build:docs",
|
|
101
|
+
"dev": "pnpm dev:prepare && nuxi dev playground",
|
|
102
|
+
"dev:build": "nuxi build playground",
|
|
103
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxi prepare playground",
|
|
104
|
+
"dev:docs": "nuxi dev docs",
|
|
105
|
+
"build:docs": "nuxi build docs",
|
|
106
|
+
"deploy": "wrangler deploy",
|
|
107
|
+
"lint": "eslint .",
|
|
108
|
+
"lint:fix": "eslint . --fix",
|
|
109
|
+
"typecheck": "vue-tsc --noEmit",
|
|
110
|
+
"typecheck:runtime-server": "cd test/cases/plugins-type-inference && nuxi prepare && vue-tsc --noEmit --pretty false -p .nuxt/tsconfig.server.json",
|
|
111
|
+
"typecheck:playground": "pnpm -C playground exec nuxi prepare && pnpm -C playground exec vue-tsc --noEmit -p .nuxt/tsconfig.app.json",
|
|
112
|
+
"test": "vitest run",
|
|
113
|
+
"test:watch": "vitest watch"
|
|
13
114
|
}
|
|
14
115
|
}
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
// Placeholder
|