@vaultix.ai/react 0.3.0 → 0.3.2

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/index.d.mts CHANGED
@@ -42,6 +42,22 @@ interface VaultixSession {
42
42
  expiresAt: number;
43
43
  claims: SessionClaims;
44
44
  }
45
+ interface ConnectPlatformOptions {
46
+ /** OAuth scopes to request. Joined by the provider's delimiter (space or comma). */
47
+ scopes?: string[];
48
+ /**
49
+ * Meta-specific: a Login Config ID registered on the Vaultix Meta app.
50
+ * When provided, takes precedence over `scopes` (Meta's rule).
51
+ */
52
+ configId?: string;
53
+ /**
54
+ * Extra provider-specific params forwarded to the authorization URL.
55
+ * e.g. { shop: "mystore.myshopify.com" } for Shopify.
56
+ */
57
+ extraParams?: Record<string, string>;
58
+ /** Where to redirect after the user authorizes. Defaults to current page. */
59
+ redirectUrl?: string;
60
+ }
45
61
  interface VaultixContextValue {
46
62
  user: VaultixUser | null;
47
63
  session: VaultixSession | null;
@@ -49,6 +65,13 @@ interface VaultixContextValue {
49
65
  isLoaded: boolean;
50
66
  isSignedIn: boolean;
51
67
  signOut: () => Promise<void>;
68
+ /**
69
+ * Initiate a platform OAuth connection flow (posting, reading, etc.).
70
+ * Redirects the user to the provider's authorization page via Vaultix.
71
+ * On completion, Vaultix redirects back with `__vaultix_connect` + `provider`
72
+ * query params that your app exchanges via POST /oauth/{provider}/connect-complete.
73
+ */
74
+ connectPlatform: (provider: string, options?: ConnectPlatformOptions) => void;
52
75
  }
53
76
  interface VaultixProviderProps {
54
77
  publishableKey: string;
@@ -149,4 +172,9 @@ interface OrganizationSwitcherProps {
149
172
  }
150
173
  declare function OrganizationSwitcher({ afterSwitchUrl, className, }: OrganizationSwitcherProps): react_jsx_runtime.JSX.Element | null;
151
174
 
152
- export { type ChallengeType, OrganizationSwitcher, RedirectToSignIn, RedirectToSignUp, type RiskLevel, type SessionClaims, SignIn, SignUp, SignedIn, SignedOut, UserButton, type VaultixContextValue, type VaultixOrganization, VaultixProvider, type VaultixProviderProps, type VaultixSession, type VaultixUser, useAuth, useOrganization, useSession, useUser, useVaultix };
175
+ /** Returns the stored session JWT, or null if not signed in. */
176
+ declare function getStoredToken(): string | null;
177
+ /** Clears the stored session JWT. Call on 401 so the user is prompted to sign in again. */
178
+ declare function clearStoredToken(): void;
179
+
180
+ export { type ChallengeType, type ConnectPlatformOptions, OrganizationSwitcher, RedirectToSignIn, RedirectToSignUp, type RiskLevel, type SessionClaims, SignIn, SignUp, SignedIn, SignedOut, UserButton, type VaultixContextValue, type VaultixOrganization, VaultixProvider, type VaultixProviderProps, type VaultixSession, type VaultixUser, clearStoredToken, getStoredToken, useAuth, useOrganization, useSession, useUser, useVaultix };
package/dist/index.d.ts CHANGED
@@ -42,6 +42,22 @@ interface VaultixSession {
42
42
  expiresAt: number;
43
43
  claims: SessionClaims;
44
44
  }
45
+ interface ConnectPlatformOptions {
46
+ /** OAuth scopes to request. Joined by the provider's delimiter (space or comma). */
47
+ scopes?: string[];
48
+ /**
49
+ * Meta-specific: a Login Config ID registered on the Vaultix Meta app.
50
+ * When provided, takes precedence over `scopes` (Meta's rule).
51
+ */
52
+ configId?: string;
53
+ /**
54
+ * Extra provider-specific params forwarded to the authorization URL.
55
+ * e.g. { shop: "mystore.myshopify.com" } for Shopify.
56
+ */
57
+ extraParams?: Record<string, string>;
58
+ /** Where to redirect after the user authorizes. Defaults to current page. */
59
+ redirectUrl?: string;
60
+ }
45
61
  interface VaultixContextValue {
46
62
  user: VaultixUser | null;
47
63
  session: VaultixSession | null;
@@ -49,6 +65,13 @@ interface VaultixContextValue {
49
65
  isLoaded: boolean;
50
66
  isSignedIn: boolean;
51
67
  signOut: () => Promise<void>;
68
+ /**
69
+ * Initiate a platform OAuth connection flow (posting, reading, etc.).
70
+ * Redirects the user to the provider's authorization page via Vaultix.
71
+ * On completion, Vaultix redirects back with `__vaultix_connect` + `provider`
72
+ * query params that your app exchanges via POST /oauth/{provider}/connect-complete.
73
+ */
74
+ connectPlatform: (provider: string, options?: ConnectPlatformOptions) => void;
52
75
  }
53
76
  interface VaultixProviderProps {
54
77
  publishableKey: string;
@@ -149,4 +172,9 @@ interface OrganizationSwitcherProps {
149
172
  }
150
173
  declare function OrganizationSwitcher({ afterSwitchUrl, className, }: OrganizationSwitcherProps): react_jsx_runtime.JSX.Element | null;
151
174
 
152
- export { type ChallengeType, OrganizationSwitcher, RedirectToSignIn, RedirectToSignUp, type RiskLevel, type SessionClaims, SignIn, SignUp, SignedIn, SignedOut, UserButton, type VaultixContextValue, type VaultixOrganization, VaultixProvider, type VaultixProviderProps, type VaultixSession, type VaultixUser, useAuth, useOrganization, useSession, useUser, useVaultix };
175
+ /** Returns the stored session JWT, or null if not signed in. */
176
+ declare function getStoredToken(): string | null;
177
+ /** Clears the stored session JWT. Call on 401 so the user is prompted to sign in again. */
178
+ declare function clearStoredToken(): void;
179
+
180
+ export { type ChallengeType, type ConnectPlatformOptions, OrganizationSwitcher, RedirectToSignIn, RedirectToSignUp, type RiskLevel, type SessionClaims, SignIn, SignUp, SignedIn, SignedOut, UserButton, type VaultixContextValue, type VaultixOrganization, VaultixProvider, type VaultixProviderProps, type VaultixSession, type VaultixUser, clearStoredToken, getStoredToken, useAuth, useOrganization, useSession, useUser, useVaultix };
package/dist/index.js CHANGED
@@ -29,6 +29,8 @@ __export(index_exports, {
29
29
  SignedOut: () => SignedOut,
30
30
  UserButton: () => UserButton,
31
31
  VaultixProvider: () => VaultixProvider,
32
+ clearStoredToken: () => clearStoredToken,
33
+ getStoredToken: () => getStoredToken,
32
34
  useAuth: () => useAuth,
33
35
  useOrganization: () => useOrganization,
34
36
  useSession: () => useSession,
@@ -211,25 +213,35 @@ function VaultixProvider({
211
213
  const jwt = jwtRef.current;
212
214
  clearAuth();
213
215
  if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
214
- try {
215
- const headers = {};
216
- if (jwt) headers["Authorization"] = `Bearer ${jwt}`;
217
- await fetch(`${apiOrigin}/api/v1/session`, {
218
- method: "DELETE",
219
- credentials: jwt ? "omit" : "include",
220
- headers
221
- });
222
- } catch {
223
- }
216
+ const headers = {};
217
+ if (jwt) headers["Authorization"] = `Bearer ${jwt}`;
218
+ fetch(`${apiOrigin}/api/v1/session`, {
219
+ method: "DELETE",
220
+ credentials: jwt ? "omit" : "include",
221
+ headers
222
+ }).catch(() => {
223
+ });
224
224
  if (afterSignInUrl) window.location.href = afterSignInUrl;
225
225
  }, [apiOrigin, afterSignInUrl, clearAuth]);
226
+ const connectPlatform = (0, import_react.useCallback)((provider, options = {}) => {
227
+ const params = new URLSearchParams({ mode: "connect" });
228
+ params.set("redirect_url", options.redirectUrl ?? (typeof window !== "undefined" ? window.location.href : "/"));
229
+ params.set("pk", publishableKey);
230
+ if (options.scopes?.length) params.set("scope", options.scopes.join(" "));
231
+ if (options.configId) params.set("config_id", options.configId);
232
+ if (options.extraParams) Object.entries(options.extraParams).forEach(([k, v]) => params.set(k, v));
233
+ if (typeof window !== "undefined") {
234
+ window.location.href = `${apiOrigin}/api/v1/auth/oauth/${provider}/connect?${params}`;
235
+ }
236
+ }, [apiOrigin, publishableKey]);
226
237
  const value = {
227
238
  user,
228
239
  session,
229
240
  organization,
230
241
  isLoaded,
231
242
  isSignedIn: !!session,
232
- signOut
243
+ signOut,
244
+ connectPlatform
233
245
  };
234
246
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(VaultixContext.Provider, { value, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
235
247
  "div",
@@ -1568,6 +1580,22 @@ function MiniSpinner() {
1568
1580
  /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("path", { className: "opacity-75", fill: "currentColor", d: "M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" })
1569
1581
  ] });
1570
1582
  }
1583
+
1584
+ // src/index.ts
1585
+ var STORAGE_KEY2 = "vaultix_jwt";
1586
+ function getStoredToken() {
1587
+ try {
1588
+ return (typeof sessionStorage !== "undefined" ? sessionStorage : null)?.getItem(STORAGE_KEY2) ?? null;
1589
+ } catch {
1590
+ return null;
1591
+ }
1592
+ }
1593
+ function clearStoredToken() {
1594
+ try {
1595
+ if (typeof sessionStorage !== "undefined") sessionStorage.removeItem(STORAGE_KEY2);
1596
+ } catch {
1597
+ }
1598
+ }
1571
1599
  // Annotate the CommonJS export names for ESM import in node:
1572
1600
  0 && (module.exports = {
1573
1601
  OrganizationSwitcher,
@@ -1579,6 +1607,8 @@ function MiniSpinner() {
1579
1607
  SignedOut,
1580
1608
  UserButton,
1581
1609
  VaultixProvider,
1610
+ clearStoredToken,
1611
+ getStoredToken,
1582
1612
  useAuth,
1583
1613
  useOrganization,
1584
1614
  useSession,
package/dist/index.mjs CHANGED
@@ -179,25 +179,35 @@ function VaultixProvider({
179
179
  const jwt = jwtRef.current;
180
180
  clearAuth();
181
181
  if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
182
- try {
183
- const headers = {};
184
- if (jwt) headers["Authorization"] = `Bearer ${jwt}`;
185
- await fetch(`${apiOrigin}/api/v1/session`, {
186
- method: "DELETE",
187
- credentials: jwt ? "omit" : "include",
188
- headers
189
- });
190
- } catch {
191
- }
182
+ const headers = {};
183
+ if (jwt) headers["Authorization"] = `Bearer ${jwt}`;
184
+ fetch(`${apiOrigin}/api/v1/session`, {
185
+ method: "DELETE",
186
+ credentials: jwt ? "omit" : "include",
187
+ headers
188
+ }).catch(() => {
189
+ });
192
190
  if (afterSignInUrl) window.location.href = afterSignInUrl;
193
191
  }, [apiOrigin, afterSignInUrl, clearAuth]);
192
+ const connectPlatform = useCallback((provider, options = {}) => {
193
+ const params = new URLSearchParams({ mode: "connect" });
194
+ params.set("redirect_url", options.redirectUrl ?? (typeof window !== "undefined" ? window.location.href : "/"));
195
+ params.set("pk", publishableKey);
196
+ if (options.scopes?.length) params.set("scope", options.scopes.join(" "));
197
+ if (options.configId) params.set("config_id", options.configId);
198
+ if (options.extraParams) Object.entries(options.extraParams).forEach(([k, v]) => params.set(k, v));
199
+ if (typeof window !== "undefined") {
200
+ window.location.href = `${apiOrigin}/api/v1/auth/oauth/${provider}/connect?${params}`;
201
+ }
202
+ }, [apiOrigin, publishableKey]);
194
203
  const value = {
195
204
  user,
196
205
  session,
197
206
  organization,
198
207
  isLoaded,
199
208
  isSignedIn: !!session,
200
- signOut
209
+ signOut,
210
+ connectPlatform
201
211
  };
202
212
  return /* @__PURE__ */ jsx(VaultixContext.Provider, { value, children: /* @__PURE__ */ jsx(
203
213
  "div",
@@ -1536,6 +1546,22 @@ function MiniSpinner() {
1536
1546
  /* @__PURE__ */ jsx6("path", { className: "opacity-75", fill: "currentColor", d: "M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" })
1537
1547
  ] });
1538
1548
  }
1549
+
1550
+ // src/index.ts
1551
+ var STORAGE_KEY2 = "vaultix_jwt";
1552
+ function getStoredToken() {
1553
+ try {
1554
+ return (typeof sessionStorage !== "undefined" ? sessionStorage : null)?.getItem(STORAGE_KEY2) ?? null;
1555
+ } catch {
1556
+ return null;
1557
+ }
1558
+ }
1559
+ function clearStoredToken() {
1560
+ try {
1561
+ if (typeof sessionStorage !== "undefined") sessionStorage.removeItem(STORAGE_KEY2);
1562
+ } catch {
1563
+ }
1564
+ }
1539
1565
  export {
1540
1566
  OrganizationSwitcher,
1541
1567
  RedirectToSignIn,
@@ -1546,6 +1572,8 @@ export {
1546
1572
  SignedOut,
1547
1573
  UserButton,
1548
1574
  VaultixProvider,
1575
+ clearStoredToken,
1576
+ getStoredToken,
1549
1577
  useAuth,
1550
1578
  useOrganization,
1551
1579
  useSession,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaultix.ai/react",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Vaultix-ID React SDK — drop-in auth components for any React app",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",