@onmax/nuxt-better-auth 0.0.2-alpha.28 → 0.0.2-alpha.29
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/module.d.mts +3 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +18 -8
- package/dist/runtime/app/composables/useUserSession.js +47 -0
- package/dist/runtime/config.d.ts +3 -0
- package/dist/runtime/server/api/_better-auth/config.get.d.ts +10 -9
- package/dist/runtime/server/api/_better-auth/config.get.js +1 -0
- package/dist/runtime/server/utils/auth.d.ts +2 -3
- package/package.json +3 -4
package/dist/module.d.mts
CHANGED
|
@@ -5,12 +5,13 @@ export { BetterAuthModuleOptions, defineClientAuth, defineServerAuth } from '../
|
|
|
5
5
|
import { BetterAuthOptions } from 'better-auth';
|
|
6
6
|
export { AppSession, Auth, AuthActionError, AuthMeta, AuthMode, AuthRouteRules, AuthSession, AuthUser, InferSession, InferUser, RequireSessionOptions, ServerAuthContext, UserMatch } from '../dist/runtime/types.js';
|
|
7
7
|
|
|
8
|
-
interface
|
|
8
|
+
interface RuntimeDefineServerAuthFn {
|
|
9
9
|
(...args: unknown[]): unknown;
|
|
10
10
|
_count: number;
|
|
11
11
|
}
|
|
12
12
|
declare global {
|
|
13
|
-
var
|
|
13
|
+
var __nuxtBetterAuthDefineServerAuth: RuntimeDefineServerAuthFn | undefined;
|
|
14
|
+
var defineServerAuth: RuntimeDefineServerAuthFn | undefined;
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
type DbDialect = 'sqlite' | 'postgresql' | 'mysql';
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -10,7 +10,7 @@ import { randomBytes } from 'node:crypto';
|
|
|
10
10
|
import { isCI, isTest } from 'std-env';
|
|
11
11
|
export { defineClientAuth, defineServerAuth } from '../dist/runtime/config.js';
|
|
12
12
|
|
|
13
|
-
const version = "0.0.2-alpha.
|
|
13
|
+
const version = "0.0.2-alpha.29";
|
|
14
14
|
|
|
15
15
|
function resolveDatabaseProvider(input) {
|
|
16
16
|
const enabledProviders = Object.entries(input.providers).filter(([_id, provider]) => provider.isEnabled?.(input.context) ?? true);
|
|
@@ -146,6 +146,7 @@ function setupRuntimeConfig(input) {
|
|
|
146
146
|
redirects: {
|
|
147
147
|
login: options.redirects?.login ?? "/login",
|
|
148
148
|
guest: options.redirects?.guest ?? "/",
|
|
149
|
+
authenticated: options.redirects?.authenticated,
|
|
149
150
|
logout: options.redirects?.logout
|
|
150
151
|
},
|
|
151
152
|
preserveRedirect: options.preserveRedirect ?? true,
|
|
@@ -210,13 +211,16 @@ async function generateDrizzleSchema(authOptions, dialect, schemaOptions) {
|
|
|
210
211
|
}
|
|
211
212
|
async function loadUserAuthConfig(configPath, throwOnError = false) {
|
|
212
213
|
const { createJiti } = await import('jiti');
|
|
213
|
-
const { defineServerAuth } = await import('../dist/runtime/config.js');
|
|
214
|
+
const { defineServerAuth: runtimeDefineServerAuth } = await import('../dist/runtime/config.js');
|
|
214
215
|
const jiti = createJiti(import.meta.url, { interopDefault: true, moduleCache: false });
|
|
216
|
+
if (!globalThis.__nuxtBetterAuthDefineServerAuth) {
|
|
217
|
+
runtimeDefineServerAuth._count = 0;
|
|
218
|
+
globalThis.__nuxtBetterAuthDefineServerAuth = runtimeDefineServerAuth;
|
|
219
|
+
}
|
|
215
220
|
if (!globalThis.defineServerAuth) {
|
|
216
|
-
defineServerAuth
|
|
217
|
-
globalThis.defineServerAuth = defineServerAuth;
|
|
221
|
+
globalThis.defineServerAuth = globalThis.__nuxtBetterAuthDefineServerAuth;
|
|
218
222
|
}
|
|
219
|
-
globalThis.
|
|
223
|
+
globalThis.__nuxtBetterAuthDefineServerAuth._count++;
|
|
220
224
|
try {
|
|
221
225
|
const mod = await jiti.import(configPath);
|
|
222
226
|
const configFn = mod.default;
|
|
@@ -235,9 +239,15 @@ async function loadUserAuthConfig(configPath, throwOnError = false) {
|
|
|
235
239
|
consola$1.error("[@onmax/nuxt-better-auth] Failed to load auth config for schema generation. Schema may be incomplete:", error);
|
|
236
240
|
return {};
|
|
237
241
|
} finally {
|
|
238
|
-
globalThis.
|
|
239
|
-
if (
|
|
240
|
-
|
|
242
|
+
const sharedDefineServerAuth = globalThis.__nuxtBetterAuthDefineServerAuth;
|
|
243
|
+
if (sharedDefineServerAuth) {
|
|
244
|
+
sharedDefineServerAuth._count--;
|
|
245
|
+
if (!sharedDefineServerAuth._count) {
|
|
246
|
+
globalThis.__nuxtBetterAuthDefineServerAuth = void 0;
|
|
247
|
+
if (globalThis.defineServerAuth === sharedDefineServerAuth) {
|
|
248
|
+
globalThis.defineServerAuth = void 0;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
241
251
|
}
|
|
242
252
|
}
|
|
243
253
|
}
|
|
@@ -128,6 +128,30 @@ export function useUserSession() {
|
|
|
128
128
|
}, 5e3);
|
|
129
129
|
});
|
|
130
130
|
}
|
|
131
|
+
function isSafeLocalRedirect(redirect) {
|
|
132
|
+
if (typeof redirect !== "string")
|
|
133
|
+
return;
|
|
134
|
+
if (!redirect.startsWith("/") || redirect.startsWith("//"))
|
|
135
|
+
return;
|
|
136
|
+
return redirect;
|
|
137
|
+
}
|
|
138
|
+
function resolvePostAuthRedirect() {
|
|
139
|
+
const authConfig = runtimeConfig.public.auth;
|
|
140
|
+
const redirectQueryKey = authConfig?.redirectQueryKey ?? "redirect";
|
|
141
|
+
const queryRedirect = requestURL.searchParams?.get(redirectQueryKey);
|
|
142
|
+
const safeQueryRedirect = isSafeLocalRedirect(queryRedirect);
|
|
143
|
+
if (safeQueryRedirect)
|
|
144
|
+
return safeQueryRedirect;
|
|
145
|
+
return isSafeLocalRedirect(authConfig?.redirects?.authenticated);
|
|
146
|
+
}
|
|
147
|
+
function resolvePostAuthSuccessRedirect() {
|
|
148
|
+
const target = resolvePostAuthRedirect();
|
|
149
|
+
if (!target)
|
|
150
|
+
return;
|
|
151
|
+
return async () => {
|
|
152
|
+
await navigateTo(target);
|
|
153
|
+
};
|
|
154
|
+
}
|
|
131
155
|
function wrapOnSuccess(cb) {
|
|
132
156
|
return async (ctx) => {
|
|
133
157
|
await fetchSession({ force: true });
|
|
@@ -146,6 +170,12 @@ export function useUserSession() {
|
|
|
146
170
|
const fetchOptions = isRecord(dataRecord?.fetchOptions) ? dataRecord.fetchOptions : void 0;
|
|
147
171
|
const nestedOnSuccess = fetchOptions?.onSuccess;
|
|
148
172
|
const topLevelOnSuccess = optionsRecord?.onSuccess;
|
|
173
|
+
const fallbackOnSuccess = resolvePostAuthSuccessRedirect();
|
|
174
|
+
const wrappedFallbackOnSuccess = fallbackOnSuccess && wrapOnSuccess(async () => {
|
|
175
|
+
if (!loggedIn.value)
|
|
176
|
+
return;
|
|
177
|
+
await fallbackOnSuccess();
|
|
178
|
+
});
|
|
149
179
|
if (typeof nestedOnSuccess === "function") {
|
|
150
180
|
const nextData = {
|
|
151
181
|
...dataRecord,
|
|
@@ -163,6 +193,23 @@ export function useUserSession() {
|
|
|
163
193
|
};
|
|
164
194
|
return method(data, nextOptions);
|
|
165
195
|
}
|
|
196
|
+
if (wrappedFallbackOnSuccess) {
|
|
197
|
+
if (fetchOptions) {
|
|
198
|
+
const nextData = {
|
|
199
|
+
...dataRecord,
|
|
200
|
+
fetchOptions: {
|
|
201
|
+
...fetchOptions,
|
|
202
|
+
onSuccess: wrappedFallbackOnSuccess
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
return method(nextData, options);
|
|
206
|
+
}
|
|
207
|
+
const nextOptions = {
|
|
208
|
+
...optionsRecord,
|
|
209
|
+
onSuccess: wrappedFallbackOnSuccess
|
|
210
|
+
};
|
|
211
|
+
return method(data, nextOptions);
|
|
212
|
+
}
|
|
166
213
|
return method(data, options);
|
|
167
214
|
});
|
|
168
215
|
}
|
package/dist/runtime/config.d.ts
CHANGED
|
@@ -29,6 +29,8 @@ export interface BetterAuthModuleOptions {
|
|
|
29
29
|
login?: string;
|
|
30
30
|
/** Where to redirect authenticated users on guest-only routes. Default: '/' */
|
|
31
31
|
guest?: string;
|
|
32
|
+
/** Where to navigate after successful signIn/signUp when no onSuccess is provided. Default: no automatic navigation */
|
|
33
|
+
authenticated?: string;
|
|
32
34
|
/** Where to navigate after logout. Default: no automatic navigation */
|
|
33
35
|
logout?: string;
|
|
34
36
|
};
|
|
@@ -74,6 +76,7 @@ export interface AuthRuntimeConfig {
|
|
|
74
76
|
redirects: {
|
|
75
77
|
login: string;
|
|
76
78
|
guest: string;
|
|
79
|
+
authenticated?: string;
|
|
77
80
|
logout?: string;
|
|
78
81
|
};
|
|
79
82
|
preserveRedirect: boolean;
|
|
@@ -4,6 +4,7 @@ declare const _default: import("h3").EventHandler<import("h3").EventHandlerReque
|
|
|
4
4
|
redirects: {
|
|
5
5
|
login: string;
|
|
6
6
|
guest: string;
|
|
7
|
+
authenticated: string | undefined;
|
|
7
8
|
logout: string | undefined;
|
|
8
9
|
};
|
|
9
10
|
preserveRedirect: boolean;
|
|
@@ -13,22 +14,22 @@ declare const _default: import("h3").EventHandler<import("h3").EventHandlerReque
|
|
|
13
14
|
databaseProvider: "none" | "nuxthub";
|
|
14
15
|
};
|
|
15
16
|
server: {
|
|
16
|
-
baseURL:
|
|
17
|
-
basePath:
|
|
17
|
+
baseURL: string | undefined;
|
|
18
|
+
basePath: string;
|
|
18
19
|
socialProviders: string[];
|
|
19
|
-
plugins:
|
|
20
|
-
trustedOrigins:
|
|
21
|
-
configuredTrustedOrigins:
|
|
20
|
+
plugins: string[];
|
|
21
|
+
trustedOrigins: string[];
|
|
22
|
+
configuredTrustedOrigins: string[];
|
|
22
23
|
session: {
|
|
23
24
|
expiresIn: string;
|
|
24
25
|
updateAge: string;
|
|
25
|
-
cookieCache:
|
|
26
|
+
cookieCache: boolean;
|
|
26
27
|
};
|
|
27
28
|
emailAndPassword: boolean;
|
|
28
|
-
rateLimit:
|
|
29
|
+
rateLimit: boolean;
|
|
29
30
|
advanced: {
|
|
30
|
-
useSecureCookies:
|
|
31
|
-
disableCSRFCheck:
|
|
31
|
+
useSecureCookies: string | boolean;
|
|
32
|
+
disableCSRFCheck: boolean;
|
|
32
33
|
};
|
|
33
34
|
};
|
|
34
35
|
};
|
|
@@ -21,6 +21,7 @@ export default defineEventHandler(async (event) => {
|
|
|
21
21
|
redirects: {
|
|
22
22
|
login: publicAuth?.redirects?.login ?? "/login",
|
|
23
23
|
guest: publicAuth?.redirects?.guest ?? "/",
|
|
24
|
+
authenticated: publicAuth?.redirects?.authenticated,
|
|
24
25
|
logout: publicAuth?.redirects?.logout
|
|
25
26
|
},
|
|
26
27
|
preserveRedirect: publicAuth?.preserveRedirect ?? true,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { Auth } from 'better-auth';
|
|
2
1
|
import type { H3Event } from 'h3';
|
|
3
|
-
import
|
|
4
|
-
type AuthInstance =
|
|
2
|
+
import { betterAuth } from 'better-auth';
|
|
3
|
+
type AuthInstance = ReturnType<typeof betterAuth>;
|
|
5
4
|
/** Returns Better Auth instance. Caches per resolved host (or single instance when siteUrl is explicit). */
|
|
6
5
|
export declare function serverAuth(event?: H3Event): AuthInstance;
|
|
7
6
|
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onmax/nuxt-better-auth",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.2-alpha.
|
|
4
|
+
"version": "0.0.2-alpha.29",
|
|
5
5
|
"packageManager": "pnpm@10.15.1",
|
|
6
6
|
"description": "Nuxt module for Better Auth integration with NuxtHub, route protection, session management, and role-based access",
|
|
7
7
|
"author": "onmax",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"lint:fix": "eslint . --fix",
|
|
57
57
|
"typecheck": "vue-tsc --noEmit",
|
|
58
58
|
"typecheck:runtime-server": "tsc --noEmit --pretty false -p src/runtime/server/tsconfig.json",
|
|
59
|
-
"typecheck:playground": "
|
|
59
|
+
"typecheck:playground": "pnpm -C playground exec nuxi prepare && pnpm -C playground exec vue-tsc --noEmit -p .nuxt/tsconfig.app.json",
|
|
60
60
|
"test": "vitest run",
|
|
61
61
|
"test:watch": "vitest watch"
|
|
62
62
|
},
|
|
@@ -116,8 +116,7 @@
|
|
|
116
116
|
"workerd"
|
|
117
117
|
],
|
|
118
118
|
"patchedDependencies": {
|
|
119
|
-
"@peculiar/x509@1.14.2": "patches/@peculiar__x509@1.14.2.patch"
|
|
120
|
-
"unenv@2.0.0-rc.24": "patches/unenv@2.0.0-rc.24.patch"
|
|
119
|
+
"@peculiar/x509@1.14.2": "patches/@peculiar__x509@1.14.2.patch"
|
|
121
120
|
},
|
|
122
121
|
"overrides": {
|
|
123
122
|
"reka-ui": "^2.6.1"
|