@nebulr-group/bridge-svelte 1.0.0-alpha.5 → 1.0.0-alpha.6

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
@@ -76,6 +76,8 @@ In your app's settings in Bridge, add your app's address as a browser address an
76
76
  | State | `session`, `user`, `workspace`, `workspaces`, `me`, `flags`, `billing`, `plans`, `usage`, `liveStatus`, `getBridge()` |
77
77
  | Server | `@nebulr-group/bridge-svelte/server`: `bridgeHandle`, `requireActor`, `meter`, `flagsFor` |
78
78
  | Hand-off | `HandoffSignIn`, `readHandoff`, `handoffSignIn`: a sign-in page for a command-line tool |
79
+ | Hosted mode | `hosted: {}` in the config: `/auth/sign-in` sends people to the page Bridge hosts for the app, and `/auth/callback` (`HostedCallback`) finishes the sign-in when they come back. Register the page's origin as a browser address and `https://<your app>/auth/callback` as a sign-in return |
80
+ | Several sign-in pages | `linkAddress` in the config: which of the app's registered email-links addresses its emails link to |
79
81
 
80
82
  All the guides: [the learning docs](https://github.com/thebridgedev/bridge-svelte/tree/v1/learning).
81
83
 
@@ -5,7 +5,8 @@
5
5
  <BridgeAuthRoutes />
6
6
 
7
7
  Serves sign-in, sign-up, verify-email, forgot-password, set-password,
8
- invite, workspaces, handoff, magic-link (a sign-in link's landing) and passkey-setup (a passkey setup link's landing). An unknown page gets the app's own 404.
8
+ invite, workspaces, handoff, magic-link (a sign-in link's landing), passkey-setup (a passkey setup link's landing)
9
+ and callback (hosted mode's return from Bridge's page). An unknown page gets the app's own 404.
9
10
 
10
11
  Customising, in rungs:
11
12
  1. `--bridge-*` CSS tokens restyle the forms.
@@ -36,6 +37,8 @@
36
37
  import HandoffSignIn from './HandoffSignIn.svelte';
37
38
  import MagicLinkLanding from './MagicLinkLanding.svelte';
38
39
  import PasskeySetupLanding from './PasskeySetupLanding.svelte';
40
+ import HostedSignIn from './HostedSignIn.svelte';
41
+ import HostedCallback from './HostedCallback.svelte';
39
42
 
40
43
  interface Props {
41
44
  /** Replaces everything around the form, on every page. */
@@ -61,8 +64,12 @@
61
64
  {@render heading?.(p)}
62
65
  {/snippet}
63
66
  {@const pageHeading = heading ? boundHeading : undefined}
64
- {#if p === 'sign-in'}
67
+ {#if p === 'sign-in' && config?.hosted}
68
+ <HostedSignIn headingSnippet={pageHeading} {messages} />
69
+ {:else if p === 'sign-in'}
65
70
  <SignInForm headingSnippet={pageHeading} {messages} />
71
+ {:else if p === 'callback'}
72
+ <HostedCallback headingSnippet={pageHeading} {messages} />
66
73
  {:else if p === 'sign-up'}
67
74
  <SignUpForm headingSnippet={pageHeading} {messages} />
68
75
  {:else if p === 'verify-email'}
@@ -0,0 +1,56 @@
1
+ <!--
2
+ Hosted mode (`hosted` in the config), `<auth base>/callback`: where Bridge's
3
+ hosted page sends the person back. It checks the return (its `state` and
4
+ issuer) and trades the code with the verifier this browser kept, then goes
5
+ on to where the person was going.
6
+ -->
7
+ <script lang="ts">
8
+ import type { Snippet } from 'svelte';
9
+ import { onMount } from 'svelte';
10
+ import { goto } from '$app/navigation';
11
+ import { takeReturnTo } from '@nebulr-group/bridge-auth-core';
12
+ import { getBridge, peekConfig } from '../../core/client.js';
13
+ import type { BridgeMessages } from '../../core/config.js';
14
+ import { errorMessage, translator } from '../../core/i18n.js';
15
+ import AuthFormWrapper from '../shared/AuthFormWrapper.svelte';
16
+ import Alert from '../shared/Alert.svelte';
17
+ import Spinner from '../shared/Spinner.svelte';
18
+
19
+ interface Props {
20
+ headingSnippet?: Snippet;
21
+ messages?: BridgeMessages;
22
+ }
23
+
24
+ let { headingSnippet, messages }: Props = $props();
25
+ const t = $derived(translator(messages));
26
+ const config = peekConfig();
27
+ const authBase = config?.authBase ?? '/auth';
28
+ let failure = $state<string | null>(null);
29
+
30
+ onMount(async () => {
31
+ const back = location.href;
32
+ // The code and state leave the address bar and the history at once.
33
+ history.replaceState(history.state, '', location.pathname);
34
+ try {
35
+ await getBridge().hosted.finish(back);
36
+ await goto(takeReturnTo() ?? config?.afterSignIn ?? '/', { invalidateAll: true, replaceState: true });
37
+ } catch (error) {
38
+ failure = errorMessage(error, t, 'link.error.invalid');
39
+ }
40
+ });
41
+ </script>
42
+
43
+ {#if failure === null}
44
+ <AuthFormWrapper heading={null} data-bridge-hosted="finishing">
45
+ <div class="bridge-auth-settling" data-bridge-auth-settling>
46
+ <Spinner size={24} />
47
+ </div>
48
+ </AuthFormWrapper>
49
+ {:else}
50
+ <AuthFormWrapper heading={t('login.heading')} {headingSnippet} data-bridge-hosted="failed">
51
+ <Alert variant="error">{failure}</Alert>
52
+ <div class="bridge-form-footer">
53
+ <a href={`${authBase}/sign-in`} class="bridge-link">{t('action.backToSignIn')}</a>
54
+ </div>
55
+ </AuthFormWrapper>
56
+ {/if}
@@ -0,0 +1,9 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { BridgeMessages } from '../../core/config.js';
3
+ interface Props {
4
+ headingSnippet?: Snippet;
5
+ messages?: BridgeMessages;
6
+ }
7
+ declare const HostedCallback: import("svelte").Component<Props, {}, "">;
8
+ type HostedCallback = ReturnType<typeof HostedCallback>;
9
+ export default HostedCallback;
@@ -0,0 +1,51 @@
1
+ <!--
2
+ Hosted mode (`hosted` in the config), the sign-in page: starts a sign-in
3
+ request at Bridge and sends the browser to the page Bridge hosts for the
4
+ app. `HostedCallback` finishes it when the person comes back.
5
+ -->
6
+ <script lang="ts">
7
+ import type { Snippet } from 'svelte';
8
+ import { onMount } from 'svelte';
9
+ import { page } from '$app/state';
10
+ import { readReturnTo, stashReturnTo } from '@nebulr-group/bridge-auth-core';
11
+ import { getBridge, peekConfig } from '../../core/client.js';
12
+ import type { BridgeMessages } from '../../core/config.js';
13
+ import { errorMessage, translator } from '../../core/i18n.js';
14
+ import AuthFormWrapper from '../shared/AuthFormWrapper.svelte';
15
+ import Alert from '../shared/Alert.svelte';
16
+ import Spinner from '../shared/Spinner.svelte';
17
+
18
+ interface Props {
19
+ headingSnippet?: Snippet;
20
+ messages?: BridgeMessages;
21
+ }
22
+
23
+ let { headingSnippet, messages }: Props = $props();
24
+ const t = $derived(translator(messages));
25
+ const config = peekConfig();
26
+ let failure = $state<string | null>(null);
27
+
28
+ onMount(async () => {
29
+ try {
30
+ const hosted = config?.hosted ?? {};
31
+ const redirectUri = hosted.redirectUri ?? new URL(`${config?.authBase ?? '/auth'}/callback`, location.origin).toString();
32
+ // Where to go once signed in survives the trip to Bridge's page, in this tab.
33
+ stashReturnTo(readReturnTo(page.url, config?.returnToParam));
34
+ location.assign(await getBridge().hosted.start({ redirectUri, ...(hosted.linkAddress === undefined ? {} : { linkAddress: hosted.linkAddress }) }));
35
+ } catch (error) {
36
+ failure = errorMessage(error, t, 'login.error.invalidCredentials');
37
+ }
38
+ });
39
+ </script>
40
+
41
+ {#if failure === null}
42
+ <AuthFormWrapper heading={null} data-bridge-hosted="starting">
43
+ <div class="bridge-auth-settling" data-bridge-auth-settling>
44
+ <Spinner size={24} />
45
+ </div>
46
+ </AuthFormWrapper>
47
+ {:else}
48
+ <AuthFormWrapper heading={t('login.heading')} {headingSnippet} data-bridge-hosted="failed">
49
+ <Alert variant="error">{failure}</Alert>
50
+ </AuthFormWrapper>
51
+ {/if}
@@ -0,0 +1,9 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { BridgeMessages } from '../../core/config.js';
3
+ interface Props {
4
+ headingSnippet?: Snippet;
5
+ messages?: BridgeMessages;
6
+ }
7
+ declare const HostedSignIn: import("svelte").Component<Props, {}, "">;
8
+ type HostedSignIn = ReturnType<typeof HostedSignIn>;
9
+ export default HostedSignIn;
@@ -21,7 +21,7 @@
21
21
  import Spinner from '../shared/Spinner.svelte';
22
22
  import WorkspaceChooser from './WorkspaceChooser.svelte';
23
23
  import { takeEmailLink } from './email-link.js';
24
- import { bounded, leave } from './finish-link.js';
24
+ import { bounded, handOff, leave } from './finish-link.js';
25
25
 
26
26
  interface Props extends HTMLAttributes<HTMLDivElement> {
27
27
  /** The link's token. @default read from the page's address */
@@ -64,7 +64,8 @@
64
64
  }
65
65
 
66
66
  async function follow(step: SignInStep) {
67
- if (step.done) await finish();
67
+ if (handOff(step)) view = { step: 'working' };
68
+ else if (step.done) await finish();
68
69
  else view = { step: 'workspace', workspaces: step.workspaces, choose: step.choose };
69
70
  }
70
71
 
@@ -16,6 +16,7 @@
16
16
  import type { Snippet } from 'svelte';
17
17
  import type { HTMLAttributes } from 'svelte/elements';
18
18
  import { onDestroy, onMount } from 'svelte';
19
+ import { handOff } from './finish-link.js';
19
20
  import { goto } from '$app/navigation';
20
21
  import { page } from '$app/state';
21
22
  import { isBridgeError, readReturnTo, type SignInOptions, type SignInStep, type WorkspaceChoice } from '@nebulr-group/bridge-auth-core';
@@ -172,7 +173,9 @@
172
173
  }
173
174
 
174
175
  async function follow(step: SignInStep) {
175
- if (step.done) {
176
+ if (handOff(step)) {
177
+ settled = true;
178
+ } else if (step.done) {
176
179
  finish();
177
180
  } else {
178
181
  choice = { workspaces: step.workspaces, choose: step.choose };
@@ -25,7 +25,7 @@
25
25
  import Spinner from '../shared/Spinner.svelte';
26
26
  import WorkspaceChooser from './WorkspaceChooser.svelte';
27
27
  import { takeEmailLink } from './email-link.js';
28
- import { bounded, leave } from './finish-link.js';
28
+ import { bounded, handOff, leave } from './finish-link.js';
29
29
 
30
30
  interface Props extends HTMLAttributes<HTMLDivElement> {
31
31
  /** The link's token. @default read from the page's address */
@@ -68,7 +68,8 @@
68
68
  }
69
69
 
70
70
  async function follow(step: SignInStep) {
71
- if (step.done) await finish();
71
+ if (handOff(step)) view = { step: 'working' };
72
+ else if (step.done) await finish();
72
73
  else view = { step: 'workspace', workspaces: step.workspaces, choose: step.choose };
73
74
  }
74
75
 
@@ -14,3 +14,12 @@ export declare function bounded<T>(work: Promise<T>, ms?: number): Promise<T>;
14
14
  * left; false after `tries`, and the page offers a link instead.
15
15
  */
16
16
  export declare function leave(go: () => Promise<unknown>, left: () => boolean, tries?: number): Promise<boolean>;
17
+ /**
18
+ * A sign-in finished for another app's sign-in request: the browser goes to
19
+ * the app's registered return with the code, and this page makes no session.
20
+ * True when it went.
21
+ */
22
+ export declare function handOff(step: {
23
+ readonly done: boolean;
24
+ readonly handoff?: string;
25
+ }, go?: (url: string) => void): boolean;
@@ -43,3 +43,14 @@ export async function leave(go, left, tries = LEAVE_TRIES) {
43
43
  }
44
44
  return false;
45
45
  }
46
+ /**
47
+ * A sign-in finished for another app's sign-in request: the browser goes to
48
+ * the app's registered return with the code, and this page makes no session.
49
+ * True when it went.
50
+ */
51
+ export function handOff(step, go = (url) => location.assign(url)) {
52
+ if (!step.done || step.handoff === undefined)
53
+ return false;
54
+ go(step.handoff);
55
+ return true;
56
+ }
@@ -62,8 +62,13 @@ export function startClient(config, extra) {
62
62
  // Memory: the refresh token and the visitor id stay in this page. A sign-up's verifier waits in localStorage
63
63
  // for its email link, which opens in a new tab, for the link's 15 minutes at most, and is removed once used.
64
64
  ...(config.storage === 'memory' ? { storage: memoryStorage(), signUpStorage: localStorageAdapter() } : {}),
65
+ ...(config.linkAddress === undefined ? {} : { linkAddress: config.linkAddress }),
65
66
  ...extra,
66
67
  });
68
+ // A page that signs in for other apps' sign-in requests takes the request from its address, before any form.
69
+ const request = config.signInRequests && typeof location !== 'undefined' ? new URLSearchParams(location.search).get('request') : null;
70
+ if (request !== null)
71
+ void client.request.use(request).catch(() => undefined);
67
72
  client.on('signed-out', ({ reason }) => {
68
73
  if (reason === 'session-ended' || reason === 'access-removed')
69
74
  lastEnded = reason;
@@ -114,10 +114,38 @@ export interface BridgeConfig {
114
114
  */
115
115
  readonly handoff?: {
116
116
  /** Return the code to the program's loopback address (`http://127.0.0.1:<port>`). */
117
- readonly loopback: true;
117
+ readonly loopback?: true;
118
+ /**
119
+ * Sign in for another app's sign-in request: opened with `?request=<id>`,
120
+ * the page signs in on the request and sends the browser to the app's
121
+ * registered return with the code. For a page that signs people in for
122
+ * other apps, such as Bridge's hosted page.
123
+ */
124
+ readonly requests?: true;
118
125
  /** The program's name, for the page's copy. @default 'the command-line tool' */
119
126
  readonly program?: string;
120
127
  };
128
+ /**
129
+ * Where the links in sign-up, sign-in link and recovery emails go: one of
130
+ * the app's registered email-links addresses, exactly, usually the address
131
+ * of these sign-in pages. Needed when the app has several sign-in pages.
132
+ * @default the app's first email-links address
133
+ */
134
+ readonly linkAddress?: string;
135
+ /**
136
+ * Hosted mode: sign in on the page Bridge hosts for the app instead of the
137
+ * app's own forms. The sign-in page sends the browser there, and
138
+ * `<auth base>/callback` finishes the sign-in when it comes back.
139
+ */
140
+ readonly hosted?: {
141
+ /**
142
+ * Where Bridge's page sends the person back: one of the app's registered
143
+ * sign-in returns, exactly. @default `<origin><auth base>/callback`
144
+ */
145
+ readonly redirectUri?: string;
146
+ /** Where an email link sent on the way goes. @default Bridge's hosted page */
147
+ readonly linkAddress?: string;
148
+ };
121
149
  }
122
150
  /** The settings with every default filled in. */
123
151
  export interface ResolvedConfig {
@@ -145,6 +173,13 @@ export interface ResolvedConfig {
145
173
  readonly handoff: {
146
174
  readonly program: string;
147
175
  } | null;
176
+ /** Whether the page signs in for other apps' sign-in requests (`?request=<id>`). */
177
+ readonly signInRequests: boolean;
178
+ readonly linkAddress: string | undefined;
179
+ readonly hosted: {
180
+ readonly redirectUri?: string;
181
+ readonly linkAddress?: string;
182
+ } | null;
148
183
  }
149
184
  /**
150
185
  * Checks the issuer before anything is sent: it must be https, except on
@@ -64,5 +64,8 @@ export function resolveConfig(config, envIssuer) {
64
64
  paywall: paywall === false ? null : (paywall ?? `${billingBase}/plan`),
65
65
  upgradeDialog: config.billing?.upgradeDialog ?? true,
66
66
  handoff: config.handoff?.loopback === true ? { program: config.handoff.program ?? 'the command-line tool' } : null,
67
+ signInRequests: config.handoff?.requests === true,
68
+ linkAddress: config.linkAddress,
69
+ hosted: config.hosted ?? null,
67
70
  };
68
71
  }
@@ -5,7 +5,7 @@
5
5
  * list, so no page an email links to can be forgotten.
6
6
  */
7
7
  import type { EmailLink } from '@nebulr-group/bridge-auth-core';
8
- export declare const AUTH_PAGES: readonly ["sign-in", "sign-up", "verify-email", "forgot-password", "set-password", "invite", "workspaces", "handoff", "magic-link", "passkey-setup"];
8
+ export declare const AUTH_PAGES: readonly ["sign-in", "sign-up", "verify-email", "forgot-password", "set-password", "invite", "workspaces", "handoff", "magic-link", "passkey-setup", "callback"];
9
9
  export type AuthPage = (typeof AUTH_PAGES)[number];
10
10
  export declare const BILLING_PAGES: readonly ["manage", "plan", "success", "error"];
11
11
  export type BillingPage = (typeof BILLING_PAGES)[number];
@@ -9,6 +9,7 @@ export const AUTH_PAGES = [
9
9
  'handoff',
10
10
  'magic-link',
11
11
  'passkey-setup',
12
+ 'callback',
12
13
  ];
13
14
  export const BILLING_PAGES = ['manage', 'plan', 'success', 'error'];
14
15
  /** The catch-all parameter's name: `[...bridge]`. */
package/dist/index.d.ts CHANGED
@@ -40,6 +40,8 @@ export { default as SetPassword } from './components/auth/SetPassword.svelte';
40
40
  export { default as InviteLanding } from './components/auth/InviteLanding.svelte';
41
41
  export { default as WorkspaceChooser } from './components/auth/WorkspaceChooser.svelte';
42
42
  export { default as HandoffSignIn } from './components/auth/HandoffSignIn.svelte';
43
+ export { default as HostedSignIn } from './components/auth/HostedSignIn.svelte';
44
+ export { default as HostedCallback } from './components/auth/HostedCallback.svelte';
43
45
  export { default as SignOutButton } from './components/auth/SignOutButton.svelte';
44
46
  export { default as ProfileName } from './components/auth/ProfileName.svelte';
45
47
  export { default as FeatureFlag } from './components/flags/FeatureFlag.svelte';
package/dist/index.js CHANGED
@@ -38,6 +38,8 @@ export { default as SetPassword } from './components/auth/SetPassword.svelte';
38
38
  export { default as InviteLanding } from './components/auth/InviteLanding.svelte';
39
39
  export { default as WorkspaceChooser } from './components/auth/WorkspaceChooser.svelte';
40
40
  export { default as HandoffSignIn } from './components/auth/HandoffSignIn.svelte';
41
+ export { default as HostedSignIn } from './components/auth/HostedSignIn.svelte';
42
+ export { default as HostedCallback } from './components/auth/HostedCallback.svelte';
41
43
  export { default as SignOutButton } from './components/auth/SignOutButton.svelte';
42
44
  export { default as ProfileName } from './components/auth/ProfileName.svelte';
43
45
  export { default as FeatureFlag } from './components/flags/FeatureFlag.svelte';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nebulr-group/bridge-svelte",
3
- "version": "1.0.0-alpha.5",
3
+ "version": "1.0.0-alpha.6",
4
4
  "description": "The Bridge for SvelteKit: sign-in in your own pages, workspaces, flag-based route guards, plans and checkout, the team panel and live updates, on one setting: the issuer.",
5
5
  "author": "Iman Pouya",
6
6
  "license": "MIT",
@@ -60,7 +60,7 @@
60
60
  "@sveltejs/kit": "^2.0.0"
61
61
  },
62
62
  "dependencies": {
63
- "@nebulr-group/bridge-auth-core": "1.0.0-alpha.5"
63
+ "@nebulr-group/bridge-auth-core": "1.0.0-alpha.6"
64
64
  },
65
65
  "devDependencies": {
66
66
  "@sveltejs/kit": "^2.16.0",