@lenne.tech/nuxt-extensions 1.2.4 → 1.2.5

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.
@@ -1,13 +1,11 @@
1
1
  import { useNuxtApp } from "#imports";
2
2
  import {
3
- createLtAuthClient,
4
- setResetAuthClientCallback
3
+ getOrCreateLtAuthClient,
4
+ resetLtAuthClientSingleton
5
5
  } from "../lib/auth-client.js";
6
- let authClientInstance = null;
7
6
  export function resetLtAuthClient() {
8
- authClientInstance = null;
7
+ resetLtAuthClientSingleton();
9
8
  }
10
- setResetAuthClientCallback(resetLtAuthClient);
11
9
  function isDevMode() {
12
10
  if (import.meta.server) {
13
11
  return process.env.NODE_ENV !== "production";
@@ -25,28 +23,25 @@ function isDevMode() {
25
23
  return false;
26
24
  }
27
25
  export function useLtAuthClient() {
28
- if (!authClientInstance) {
29
- try {
30
- const nuxtApp = useNuxtApp();
31
- const config = nuxtApp.$config?.public?.ltExtensions?.auth || {};
32
- const isDev = isDevMode();
33
- let basePath = config.basePath || "/iam";
34
- if (isDev && basePath && !basePath.startsWith("/api")) {
35
- basePath = `/api${basePath}`;
36
- }
37
- authClientInstance = createLtAuthClient({
38
- baseURL: isDev ? "" : config.baseURL,
39
- basePath,
40
- twoFactorRedirectPath: config.twoFactorRedirectPath,
41
- enableAdmin: config.enableAdmin,
42
- enableTwoFactor: config.enableTwoFactor,
43
- enablePasskey: config.enablePasskey
44
- });
45
- } catch {
46
- authClientInstance = createLtAuthClient();
26
+ try {
27
+ const nuxtApp = useNuxtApp();
28
+ const config = nuxtApp.$config?.public?.ltExtensions?.auth || {};
29
+ const isDev = isDevMode();
30
+ let basePath = config.basePath || "/iam";
31
+ if (isDev && basePath && !basePath.startsWith("/api")) {
32
+ basePath = `/api${basePath}`;
47
33
  }
34
+ return getOrCreateLtAuthClient({
35
+ baseURL: isDev ? "" : config.baseURL,
36
+ basePath,
37
+ twoFactorRedirectPath: config.twoFactorRedirectPath,
38
+ enableAdmin: config.enableAdmin,
39
+ enableTwoFactor: config.enableTwoFactor,
40
+ enablePasskey: config.enablePasskey
41
+ });
42
+ } catch {
43
+ return getOrCreateLtAuthClient();
48
44
  }
49
- return authClientInstance;
50
45
  }
51
46
  export const ltAuthClient = {
52
47
  get instance() {
@@ -8,19 +8,14 @@
8
8
  * plain text password transmission over the network.
9
9
  */
10
10
  import type { LtAuthClientConfig } from "../types/index.js";
11
- /**
12
- * Set the callback to reset the auth client.
13
- * Called internally by use-lt-auth-client.ts
14
- */
15
- export declare function setResetAuthClientCallback(callback: () => void): void;
16
11
  /**
17
12
  * Register additional Better Auth plugins before auth client initialization.
18
13
  *
19
14
  * Call this in a Nuxt plugin (client-side) or in app.vue setup before
20
15
  * the auth client is used.
21
16
  *
22
- * If the auth client was already created, it will be automatically reset
23
- * so the next call recreates it with the new plugins.
17
+ * If the auth client was already created, it will be automatically recreated
18
+ * with the new plugins on next access.
24
19
  *
25
20
  * @example
26
21
  * ```typescript
@@ -47,6 +42,17 @@ export declare function getLtAuthPluginRegistry(): unknown[];
47
42
  * Useful for testing or resetting state.
48
43
  */
49
44
  export declare function clearLtAuthPluginRegistry(): void;
45
+ /**
46
+ * Reset the auth client singleton.
47
+ * The client will be recreated on next access.
48
+ */
49
+ export declare function resetLtAuthClientSingleton(): void;
50
+ /**
51
+ * Get or create the auth client singleton.
52
+ * This is the main entry point for accessing the auth client.
53
+ * If plugins were registered after initial creation, the client is recreated.
54
+ */
55
+ export declare function getOrCreateLtAuthClient(config?: LtAuthClientConfig): LtAuthClient;
50
56
  /**
51
57
  * Creates a configured Better-Auth client with password hashing
52
58
  *
@@ -5,14 +5,13 @@ import { navigateTo } from "#imports";
5
5
  import { ltSha256 } from "../utils/crypto.js";
6
6
  import { createLtAuthFetch, isLtDevMode } from "./auth-state.js";
7
7
  let _ltAuthPluginRegistry = [];
8
- let _resetAuthClientCallback = null;
9
- export function setResetAuthClientCallback(callback) {
10
- _resetAuthClientCallback = callback;
11
- }
8
+ let _pluginsChangedAfterCreation = false;
9
+ let _authClientSingleton = null;
10
+ let _lastClientConfig = null;
12
11
  export function registerLtAuthPlugins(plugins) {
13
12
  _ltAuthPluginRegistry = [..._ltAuthPluginRegistry, ...plugins];
14
- if (_resetAuthClientCallback) {
15
- _resetAuthClientCallback();
13
+ if (_authClientSingleton) {
14
+ _pluginsChangedAfterCreation = true;
16
15
  }
17
16
  }
18
17
  export function getLtAuthPluginRegistry() {
@@ -21,6 +20,23 @@ export function getLtAuthPluginRegistry() {
21
20
  export function clearLtAuthPluginRegistry() {
22
21
  _ltAuthPluginRegistry = [];
23
22
  }
23
+ export function resetLtAuthClientSingleton() {
24
+ _authClientSingleton = null;
25
+ _pluginsChangedAfterCreation = false;
26
+ }
27
+ export function getOrCreateLtAuthClient(config) {
28
+ if (config) {
29
+ _lastClientConfig = config;
30
+ }
31
+ if (_pluginsChangedAfterCreation && _authClientSingleton) {
32
+ _authClientSingleton = null;
33
+ _pluginsChangedAfterCreation = false;
34
+ }
35
+ if (!_authClientSingleton) {
36
+ _authClientSingleton = createLtAuthClient(_lastClientConfig || {});
37
+ }
38
+ return _authClientSingleton;
39
+ }
24
40
  export function createLtAuthClient(config = {}) {
25
41
  const isDev = isLtDevMode();
26
42
  const defaultBaseURL = isDev ? "" : import.meta.env?.VITE_API_URL || process.env.API_URL || "http://localhost:3000";
@@ -1,2 +1,2 @@
1
1
  export { attemptLtJwtSwitch, createLtAuthFetch, getLtApiBase, getLtAuthMode, getLtJwtToken, isLtAuthenticated, ltAuthFetch, setLtAuthMode, setLtJwtToken, } from "./auth-state.js";
2
- export { clearLtAuthPluginRegistry, createLtAuthClient, getLtAuthPluginRegistry, registerLtAuthPlugins, setResetAuthClientCallback, type LtAuthClient, } from "./auth-client.js";
2
+ export { clearLtAuthPluginRegistry, createLtAuthClient, getOrCreateLtAuthClient, getLtAuthPluginRegistry, registerLtAuthPlugins, resetLtAuthClientSingleton, type LtAuthClient, } from "./auth-client.js";
@@ -12,7 +12,8 @@ export {
12
12
  export {
13
13
  clearLtAuthPluginRegistry,
14
14
  createLtAuthClient,
15
+ getOrCreateLtAuthClient,
15
16
  getLtAuthPluginRegistry,
16
17
  registerLtAuthPlugins,
17
- setResetAuthClientCallback
18
+ resetLtAuthClientSingleton
18
19
  } from "./auth-client.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nuxt-extensions",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "Reusable Nuxt 4 composables, components, and Better-Auth integration for lenne.tech projects",
5
5
  "repository": {
6
6
  "type": "git",