@solvapay/react-supabase 1.0.8-preview.9 → 1.0.8

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 CHANGED
@@ -14,69 +14,40 @@ yarn add @solvapay/react-supabase @supabase/supabase-js
14
14
 
15
15
  ## Usage
16
16
 
17
- Use the Supabase adapter with `SolvaPayProvider`:
17
+ Pass your existing Supabase client to `createSupabaseAuthAdapter`:
18
18
 
19
19
  ```tsx
20
+ import { createClient } from '@supabase/supabase-js'
20
21
  import { SolvaPayProvider } from '@solvapay/react'
21
22
  import { createSupabaseAuthAdapter } from '@solvapay/react-supabase'
22
23
 
23
- const adapter = createSupabaseAuthAdapter({
24
- supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL!,
25
- supabaseAnonKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
26
- })
24
+ const supabase = createClient(
25
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
26
+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
27
+ )
28
+
29
+ const adapter = createSupabaseAuthAdapter({ client: supabase })
27
30
 
28
31
  export default function RootLayout({ children }) {
29
32
  return <SolvaPayProvider config={{ auth: { adapter } }}>{children}</SolvaPayProvider>
30
33
  }
31
34
  ```
32
35
 
36
+ Reusing the host app's client avoids spawning a second `GoTrue` instance and is the only reliable form with `@supabase/ssr`, custom `auth.storageKey`, or `persistSession: false`.
37
+
33
38
  ## API
34
39
 
35
40
  ### `createSupabaseAuthAdapter(config)`
36
41
 
37
42
  Creates a Supabase auth adapter instance.
38
43
 
39
- **Parameters:**
44
+ - `config.client` (`SupabaseClient`, required) — your existing Supabase client.
40
45
 
41
- - `config.supabaseUrl` (string, required) - Your Supabase project URL
42
- - `config.supabaseAnonKey` (string, required) - Your Supabase anonymous/public key
43
-
44
- **Returns:** `AuthAdapter` instance
46
+ **Returns:** `AuthAdapter` instance.
45
47
 
46
48
  ## How It Works
47
49
 
48
- The adapter:
49
-
50
- 1. Creates a Supabase client using your credentials
51
- 2. Gets the current session using `supabase.auth.getSession()`
52
- 3. Extracts the access token and user ID from the session
53
- 4. Returns `null` if no session is available (unauthenticated)
54
-
55
- The adapter handles errors gracefully and never throws - it returns `null` when authentication is not available.
56
-
57
- ## Example: Full Setup
58
-
59
- ```tsx
60
- 'use client'
61
-
62
- import { SolvaPayProvider } from '@solvapay/react'
63
- import { createSupabaseAuthAdapter } from '@solvapay/react-supabase'
64
-
65
- export default function RootLayout({ children }) {
66
- const adapter = createSupabaseAuthAdapter({
67
- supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL!,
68
- supabaseAnonKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
69
- })
70
-
71
- return (
72
- <html>
73
- <body>
74
- <SolvaPayProvider config={{ auth: { adapter } }}>{children}</SolvaPayProvider>
75
- </body>
76
- </html>
77
- )
78
- }
79
- ```
50
+ The adapter calls `supabase.auth.getSession()` to resolve the current access token and user ID, and subscribes to `supabase.auth.onAuthStateChange(...)` so `SolvaPayProvider` reacts immediately to sign-in, sign-out, and token-refresh events without polling. Returns `null` when there is no active session. Never throws.
80
51
 
81
52
  ## Custom Adapters
82
53
 
package/dist/index.cjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
@@ -36,41 +26,19 @@ module.exports = __toCommonJS(index_exports);
36
26
 
37
27
  // src/supabase-adapter.ts
38
28
  function createSupabaseAuthAdapter(config) {
39
- if (!config.supabaseUrl || !config.supabaseAnonKey) {
40
- throw new Error("SupabaseAuthAdapter requires both supabaseUrl and supabaseAnonKey");
29
+ if (!config || !config.client) {
30
+ throw new Error(
31
+ "createSupabaseAuthAdapter: pass { client } with your existing Supabase client."
32
+ );
41
33
  }
42
- let supabaseClient = null;
43
- let clientPromise = null;
44
- const getSupabaseClient = async () => {
45
- if (supabaseClient) {
46
- return supabaseClient;
47
- }
48
- if (clientPromise) {
49
- return clientPromise;
50
- }
51
- clientPromise = (async () => {
52
- try {
53
- const { createClient } = await import("@supabase/supabase-js");
54
- const client = createClient(config.supabaseUrl, config.supabaseAnonKey);
55
- supabaseClient = client;
56
- return client;
57
- } catch {
58
- clientPromise = null;
59
- throw new Error(
60
- "Failed to load @supabase/supabase-js. Make sure it is installed: npm install @supabase/supabase-js"
61
- );
62
- }
63
- })();
64
- return clientPromise;
65
- };
34
+ const client = config.client;
66
35
  return {
67
36
  async getToken() {
68
37
  if (typeof window === "undefined") return null;
69
38
  try {
70
- const supabase = await getSupabaseClient();
71
39
  const {
72
40
  data: { session }
73
- } = await supabase.auth.getSession();
41
+ } = await client.auth.getSession();
74
42
  return session?.access_token || null;
75
43
  } catch (error) {
76
44
  console.warn("[SupabaseAuthAdapter] Failed to get token:", error);
@@ -80,15 +48,36 @@ function createSupabaseAuthAdapter(config) {
80
48
  async getUserId() {
81
49
  if (typeof window === "undefined") return null;
82
50
  try {
83
- const supabase = await getSupabaseClient();
84
51
  const {
85
52
  data: { session }
86
- } = await supabase.auth.getSession();
53
+ } = await client.auth.getSession();
87
54
  return session?.user?.id || null;
88
55
  } catch (error) {
89
56
  console.warn("[SupabaseAuthAdapter] Failed to get user ID:", error);
90
57
  return null;
91
58
  }
59
+ },
60
+ subscribe(listener) {
61
+ if (typeof window === "undefined") {
62
+ return () => void 0;
63
+ }
64
+ try {
65
+ const {
66
+ data: { subscription }
67
+ } = client.auth.onAuthStateChange(() => {
68
+ listener();
69
+ });
70
+ return () => {
71
+ try {
72
+ subscription.unsubscribe();
73
+ } catch (error) {
74
+ console.warn("[SupabaseAuthAdapter] Failed to unsubscribe:", error);
75
+ }
76
+ };
77
+ } catch (error) {
78
+ console.warn("[SupabaseAuthAdapter] Failed to subscribe to auth changes:", error);
79
+ return () => void 0;
80
+ }
92
81
  }
93
82
  };
94
83
  }
package/dist/index.d.cts CHANGED
@@ -7,16 +7,73 @@ import { AuthAdapter } from '@solvapay/react';
7
7
  * This adapter integrates with Supabase Auth to get tokens and user IDs.
8
8
  */
9
9
 
10
- interface SupabaseAuthAdapterConfig {
11
- /**
12
- * Supabase project URL
13
- */
14
- supabaseUrl: string;
15
- /**
16
- * Supabase anonymous/public key
17
- */
18
- supabaseAnonKey: string;
19
- }
10
+ /**
11
+ * Structural shape of a Supabase client as used by the adapter.
12
+ *
13
+ * Matches `SupabaseClient` from `@supabase/supabase-js` without importing
14
+ * the type, so consumers pass the real client without a cast and this
15
+ * package stays dependency-light.
16
+ */
17
+ type SupabaseClientLike = {
18
+ auth: {
19
+ getSession: () => Promise<{
20
+ data: {
21
+ session: {
22
+ access_token?: string;
23
+ user?: {
24
+ id?: string;
25
+ };
26
+ } | null;
27
+ };
28
+ }>;
29
+ /**
30
+ * Supabase's native auth-state subscription. Structurally typed so we
31
+ * don't import `@supabase/supabase-js`. Returns `{ data: { subscription: { unsubscribe } } }`.
32
+ */
33
+ onAuthStateChange: (callback: (event: string, session: unknown) => void) => {
34
+ data: {
35
+ subscription: {
36
+ unsubscribe: () => void;
37
+ };
38
+ };
39
+ };
40
+ };
41
+ };
42
+ /**
43
+ * Configuration for `createSupabaseAuthAdapter`.
44
+ *
45
+ * Pass your app's existing Supabase client so only one `GoTrue` instance is
46
+ * ever in the page. This works with `@supabase/ssr`, custom storage keys,
47
+ * and SSR hydration without the adapter creating a second client.
48
+ */
49
+ type SupabaseAuthAdapterConfig = {
50
+ client: SupabaseClientLike;
51
+ };
52
+ /**
53
+ * Create a Supabase authentication adapter for SolvaPayProvider.
54
+ *
55
+ * @example
56
+ * ```tsx
57
+ * import { createClient } from '@supabase/supabase-js'
58
+ * import { createSupabaseAuthAdapter } from '@solvapay/react-supabase'
59
+ * import { SolvaPayProvider } from '@solvapay/react'
60
+ *
61
+ * const supabase = createClient(url, anonKey)
62
+ * const adapter = createSupabaseAuthAdapter({ client: supabase })
63
+ *
64
+ * export function App() {
65
+ * return (
66
+ * <SolvaPayProvider config={{ auth: { adapter } }}>
67
+ * <YourApp />
68
+ * </SolvaPayProvider>
69
+ * )
70
+ * }
71
+ * ```
72
+ *
73
+ * @see SolvaPayProvider (from `@solvapay/react`) for using the adapter
74
+ * @see {@link AuthAdapter} for the adapter interface
75
+ * @since 1.0.0
76
+ */
20
77
  declare function createSupabaseAuthAdapter(config: SupabaseAuthAdapterConfig): AuthAdapter;
21
78
 
22
- export { type SupabaseAuthAdapterConfig, createSupabaseAuthAdapter };
79
+ export { type SupabaseAuthAdapterConfig, type SupabaseClientLike, createSupabaseAuthAdapter };
package/dist/index.d.ts CHANGED
@@ -7,16 +7,73 @@ import { AuthAdapter } from '@solvapay/react';
7
7
  * This adapter integrates with Supabase Auth to get tokens and user IDs.
8
8
  */
9
9
 
10
- interface SupabaseAuthAdapterConfig {
11
- /**
12
- * Supabase project URL
13
- */
14
- supabaseUrl: string;
15
- /**
16
- * Supabase anonymous/public key
17
- */
18
- supabaseAnonKey: string;
19
- }
10
+ /**
11
+ * Structural shape of a Supabase client as used by the adapter.
12
+ *
13
+ * Matches `SupabaseClient` from `@supabase/supabase-js` without importing
14
+ * the type, so consumers pass the real client without a cast and this
15
+ * package stays dependency-light.
16
+ */
17
+ type SupabaseClientLike = {
18
+ auth: {
19
+ getSession: () => Promise<{
20
+ data: {
21
+ session: {
22
+ access_token?: string;
23
+ user?: {
24
+ id?: string;
25
+ };
26
+ } | null;
27
+ };
28
+ }>;
29
+ /**
30
+ * Supabase's native auth-state subscription. Structurally typed so we
31
+ * don't import `@supabase/supabase-js`. Returns `{ data: { subscription: { unsubscribe } } }`.
32
+ */
33
+ onAuthStateChange: (callback: (event: string, session: unknown) => void) => {
34
+ data: {
35
+ subscription: {
36
+ unsubscribe: () => void;
37
+ };
38
+ };
39
+ };
40
+ };
41
+ };
42
+ /**
43
+ * Configuration for `createSupabaseAuthAdapter`.
44
+ *
45
+ * Pass your app's existing Supabase client so only one `GoTrue` instance is
46
+ * ever in the page. This works with `@supabase/ssr`, custom storage keys,
47
+ * and SSR hydration without the adapter creating a second client.
48
+ */
49
+ type SupabaseAuthAdapterConfig = {
50
+ client: SupabaseClientLike;
51
+ };
52
+ /**
53
+ * Create a Supabase authentication adapter for SolvaPayProvider.
54
+ *
55
+ * @example
56
+ * ```tsx
57
+ * import { createClient } from '@supabase/supabase-js'
58
+ * import { createSupabaseAuthAdapter } from '@solvapay/react-supabase'
59
+ * import { SolvaPayProvider } from '@solvapay/react'
60
+ *
61
+ * const supabase = createClient(url, anonKey)
62
+ * const adapter = createSupabaseAuthAdapter({ client: supabase })
63
+ *
64
+ * export function App() {
65
+ * return (
66
+ * <SolvaPayProvider config={{ auth: { adapter } }}>
67
+ * <YourApp />
68
+ * </SolvaPayProvider>
69
+ * )
70
+ * }
71
+ * ```
72
+ *
73
+ * @see SolvaPayProvider (from `@solvapay/react`) for using the adapter
74
+ * @see {@link AuthAdapter} for the adapter interface
75
+ * @since 1.0.0
76
+ */
20
77
  declare function createSupabaseAuthAdapter(config: SupabaseAuthAdapterConfig): AuthAdapter;
21
78
 
22
- export { type SupabaseAuthAdapterConfig, createSupabaseAuthAdapter };
79
+ export { type SupabaseAuthAdapterConfig, type SupabaseClientLike, createSupabaseAuthAdapter };
package/dist/index.js CHANGED
@@ -1,40 +1,18 @@
1
1
  // src/supabase-adapter.ts
2
2
  function createSupabaseAuthAdapter(config) {
3
- if (!config.supabaseUrl || !config.supabaseAnonKey) {
4
- throw new Error("SupabaseAuthAdapter requires both supabaseUrl and supabaseAnonKey");
3
+ if (!config || !config.client) {
4
+ throw new Error(
5
+ "createSupabaseAuthAdapter: pass { client } with your existing Supabase client."
6
+ );
5
7
  }
6
- let supabaseClient = null;
7
- let clientPromise = null;
8
- const getSupabaseClient = async () => {
9
- if (supabaseClient) {
10
- return supabaseClient;
11
- }
12
- if (clientPromise) {
13
- return clientPromise;
14
- }
15
- clientPromise = (async () => {
16
- try {
17
- const { createClient } = await import("@supabase/supabase-js");
18
- const client = createClient(config.supabaseUrl, config.supabaseAnonKey);
19
- supabaseClient = client;
20
- return client;
21
- } catch {
22
- clientPromise = null;
23
- throw new Error(
24
- "Failed to load @supabase/supabase-js. Make sure it is installed: npm install @supabase/supabase-js"
25
- );
26
- }
27
- })();
28
- return clientPromise;
29
- };
8
+ const client = config.client;
30
9
  return {
31
10
  async getToken() {
32
11
  if (typeof window === "undefined") return null;
33
12
  try {
34
- const supabase = await getSupabaseClient();
35
13
  const {
36
14
  data: { session }
37
- } = await supabase.auth.getSession();
15
+ } = await client.auth.getSession();
38
16
  return session?.access_token || null;
39
17
  } catch (error) {
40
18
  console.warn("[SupabaseAuthAdapter] Failed to get token:", error);
@@ -44,15 +22,36 @@ function createSupabaseAuthAdapter(config) {
44
22
  async getUserId() {
45
23
  if (typeof window === "undefined") return null;
46
24
  try {
47
- const supabase = await getSupabaseClient();
48
25
  const {
49
26
  data: { session }
50
- } = await supabase.auth.getSession();
27
+ } = await client.auth.getSession();
51
28
  return session?.user?.id || null;
52
29
  } catch (error) {
53
30
  console.warn("[SupabaseAuthAdapter] Failed to get user ID:", error);
54
31
  return null;
55
32
  }
33
+ },
34
+ subscribe(listener) {
35
+ if (typeof window === "undefined") {
36
+ return () => void 0;
37
+ }
38
+ try {
39
+ const {
40
+ data: { subscription }
41
+ } = client.auth.onAuthStateChange(() => {
42
+ listener();
43
+ });
44
+ return () => {
45
+ try {
46
+ subscription.unsubscribe();
47
+ } catch (error) {
48
+ console.warn("[SupabaseAuthAdapter] Failed to unsubscribe:", error);
49
+ }
50
+ };
51
+ } catch (error) {
52
+ console.warn("[SupabaseAuthAdapter] Failed to subscribe to auth changes:", error);
53
+ return () => void 0;
54
+ }
56
55
  }
57
56
  };
58
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solvapay/react-supabase",
3
- "version": "1.0.8-preview.9",
3
+ "version": "1.0.8",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -19,19 +19,19 @@
19
19
  },
20
20
  "peerDependencies": {
21
21
  "@supabase/supabase-js": "^2.0.0",
22
- "@solvapay/react": "^1.0.8-preview.9"
22
+ "@solvapay/react": "^1.0.11-preview-e89911bc8c4dd3759eab37b440559220b691fd23"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/react": "^19.2.14",
26
26
  "tsup": "^8.5.1",
27
27
  "typescript": "^5.9.3",
28
28
  "vitest": "^4.1.2",
29
- "@solvapay/react": "1.0.8-preview.9"
29
+ "@solvapay/react": "1.0.11-preview-e89911bc8c4dd3759eab37b440559220b691fd23"
30
30
  },
31
31
  "scripts": {
32
32
  "build": "tsup src/index.ts --format esm,cjs --dts --tsconfig tsconfig.build.json",
33
- "test": "vitest run || exit 0",
34
- "test:unit": "vitest run || exit 0",
33
+ "test": "vitest run",
34
+ "test:unit": "vitest run",
35
35
  "test:watch": "vitest",
36
36
  "lint": "eslint src --config ../../eslint.config.react.mjs",
37
37
  "lint:fix": "eslint src --config ../../eslint.config.react.mjs --fix"