@lenne.tech/nuxt-extensions 1.2.3 → 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,8 +1,10 @@
1
1
  import { useNuxtApp } from "#imports";
2
- import { createLtAuthClient } from "../lib/auth-client.js";
3
- let authClientInstance = null;
2
+ import {
3
+ getOrCreateLtAuthClient,
4
+ resetLtAuthClientSingleton
5
+ } from "../lib/auth-client.js";
4
6
  export function resetLtAuthClient() {
5
- authClientInstance = null;
7
+ resetLtAuthClientSingleton();
6
8
  }
7
9
  function isDevMode() {
8
10
  if (import.meta.server) {
@@ -21,28 +23,25 @@ function isDevMode() {
21
23
  return false;
22
24
  }
23
25
  export function useLtAuthClient() {
24
- if (!authClientInstance) {
25
- try {
26
- const nuxtApp = useNuxtApp();
27
- const config = nuxtApp.$config?.public?.ltExtensions?.auth || {};
28
- const isDev = isDevMode();
29
- let basePath = config.basePath || "/iam";
30
- if (isDev && basePath && !basePath.startsWith("/api")) {
31
- basePath = `/api${basePath}`;
32
- }
33
- authClientInstance = createLtAuthClient({
34
- baseURL: isDev ? "" : config.baseURL,
35
- basePath,
36
- twoFactorRedirectPath: config.twoFactorRedirectPath,
37
- enableAdmin: config.enableAdmin,
38
- enableTwoFactor: config.enableTwoFactor,
39
- enablePasskey: config.enablePasskey
40
- });
41
- } catch {
42
- 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}`;
43
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();
44
44
  }
45
- return authClientInstance;
46
45
  }
47
46
  export const ltAuthClient = {
48
47
  get instance() {
@@ -14,10 +14,13 @@ import type { LtAuthClientConfig } from "../types/index.js";
14
14
  * Call this in a Nuxt plugin (client-side) or in app.vue setup before
15
15
  * the auth client is used.
16
16
  *
17
+ * If the auth client was already created, it will be automatically recreated
18
+ * with the new plugins on next access.
19
+ *
17
20
  * @example
18
21
  * ```typescript
19
22
  * // plugins/auth-plugins.client.ts
20
- * import { registerLtAuthPlugins } from '@lenne.tech/nuxt-extensions';
23
+ * import { registerLtAuthPlugins } from '@lenne.tech/nuxt-extensions/lib';
21
24
  * import { organizationClient, magicLinkClient } from 'better-auth/client/plugins';
22
25
  *
23
26
  * export default defineNuxtPlugin(() => {
@@ -39,6 +42,17 @@ export declare function getLtAuthPluginRegistry(): unknown[];
39
42
  * Useful for testing or resetting state.
40
43
  */
41
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;
42
56
  /**
43
57
  * Creates a configured Better-Auth client with password hashing
44
58
  *
@@ -5,8 +5,14 @@ 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 _pluginsChangedAfterCreation = false;
9
+ let _authClientSingleton = null;
10
+ let _lastClientConfig = null;
8
11
  export function registerLtAuthPlugins(plugins) {
9
12
  _ltAuthPluginRegistry = [..._ltAuthPluginRegistry, ...plugins];
13
+ if (_authClientSingleton) {
14
+ _pluginsChangedAfterCreation = true;
15
+ }
10
16
  }
11
17
  export function getLtAuthPluginRegistry() {
12
18
  return _ltAuthPluginRegistry;
@@ -14,6 +20,23 @@ export function getLtAuthPluginRegistry() {
14
20
  export function clearLtAuthPluginRegistry() {
15
21
  _ltAuthPluginRegistry = [];
16
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
+ }
17
40
  export function createLtAuthClient(config = {}) {
18
41
  const isDev = isLtDevMode();
19
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, type LtAuthClient, } from "./auth-client.js";
2
+ export { clearLtAuthPluginRegistry, createLtAuthClient, getOrCreateLtAuthClient, getLtAuthPluginRegistry, registerLtAuthPlugins, resetLtAuthClientSingleton, type LtAuthClient, } from "./auth-client.js";
@@ -12,6 +12,8 @@ export {
12
12
  export {
13
13
  clearLtAuthPluginRegistry,
14
14
  createLtAuthClient,
15
+ getOrCreateLtAuthClient,
15
16
  getLtAuthPluginRegistry,
16
- registerLtAuthPlugins
17
+ registerLtAuthPlugins,
18
+ resetLtAuthClientSingleton
17
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.3",
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",