@nuxtjs/better-auth 0.1.4 → 0.2.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/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.
|
|
13
|
+
const version = "0.2.0";
|
|
14
14
|
|
|
15
15
|
const CONFIG_EXTENSIONS = [".ts", ".js"];
|
|
16
16
|
const CONFIG_EXTENSION_RE = /\.(?:ts|js)$/;
|
|
@@ -592,10 +592,7 @@ function setupRuntimeConfig(input) {
|
|
|
592
592
|
redirectQueryKey: options.redirectQueryKey ?? "redirect",
|
|
593
593
|
useDatabase: databaseProvider !== "none",
|
|
594
594
|
databaseProvider,
|
|
595
|
-
clientOnly
|
|
596
|
-
session: {
|
|
597
|
-
skipHydratedSsrGetSession: options.session?.skipHydratedSsrGetSession ?? false
|
|
598
|
-
}
|
|
595
|
+
clientOnly
|
|
599
596
|
});
|
|
600
597
|
if (clientOnly) {
|
|
601
598
|
const siteUrl = nuxt.options.runtimeConfig.public.siteUrl;
|
|
@@ -6,7 +6,6 @@ import { isRecord } from "../internal/utils.js";
|
|
|
6
6
|
import { createVueSafeAuthFacade, isAuthProxyProbeKey } from "../internal/vue-safe-auth-proxy.js";
|
|
7
7
|
import { wrapAuthMethod } from "../internal/wrap-auth-method.js";
|
|
8
8
|
import { getAuthRuntimeFlags, useRawAuthClient } from "./useAuthClient.js";
|
|
9
|
-
let _sessionSignalListenerBound = false;
|
|
10
9
|
let _signOutPromise = null;
|
|
11
10
|
function createServerOnlyActionNamespace(path) {
|
|
12
11
|
return new Proxy({}, {
|
|
@@ -22,24 +21,6 @@ function createServerOnlyActionNamespace(path) {
|
|
|
22
21
|
}
|
|
23
22
|
const _signInServerOnly = createServerOnlyActionNamespace("signIn");
|
|
24
23
|
const _signUpServerOnly = createServerOnlyActionNamespace("signUp");
|
|
25
|
-
function ensureSessionSignalListener(client, onSignal) {
|
|
26
|
-
if (_sessionSignalListenerBound)
|
|
27
|
-
return;
|
|
28
|
-
const store = client.$store;
|
|
29
|
-
if (!isRecord(store))
|
|
30
|
-
return;
|
|
31
|
-
const listen = store.listen;
|
|
32
|
-
if (typeof listen !== "function")
|
|
33
|
-
return;
|
|
34
|
-
_sessionSignalListenerBound = true;
|
|
35
|
-
const listenFn = listen;
|
|
36
|
-
listenFn("$sessionSignal", async () => {
|
|
37
|
-
try {
|
|
38
|
-
await onSignal();
|
|
39
|
-
} catch {
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
24
|
export function useUserSession() {
|
|
44
25
|
const runtimeFlags = getAuthRuntimeFlags();
|
|
45
26
|
const runtimeConfig = useRuntimeConfig();
|
|
@@ -60,21 +41,6 @@ export function useUserSession() {
|
|
|
60
41
|
return false;
|
|
61
42
|
return !session.value && !user.value;
|
|
62
43
|
});
|
|
63
|
-
const skipHydratedSsrGetSession = computed(() => {
|
|
64
|
-
const authConfig = runtimeConfig.public.auth;
|
|
65
|
-
return Boolean(authConfig?.session?.skipHydratedSsrGetSession);
|
|
66
|
-
});
|
|
67
|
-
const shouldSkipInitialClientSessionFetch = computed(() => {
|
|
68
|
-
if (!skipHydratedSsrGetSession.value)
|
|
69
|
-
return false;
|
|
70
|
-
if (!runtimeFlags.client)
|
|
71
|
-
return false;
|
|
72
|
-
if (!nuxtApp.payload.serverRendered)
|
|
73
|
-
return false;
|
|
74
|
-
if (isPrerenderedPayload.value)
|
|
75
|
-
return false;
|
|
76
|
-
return Boolean(session.value && user.value);
|
|
77
|
-
});
|
|
78
44
|
if (isPrerenderHydrationEmptySnapshot.value && authReady.value && !prerenderReadyResetQueued.value) {
|
|
79
45
|
prerenderReadyResetQueued.value = true;
|
|
80
46
|
nuxtApp.hook("app:suspense:resolve", () => {
|
|
@@ -86,8 +52,6 @@ export function useUserSession() {
|
|
|
86
52
|
}
|
|
87
53
|
});
|
|
88
54
|
}
|
|
89
|
-
if (shouldSkipInitialClientSessionFetch.value && !authReady.value)
|
|
90
|
-
authReady.value = true;
|
|
91
55
|
function clearSession() {
|
|
92
56
|
session.value = null;
|
|
93
57
|
user.value = null;
|
|
@@ -123,7 +87,7 @@ export function useUserSession() {
|
|
|
123
87
|
throw error;
|
|
124
88
|
}
|
|
125
89
|
}
|
|
126
|
-
if (runtimeFlags.client && rawClient
|
|
90
|
+
if (runtimeFlags.client && rawClient) {
|
|
127
91
|
const clientSession = rawClient.useSession();
|
|
128
92
|
watch(
|
|
129
93
|
() => clientSession.value,
|
|
@@ -172,9 +136,6 @@ export function useUserSession() {
|
|
|
172
136
|
}, 5e3);
|
|
173
137
|
});
|
|
174
138
|
}
|
|
175
|
-
if (runtimeFlags.client && rawClient && shouldSkipInitialClientSessionFetch.value) {
|
|
176
|
-
ensureSessionSignalListener(rawClient, () => fetchSession({ force: true }));
|
|
177
|
-
}
|
|
178
139
|
async function signOut(options) {
|
|
179
140
|
if (!rawClient)
|
|
180
141
|
throw new Error("signOut can only be called on client-side");
|
package/dist/runtime/config.d.ts
CHANGED
|
@@ -49,16 +49,6 @@ export interface BetterAuthModuleOptions {
|
|
|
49
49
|
* Default: 'redirect'
|
|
50
50
|
*/
|
|
51
51
|
redirectQueryKey?: string;
|
|
52
|
-
session?: {
|
|
53
|
-
/**
|
|
54
|
-
* When enabled, and session/user are already hydrated from SSR, skip the initial
|
|
55
|
-
* client `/api/auth/get-session` bootstrap request. This also skips Better Auth's
|
|
56
|
-
* session refresh manager on those pages.
|
|
57
|
-
*
|
|
58
|
-
* Default: false
|
|
59
|
-
*/
|
|
60
|
-
skipHydratedSsrGetSession?: boolean;
|
|
61
|
-
};
|
|
62
52
|
/**
|
|
63
53
|
* Enable secondary storage for sessions.
|
|
64
54
|
* - `true`: Use NuxtHub KV (requires hub.kv: true)
|
|
@@ -88,9 +78,6 @@ export interface AuthRuntimeConfig {
|
|
|
88
78
|
useDatabase: boolean;
|
|
89
79
|
databaseProvider: EffectiveDatabaseProviderId;
|
|
90
80
|
clientOnly: boolean;
|
|
91
|
-
session: {
|
|
92
|
-
skipHydratedSsrGetSession: boolean;
|
|
93
|
-
};
|
|
94
81
|
}
|
|
95
82
|
export interface AuthPrivateRuntimeConfig {
|
|
96
83
|
hubSecondaryStorage: boolean | 'custom';
|
|
@@ -2,6 +2,7 @@ import type { AppSession, AuthSession, RequireSessionOptions } from '#nuxt-bette
|
|
|
2
2
|
import type { ServerEvent } from '../internal/nitro-compat.js';
|
|
3
3
|
export declare function getRequestSession(event: ServerEvent): Promise<AppSession | null>;
|
|
4
4
|
export declare function getUserSession(event: ServerEvent): Promise<AppSession | null>;
|
|
5
|
+
export declare function setRequestSession(event: ServerEvent, session: AppSession | null): void;
|
|
5
6
|
export declare function refreshSessionCookieCache(event: ServerEvent): Promise<AppSession | null>;
|
|
6
7
|
export declare function setSessionCookie(event: ServerEvent, token: string): Promise<void>;
|
|
7
8
|
export declare function createSession(event: ServerEvent, userId: string): Promise<AuthSession>;
|
|
@@ -194,37 +194,47 @@ function updateRequestHeaders(event, sessionCookie, clearedCookieNames) {
|
|
|
194
194
|
}
|
|
195
195
|
export async function getRequestSession(event) {
|
|
196
196
|
const context = getRequestSessionContext(event);
|
|
197
|
-
if (context.requestSession !== void 0)
|
|
198
|
-
return context.requestSession;
|
|
199
197
|
const inFlight = context[requestSessionLoadKey];
|
|
200
198
|
if (inFlight)
|
|
201
199
|
return inFlight;
|
|
200
|
+
if (context.requestSession !== void 0)
|
|
201
|
+
return context.requestSession;
|
|
202
202
|
const load = loadSession(event);
|
|
203
203
|
context[requestSessionLoadKey] = load;
|
|
204
204
|
try {
|
|
205
205
|
const session = await load;
|
|
206
|
-
context
|
|
206
|
+
if (context[requestSessionLoadKey] === load)
|
|
207
|
+
context.requestSession = session;
|
|
207
208
|
return session;
|
|
208
209
|
} finally {
|
|
209
|
-
|
|
210
|
+
if (context[requestSessionLoadKey] === load)
|
|
211
|
+
delete context[requestSessionLoadKey];
|
|
210
212
|
}
|
|
211
213
|
}
|
|
212
214
|
export async function getUserSession(event) {
|
|
213
215
|
const context = getRequestSessionContext(event);
|
|
214
|
-
if (context.requestSession !== void 0)
|
|
215
|
-
return context.requestSession;
|
|
216
216
|
const inFlight = context[requestSessionLoadKey];
|
|
217
217
|
if (inFlight)
|
|
218
218
|
return inFlight;
|
|
219
|
+
if (context.requestSession !== void 0)
|
|
220
|
+
return context.requestSession;
|
|
219
221
|
return loadSession(event);
|
|
220
222
|
}
|
|
223
|
+
export function setRequestSession(event, session) {
|
|
224
|
+
const context = getRequestSessionContext(event);
|
|
225
|
+
context.requestSession = session;
|
|
226
|
+
delete context[requestSessionLoadKey];
|
|
227
|
+
}
|
|
221
228
|
export async function refreshSessionCookieCache(event) {
|
|
222
229
|
const context = getRequestSessionContext(event);
|
|
223
230
|
const inFlight = context[requestSessionLoadKey];
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
231
|
+
const load = (inFlight ?? Promise.resolve(null)).catch(() => void 0).then(async () => {
|
|
232
|
+
if (context[requestSessionLoadKey] !== load)
|
|
233
|
+
return context.requestSession ?? null;
|
|
234
|
+
delete context.requestSession;
|
|
235
|
+
const { headers, response } = await loadFreshSession(event);
|
|
236
|
+
if (context[requestSessionLoadKey] !== load)
|
|
237
|
+
return context.requestSession ?? null;
|
|
228
238
|
appendSetCookieHeaders(event, headers);
|
|
229
239
|
context.requestSession = response;
|
|
230
240
|
return response;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxtjs/better-auth",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"description": "Nuxt module for Better Auth integration with NuxtHub, route protection, session management, and role-based access",
|
|
6
6
|
"author": "onmax",
|
|
7
7
|
"license": "MIT",
|