@nebulr-group/bridge-svelte 0.2.0 → 0.3.0-beta.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.
Files changed (84) hide show
  1. package/dist/auth/route-guard.d.ts +2 -32
  2. package/dist/auth/route-guard.js +16 -96
  3. package/dist/client/BridgeBootstrap.d.ts +6 -3
  4. package/dist/client/BridgeBootstrap.js +35 -30
  5. package/dist/client/BridgeBootstrap.svelte +14 -12
  6. package/dist/client/components/ProfileName.svelte +18 -0
  7. package/dist/client/components/ProfileName.svelte.d.ts +6 -0
  8. package/dist/client/components/developer/ApiTokenManagement.svelte +365 -0
  9. package/dist/client/components/developer/ApiTokenManagement.svelte.d.ts +6 -0
  10. package/dist/client/components/sdk-auth/ForgotPassword.svelte +170 -0
  11. package/dist/client/components/sdk-auth/ForgotPassword.svelte.d.ts +10 -0
  12. package/dist/client/components/sdk-auth/LoginForm.svelte +414 -0
  13. package/dist/client/components/sdk-auth/LoginForm.svelte.d.ts +25 -0
  14. package/dist/client/components/sdk-auth/MagicLink.svelte +92 -0
  15. package/dist/client/components/sdk-auth/MagicLink.svelte.d.ts +9 -0
  16. package/dist/client/components/sdk-auth/MfaChallenge.svelte +150 -0
  17. package/dist/client/components/sdk-auth/MfaChallenge.svelte.d.ts +9 -0
  18. package/dist/client/components/sdk-auth/MfaSetup.svelte +182 -0
  19. package/dist/client/components/sdk-auth/MfaSetup.svelte.d.ts +8 -0
  20. package/dist/client/components/sdk-auth/PasskeyLogin.svelte +99 -0
  21. package/dist/client/components/sdk-auth/PasskeyLogin.svelte.d.ts +10 -0
  22. package/dist/client/components/sdk-auth/PasskeyRequestSetupLink.svelte +83 -0
  23. package/dist/client/components/sdk-auth/PasskeyRequestSetupLink.svelte.d.ts +7 -0
  24. package/dist/client/components/sdk-auth/PasskeySetup.svelte +151 -0
  25. package/dist/client/components/sdk-auth/PasskeySetup.svelte.d.ts +11 -0
  26. package/dist/client/components/sdk-auth/SignupForm.svelte +119 -0
  27. package/dist/client/components/sdk-auth/SignupForm.svelte.d.ts +14 -0
  28. package/dist/client/components/sdk-auth/SsoButton.svelte +70 -0
  29. package/dist/client/components/sdk-auth/SsoButton.svelte.d.ts +13 -0
  30. package/dist/client/components/sdk-auth/SsoProviderIcon.svelte +56 -0
  31. package/dist/client/components/sdk-auth/SsoProviderIcon.svelte.d.ts +7 -0
  32. package/dist/client/components/sdk-auth/TenantSelector.svelte +83 -0
  33. package/dist/client/components/sdk-auth/TenantSelector.svelte.d.ts +11 -0
  34. package/dist/client/components/sdk-auth/WorkspaceSelector.svelte +109 -0
  35. package/dist/client/components/sdk-auth/WorkspaceSelector.svelte.d.ts +16 -0
  36. package/dist/client/components/sdk-auth/shared/Alert.svelte +17 -0
  37. package/dist/client/components/sdk-auth/shared/Alert.svelte.d.ts +9 -0
  38. package/dist/client/components/sdk-auth/shared/AuthFormWrapper.svelte +23 -0
  39. package/dist/client/components/sdk-auth/shared/AuthFormWrapper.svelte.d.ts +10 -0
  40. package/dist/client/components/sdk-auth/shared/Spinner.svelte +16 -0
  41. package/dist/client/components/sdk-auth/shared/Spinner.svelte.d.ts +7 -0
  42. package/dist/client/components/subscription/PlanSelector.svelte +204 -0
  43. package/dist/client/components/subscription/PlanSelector.svelte.d.ts +22 -0
  44. package/dist/client/components/team/TeamAddUserDialog.svelte +125 -0
  45. package/dist/client/components/team/TeamAddUserDialog.svelte.d.ts +17 -0
  46. package/dist/client/components/team/TeamConfirmDialog.svelte +80 -0
  47. package/dist/client/components/team/TeamConfirmDialog.svelte.d.ts +21 -0
  48. package/dist/client/components/team/TeamEditUserDialog.svelte +131 -0
  49. package/dist/client/components/team/TeamEditUserDialog.svelte.d.ts +19 -0
  50. package/dist/client/components/team/TeamManagementPanel.svelte +72 -0
  51. package/dist/client/components/team/TeamManagementPanel.svelte.d.ts +20 -0
  52. package/dist/client/components/team/TeamProfileForm.svelte +129 -0
  53. package/dist/client/components/team/TeamProfileForm.svelte.d.ts +7 -0
  54. package/dist/client/components/team/TeamUserActionsMenu.svelte +63 -0
  55. package/dist/client/components/team/TeamUserActionsMenu.svelte.d.ts +9 -0
  56. package/dist/client/components/team/TeamUserList.svelte +226 -0
  57. package/dist/client/components/team/TeamUserList.svelte.d.ts +7 -0
  58. package/dist/client/components/team/TeamWorkspaceForm.svelte +134 -0
  59. package/dist/client/components/team/TeamWorkspaceForm.svelte.d.ts +7 -0
  60. package/dist/client/stores/config.store.js +8 -5
  61. package/dist/client/utils/cn.d.ts +1 -0
  62. package/dist/client/utils/cn.js +3 -0
  63. package/dist/core/bridge-instance.d.ts +52 -0
  64. package/dist/core/bridge-instance.js +160 -0
  65. package/dist/index.d.ts +26 -6
  66. package/dist/index.js +26 -7
  67. package/dist/shared/feature-flag.js +6 -60
  68. package/dist/shared/logger.d.ts +1 -0
  69. package/dist/shared/logger.js +7 -13
  70. package/dist/shared/profile.d.ts +13 -42
  71. package/dist/shared/profile.js +19 -95
  72. package/dist/shared/types/config.d.ts +8 -13
  73. package/dist/styles.css +1089 -0
  74. package/package.json +60 -37
  75. package/dist/client/components/auth/Login.svelte +0 -20
  76. package/dist/client/components/auth/Login.svelte.d.ts +0 -18
  77. package/dist/client/components/team/TeamManagement.svelte +0 -139
  78. package/dist/client/components/team/TeamManagement.svelte.d.ts +0 -3
  79. package/dist/client/stores/profile.store.d.ts +0 -10
  80. package/dist/client/stores/profile.store.js +0 -29
  81. package/dist/shared/services/auth.service.d.ts +0 -24
  82. package/dist/shared/services/auth.service.js +0 -241
  83. package/dist/shared/services/plan.service.d.ts +0 -7
  84. package/dist/shared/services/plan.service.js +0 -90
@@ -1,32 +1,2 @@
1
- export type PublicRoutePattern = string | RegExp;
2
- export type FlagRequirement = string | {
3
- any: string[];
4
- } | {
5
- all: string[];
6
- };
7
- export type RouteRule = {
8
- match: string | RegExp;
9
- public?: boolean;
10
- featureFlag?: FlagRequirement;
11
- redirectTo?: string;
12
- };
13
- export interface RouteGuardConfig {
14
- rules: RouteRule[];
15
- defaultAccess?: 'public' | 'protected';
16
- }
17
- export declare function createRouteGuard(): {
18
- isPublicRoute: (pathname: string) => boolean;
19
- isProtectedRoute: (pathname: string) => boolean;
20
- shouldRedirectToLogin: (pathname: string) => boolean;
21
- checkRouteRestrictions: (pathname: string) => Promise<string | null>;
22
- getLoginRedirect: () => string;
23
- getNavigationDecision(pathname: string): Promise<{
24
- type: "allow";
25
- } | {
26
- type: "login";
27
- loginUrl: string;
28
- } | {
29
- type: "redirect";
30
- to: string;
31
- }>;
32
- };
1
+ export type { FlagRequirement, NavigationDecision, RouteGuard, RouteGuardConfig, RouteRule } from '@nebulr-group/bridge-auth-core';
2
+ export declare function createRouteGuard(flagsReady?: Promise<void>): import("@nebulr-group/bridge-auth-core").RouteGuard;
@@ -1,104 +1,24 @@
1
- import { get } from 'svelte/store';
1
+ // src/lib/auth/route-guard.ts thin wrapper delegating to auth-core via bridge-instance
2
+ import { getBridgeAuth } from '../core/bridge-instance.js';
2
3
  import { getRouteGuardConfig } from '../client/stores/config.store.js';
3
- import { isFeatureEnabled } from '../shared/feature-flag.js';
4
- import { logger } from '../shared/logger.js';
5
- import { auth } from '../shared/services/auth.service.js';
6
- const { isAuthenticated, createLoginUrl } = auth;
7
- function escapeRegex(text) {
8
- return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
9
- }
10
- function toRegExp(pattern) {
11
- if (pattern instanceof RegExp)
12
- return pattern;
13
- // Support simple wildcard '*'
14
- const hasWildcard = pattern.includes('*');
15
- if (!hasWildcard) {
16
- return new RegExp(`^${escapeRegex(pattern)}$`);
17
- }
18
- const escaped = escapeRegex(pattern).replace(/\\\*/g, '.*');
19
- return new RegExp(`^${escaped}$`);
20
- }
21
- function findMatchingRule(pathname, config) {
22
- for (const rule of config.rules) {
23
- if (toRegExp(rule.match).test(pathname)) {
24
- return rule;
25
- }
26
- }
27
- return null;
28
- }
29
- async function evaluateFlagRequirement(req) {
30
- if (typeof req === 'string') {
31
- return isFeatureEnabled(req);
32
- }
33
- if ('any' in req) {
34
- const results = await Promise.all(req.any.map((f) => isFeatureEnabled(f)));
35
- return results.some(Boolean);
36
- }
37
- if ('all' in req) {
38
- const results = await Promise.all(req.all.map((f) => isFeatureEnabled(f)));
39
- return results.every(Boolean);
40
- }
41
- return true;
42
- }
43
- export function createRouteGuard() {
4
+ export function createRouteGuard(flagsReady) {
44
5
  const config = getRouteGuardConfig();
45
- function isPublicRoute(pathname) {
46
- const rule = findMatchingRule(pathname, config);
47
- if (rule) {
48
- logger.debug(`[route-guard] path ${pathname} is ${rule.public ? 'public' : 'protected'} by bridge rule ${rule.match}`);
49
- return !!rule.public;
50
- }
51
- logger.debug(`[route-guard] path ${pathname} is ${config.defaultAccess === 'public' ? 'public' : 'protected'} by bridge default access ${config.defaultAccess}`);
52
- const isPublicByDefault = (config.defaultAccess ?? 'protected') === 'public';
53
- return isPublicByDefault;
54
- }
55
- function isProtectedRoute(pathname) {
56
- return !isPublicRoute(pathname);
57
- }
58
- function shouldRedirectToLogin(pathname) {
59
- const isProtected = isProtectedRoute(pathname);
60
- const tokens = auth.getToken();
61
- const authenticated = get(isAuthenticated);
62
- logger.debug(`[route-guard] shouldRedirectToLogin check`, {
63
- pathname,
64
- isProtected,
65
- hasTokens: !!tokens?.accessToken,
66
- authenticated,
67
- tokenStoreValue: tokens
68
- });
69
- if (isProtectedRoute(pathname) && !get(isAuthenticated)) {
70
- logger.debug(`[route-guard] path ${pathname} is protected and user is not authenticated`);
71
- return true;
72
- }
73
- logger.debug(`[route-guard] path ${pathname} is ${isProtected ? 'protected' : 'public'} and user ${authenticated ? 'authenticated' : 'not authenticated'}`);
74
- return false;
75
- }
76
- async function checkRouteRestrictions(pathname) {
77
- const rule = findMatchingRule(pathname, config);
78
- if (!rule)
79
- return null;
80
- if (rule.featureFlag) {
81
- const ok = await evaluateFlagRequirement(rule.featureFlag);
82
- logger.debug(`[route-guard] path ${pathname} is restricted by bridge feature flag ${rule.featureFlag} and flag requirment evaluated to ${ok}`);
83
- if (!ok)
84
- return rule.redirectTo ?? '/';
85
- }
86
- return null;
87
- }
88
- function getLoginRedirect() {
89
- return createLoginUrl();
90
- }
6
+ const guard = getBridgeAuth().createRouteGuard(config);
7
+ if (!flagsReady)
8
+ return guard;
9
+ // Wrap checkRouteRestrictions to await flagsReady before evaluating
91
10
  return {
92
- isPublicRoute,
93
- isProtectedRoute,
94
- shouldRedirectToLogin,
95
- checkRouteRestrictions,
96
- getLoginRedirect,
11
+ ...guard,
12
+ async checkRouteRestrictions(pathname) {
13
+ await flagsReady;
14
+ return guard.checkRouteRestrictions(pathname);
15
+ },
97
16
  async getNavigationDecision(pathname) {
98
- if (shouldRedirectToLogin(pathname)) {
99
- return { type: 'login', loginUrl: getLoginRedirect() };
17
+ if (guard.shouldRedirectToLogin(pathname)) {
18
+ return { type: 'login', loginUrl: guard.getLoginRedirect() };
100
19
  }
101
- const redirectTo = await checkRouteRestrictions(pathname);
20
+ await flagsReady;
21
+ const redirectTo = await guard.checkRouteRestrictions(pathname);
102
22
  if (redirectTo) {
103
23
  return { type: 'redirect', to: redirectTo };
104
24
  }
@@ -1,5 +1,8 @@
1
1
  import type { RouteGuardConfig } from '../auth/route-guard.js';
2
+ import { waitForBridge as _waitForBridge } from '../core/bridge-instance.js';
2
3
  import type { BridgeConfig } from '../shared/types/config.js';
3
- export declare function bridgeBootstrap(url: URL, config: BridgeConfig | string, routeConfig?: RouteGuardConfig): Promise<void>;
4
- export declare const bridgeReady: import("svelte/store").Writable<boolean>;
5
- export declare function waitForBridge(): Promise<void>;
4
+ export declare function bridgeBootstrap(url: URL, config: BridgeConfig | string, routeConfig?: RouteGuardConfig): Promise<{
5
+ flagsReady: Promise<void>;
6
+ } | undefined>;
7
+ export declare const bridgeReady: import("svelte/store").Readable<boolean>;
8
+ export { _waitForBridge as waitForBridge };
@@ -1,44 +1,34 @@
1
1
  // src/lib/bridge/bootstrap.ts
2
2
  import { isRedirect, redirect } from '@sveltejs/kit';
3
- import { get, writable } from 'svelte/store';
3
+ import { get } from 'svelte/store';
4
4
  import { createRouteGuard } from '../auth/route-guard.js';
5
+ import { getBridgeAuth, bridgeReadyStore, markReady, waitForBridge as _waitForBridge, } from '../core/bridge-instance.js';
5
6
  import { featureFlags } from '../shared/feature-flag.js';
6
7
  import { logger } from '../shared/logger.js';
7
- import { auth, maybeRefreshNow } from '../shared/services/auth.service.js';
8
- import { bridgeConfig } from './stores/config.store.js';
9
- const bridgeReadyStore = writable(false);
10
- let resolveReady = null;
11
- const bridgeReadyPromise = new Promise((resolve) => {
12
- resolveReady = resolve;
13
- });
14
- function markBridgeReady() {
15
- if (get(bridgeReadyStore))
16
- return;
17
- bridgeReadyStore.set(true);
18
- resolveReady?.();
19
- }
8
+ import { bridgeConfig, getConfig } from './stores/config.store.js';
20
9
  export async function bridgeBootstrap(url, config, routeConfig = { rules: [], defaultAccess: 'protected' }) {
21
10
  // If we've already completed bootstrap once, short-circuit
22
11
  if (get(bridgeReadyStore)) {
23
12
  return;
24
13
  }
25
14
  const finalConfig = typeof config === 'string' ? { appId: config } : config;
26
- // 1. Initialize configuration (synchronously)
15
+ // 1. Initialize configuration (synchronously) — this also calls initBridge()
27
16
  bridgeConfig.initConfig(finalConfig, routeConfig);
28
17
  // 1a. If we're on bridge OAuth callback route, let bridge dedicated route handle it
29
18
  try {
30
- const callbackPath = finalConfig.callbackUrl ? new URL(finalConfig.callbackUrl).pathname : null;
19
+ const resolvedCallbackUrl = getConfig().callbackUrl;
20
+ const callbackPath = resolvedCallbackUrl ? new URL(resolvedCallbackUrl).pathname : null;
31
21
  if (callbackPath && url.pathname === callbackPath) {
32
22
  logger.debug('[bridgeBootstrap] callback route detected, skipping bootstrap flow');
33
- const { handleCallback } = auth;
23
+ const bridge = getBridgeAuth();
34
24
  const code = url.searchParams.get('code');
35
25
  logger.debug('[bridgeBootstrap] callback code check', { hasCode: !!code, pathname: url.pathname });
36
26
  if (code) {
37
27
  try {
38
28
  logger.debug('[bridgeBootstrap] calling handleCallback');
39
- await handleCallback(code);
29
+ await bridge.handleCallback(code);
40
30
  // Verify tokens before redirect
41
- const tokensAfterCallback = auth.getToken();
31
+ const tokensAfterCallback = bridge.getTokens();
42
32
  const isAuthAfterCallback = !!tokensAfterCallback?.accessToken;
43
33
  logger.debug('[bridgeBootstrap] after handleCallback', {
44
34
  hasTokens: !!tokensAfterCallback?.accessToken,
@@ -70,13 +60,26 @@ export async function bridgeBootstrap(url, config, routeConfig = { rules: [], de
70
60
  logger.warn('[bridgeBootstrap] failed parsing callbackUrl', e);
71
61
  }
72
62
  // 2. Ensure tokens are fresh if needed
73
- await maybeRefreshNow();
74
- // 3. Load feature flags before guard if your guard depends on bridgem
75
- await featureFlags.refresh();
63
+ try {
64
+ const bridge = getBridgeAuth();
65
+ if (bridge.isAuthenticated()) {
66
+ await bridge.refreshTokens();
67
+ }
68
+ }
69
+ catch {
70
+ // Non-fatal — stale tokens will be caught by route guard
71
+ }
72
+ // 3. Fire flag fetch without awaiting so GQL queries can start in parallel.
73
+ // The flagsReady promise is passed to the guard and returned to callers
74
+ // so routes that need flags can still await before rendering.
75
+ // Swallow errors — feature flag failures should never crash the bootstrap.
76
+ const flagsReady = featureFlags.refresh().catch((err) => {
77
+ logger.warn('[bridgeBootstrap] Feature flags failed to load:', err);
78
+ });
76
79
  // 4. Handle route guarding and redirects
77
- const guard = createRouteGuard();
78
- // Check auth state before route guard decision
79
- const currentTokens = auth.getToken();
80
+ const guard = createRouteGuard(flagsReady);
81
+ const bridge = getBridgeAuth();
82
+ const currentTokens = bridge.getTokens();
80
83
  const currentAuth = !!currentTokens?.accessToken;
81
84
  logger.debug('[bridgeBootstrap] before route guard check', {
82
85
  pathname: url.pathname,
@@ -86,15 +89,17 @@ export async function bridgeBootstrap(url, config, routeConfig = { rules: [], de
86
89
  const decision = await guard.getNavigationDecision(url.pathname);
87
90
  logger.debug('[bridgeBootstrap] navigation decision', decision);
88
91
  if (decision.type === 'login') {
89
- redirect(303, auth.createLoginUrl());
92
+ const { loginRoute } = getConfig();
93
+ // SDK mode: consumer explicitly set loginRoute → redirect to in-app login view
94
+ // Hosted mode (default): no loginRoute → redirect to hosted auth portal
95
+ redirect(303, loginRoute ?? bridge.createLoginUrl());
90
96
  }
91
97
  if (decision.type === 'redirect' && url.pathname !== decision.to) {
92
98
  redirect(303, decision.to);
93
99
  }
94
100
  logger.debug('[bridgeBootstrap] in bridge end');
95
- markBridgeReady();
101
+ markReady();
102
+ return { flagsReady };
96
103
  }
97
104
  export const bridgeReady = bridgeReadyStore;
98
- export function waitForBridge() {
99
- return bridgeReadyPromise;
100
- }
105
+ export { _waitForBridge as waitForBridge };
@@ -1,23 +1,27 @@
1
- <script lang="ts">
2
- import { beforeNavigate } from '$app/navigation';
1
+ <script lang="ts">
2
+ import { beforeNavigate, goto } from '$app/navigation';
3
3
  import { onMount } from 'svelte';
4
4
  import { createRouteGuard } from '../auth/route-guard.js';
5
- import { auth, startAutoRefresh } from '../shared/services/auth.service.js';
5
+ import { getBridgeAuth } from '../core/bridge-instance.js';
6
+ import { getConfig } from './stores/config.store.js';
6
7
 
7
8
  // Props: require config to be passed by consumer, now including onBootstrapComplete
8
9
  let {onBootstrapComplete }: {
9
-
10
10
  onBootstrapComplete?: () => void
11
11
  } = $props();
12
12
 
13
- const { login } = auth;
14
13
  const guard = createRouteGuard();
15
14
 
16
15
  async function handleRoute(pathname: string, cancel?: () => void) {
17
- const decision = await guard.getNavigationDecision(pathname);
16
+ const decision = await guard.getNavigationDecision(pathname);
18
17
  if (decision.type === 'login') {
19
18
  if (cancel) cancel();
20
- login();
19
+ const { loginRoute } = getConfig();
20
+ if (loginRoute) {
21
+ goto(loginRoute);
22
+ } else {
23
+ getBridgeAuth().login();
24
+ }
21
25
  return;
22
26
  }
23
27
  if (decision.type === 'redirect' && window.location.pathname !== decision.to) {
@@ -27,10 +31,8 @@
27
31
  }
28
32
  }
29
33
 
30
- onMount(async () => {
31
- startAutoRefresh();
32
-
33
- // Signal completion
34
+ onMount(async () => {
35
+ // Auth-core manages auto-refresh internally — no startAutoRefresh() needed
34
36
  if (onBootstrapComplete) onBootstrapComplete();
35
37
  });
36
38
 
@@ -38,4 +40,4 @@
38
40
  if (!to) return;
39
41
  await handleRoute(to.url.pathname, cancel);
40
42
  });
41
- </script>
43
+ </script>
@@ -0,0 +1,18 @@
1
+ <script lang="ts">
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ import { profileStore } from '../../shared/profile.js';
4
+
5
+ interface Props extends HTMLAttributes<HTMLSpanElement> {}
6
+
7
+ let { class: className, style, ...rest }: Props = $props();
8
+
9
+ const { profile } = profileStore;
10
+
11
+ let displayName = $derived(
12
+ $profile?.fullName || $profile?.email || ''
13
+ );
14
+ </script>
15
+
16
+ {#if displayName}
17
+ <span class={className} {style} data-bridge-profile-name {...rest}>{displayName}</span>
18
+ {/if}
@@ -0,0 +1,6 @@
1
+ import type { HTMLAttributes } from 'svelte/elements';
2
+ interface Props extends HTMLAttributes<HTMLSpanElement> {
3
+ }
4
+ declare const ProfileName: import("svelte").Component<Props, {}, "">;
5
+ type ProfileName = ReturnType<typeof ProfileName>;
6
+ export default ProfileName;