@atlasauth/svelte 0.1.4 → 0.2.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.
- package/README.md +21 -21
- package/dist/accessors.d.ts +5 -0
- package/dist/accessors.js +3 -1
- package/dist/appearance.d.ts +27 -2
- package/dist/appearance.js +35 -0
- package/dist/components/AtlasProvider.svelte +21 -4
- package/dist/context.d.ts +6 -1
- package/dist/context.js +11 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @atlasauth/svelte
|
|
2
2
|
|
|
3
3
|
The official **Svelte / SvelteKit** SDK for Atlas — the Svelte peer of
|
|
4
|
-
[`@
|
|
4
|
+
[`@atlasauth/react`](../react). Same surface, expressed the Svelte way: a client
|
|
5
5
|
installed through **context**, reactive auth state exposed as **stores**, and
|
|
6
6
|
`.svelte` **components** for the common flows.
|
|
7
7
|
|
|
8
|
-
It wraps [`@
|
|
8
|
+
It wraps [`@atlasauth/js`](../js), the framework-agnostic browser client, exactly as
|
|
9
9
|
the React SDK does — so both frameworks drive the same FAPI endpoints through one
|
|
10
10
|
implementation and cannot diverge in what the server sees.
|
|
11
11
|
|
|
@@ -21,7 +21,7 @@ implementation and cannot diverge in what the server sees.
|
|
|
21
21
|
## Install
|
|
22
22
|
|
|
23
23
|
```bash
|
|
24
|
-
pnpm add @
|
|
24
|
+
pnpm add @atlasauth/svelte
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
Peer dependencies: `svelte >= 4`. `@sveltejs/kit` is an **optional** peer, needed
|
|
@@ -38,7 +38,7 @@ component which also paints the appearance tokens onto a host-DOM wrapper.
|
|
|
38
38
|
```svelte
|
|
39
39
|
<!-- src/routes/+layout.svelte -->
|
|
40
40
|
<script lang="ts">
|
|
41
|
-
import { AtlasProvider } from '@
|
|
41
|
+
import { AtlasProvider } from '@atlasauth/svelte';
|
|
42
42
|
</script>
|
|
43
43
|
|
|
44
44
|
<AtlasProvider publishableKey="pk_live_…" frontendApi="https://your-instance-fapi.atlasauth.net">
|
|
@@ -50,7 +50,7 @@ Or imperatively, from any component's `<script>`:
|
|
|
50
50
|
|
|
51
51
|
```svelte
|
|
52
52
|
<script lang="ts">
|
|
53
|
-
import { initAtlas } from '@
|
|
53
|
+
import { initAtlas } from '@atlasauth/svelte';
|
|
54
54
|
const atlas = initAtlas({ publishableKey: 'pk_live_…' });
|
|
55
55
|
</script>
|
|
56
56
|
```
|
|
@@ -73,7 +73,7 @@ live. They mirror the React hooks one-to-one:
|
|
|
73
73
|
|
|
74
74
|
```svelte
|
|
75
75
|
<script lang="ts">
|
|
76
|
-
import { getUser, getAuth } from '@
|
|
76
|
+
import { getUser, getAuth } from '@atlasauth/svelte';
|
|
77
77
|
const user = getUser();
|
|
78
78
|
const auth = getAuth();
|
|
79
79
|
|
|
@@ -97,7 +97,7 @@ The raw stores are also on the client: `useAtlas().user`, `.session`,
|
|
|
97
97
|
|
|
98
98
|
### 3. Components
|
|
99
99
|
|
|
100
|
-
Mirror `@
|
|
100
|
+
Mirror `@atlasauth/react`'s `components.tsx`:
|
|
101
101
|
|
|
102
102
|
| Component | Purpose |
|
|
103
103
|
| ----------------------- | ------------------------------------------------------------- |
|
|
@@ -114,7 +114,7 @@ Mirror `@atlas/react`'s `components.tsx`:
|
|
|
114
114
|
|
|
115
115
|
```svelte
|
|
116
116
|
<script lang="ts">
|
|
117
|
-
import { SignedIn, SignedOut, SignIn, UserButton, Protect } from '@
|
|
117
|
+
import { SignedIn, SignedOut, SignIn, UserButton, Protect } from '@atlasauth/svelte';
|
|
118
118
|
</script>
|
|
119
119
|
|
|
120
120
|
<SignedOut><SignIn /></SignedOut>
|
|
@@ -132,7 +132,7 @@ Mirror `@atlas/react`'s `components.tsx`:
|
|
|
132
132
|
|
|
133
133
|
### Theming & i18n
|
|
134
134
|
|
|
135
|
-
Same as `@
|
|
135
|
+
Same as `@atlasauth/react`: pass `appearance` (design tokens + per-element class
|
|
136
136
|
overrides — components render in the host DOM, no iframe, so Tailwind/CSS just
|
|
137
137
|
work) and `localization` (a flat i18n catalog, merged over `en-US`) to
|
|
138
138
|
`initAtlas` / `<AtlasProvider>`.
|
|
@@ -148,15 +148,15 @@ work) and `localization` (a flat i18n catalog, merged over `en-US`) to
|
|
|
148
148
|
|
|
149
149
|
## SvelteKit SSR
|
|
150
150
|
|
|
151
|
-
The server helper lives at the separate `@
|
|
152
|
-
on `@
|
|
151
|
+
The server helper lives at the separate `@atlasauth/svelte/server` entry (it depends
|
|
152
|
+
on `@atlasauth/backend` and must never enter a browser bundle). It verifies the
|
|
153
153
|
session JWT **locally at the edge** — the JWKS is cached in-process, so a warm
|
|
154
154
|
server verifies with no network round-trip.
|
|
155
155
|
|
|
156
156
|
### `hooks.server.ts`
|
|
157
157
|
|
|
158
158
|
```ts
|
|
159
|
-
import { handleAtlas } from '@
|
|
159
|
+
import { handleAtlas } from '@atlasauth/svelte/server';
|
|
160
160
|
|
|
161
161
|
export const handle = handleAtlas({
|
|
162
162
|
jwksUrl: 'https://your-instance-fapi.atlasauth.net/.well-known/jwks.json',
|
|
@@ -172,7 +172,7 @@ This resolves the session once per request and stashes it on
|
|
|
172
172
|
|
|
173
173
|
```ts
|
|
174
174
|
// +layout.server.ts
|
|
175
|
-
import { getServerAuth } from '@
|
|
175
|
+
import { getServerAuth } from '@atlasauth/svelte/server';
|
|
176
176
|
|
|
177
177
|
export const load = (event) => {
|
|
178
178
|
const auth = getServerAuth(event);
|
|
@@ -182,7 +182,7 @@ export const load = (event) => {
|
|
|
182
182
|
|
|
183
183
|
```ts
|
|
184
184
|
// a protected +page.server.ts
|
|
185
|
-
import { getServerAuth } from '@
|
|
185
|
+
import { getServerAuth } from '@atlasauth/svelte/server';
|
|
186
186
|
import { error } from '@sveltejs/kit';
|
|
187
187
|
|
|
188
188
|
export const load = (event) => {
|
|
@@ -198,13 +198,13 @@ export const load = (event) => {
|
|
|
198
198
|
|
|
199
199
|
`getServerAuth(event).has(condition)` answers a condition without throwing;
|
|
200
200
|
`.protect(condition)` asserts it (throwing `ForbiddenError`). Both use the shared
|
|
201
|
-
`@
|
|
201
|
+
`@atlasauth/authz` primitive — the same one `<Protect>` evaluates in the browser — so
|
|
202
202
|
a check reads identically on the client, here, and in the React/Next SDKs.
|
|
203
203
|
|
|
204
204
|
For strong typing of `event.locals.auth`, add to `src/app.d.ts`:
|
|
205
205
|
|
|
206
206
|
```ts
|
|
207
|
-
import type { ServerAuth } from '@
|
|
207
|
+
import type { ServerAuth } from '@atlasauth/svelte/server';
|
|
208
208
|
declare global {
|
|
209
209
|
namespace App {
|
|
210
210
|
interface Locals {
|
|
@@ -218,13 +218,13 @@ export {};
|
|
|
218
218
|
## Development
|
|
219
219
|
|
|
220
220
|
```bash
|
|
221
|
-
pnpm --filter @
|
|
222
|
-
pnpm --filter @
|
|
223
|
-
pnpm --filter @
|
|
221
|
+
pnpm --filter @atlasauth/svelte build # svelte-package → dist/
|
|
222
|
+
pnpm --filter @atlasauth/svelte typecheck # svelte-check
|
|
223
|
+
pnpm --filter @atlasauth/svelte test # vitest (jsdom + @testing-library/svelte)
|
|
224
224
|
```
|
|
225
225
|
|
|
226
226
|
Tests cover the context init, the reactive stores, and `<SignedIn>`/`<SignedOut>`
|
|
227
|
-
(with `@
|
|
227
|
+
(with `@atlasauth/js` mocked).
|
|
228
228
|
|
|
229
229
|
## License
|
|
230
230
|
|
package/dist/accessors.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type Readable } from 'svelte/store';
|
|
2
2
|
import { type FlowState, type ProviderToken } from '@atlasauth/js';
|
|
3
|
+
import { type AuthStatus } from './context';
|
|
3
4
|
import { type ProtectCondition } from './protect';
|
|
4
5
|
import type { AtlasOrganizationMembership, AtlasSession, AtlasUser } from './types';
|
|
5
6
|
/**
|
|
@@ -35,6 +36,10 @@ export declare function getSession(): Readable<UseSession>;
|
|
|
35
36
|
export interface UseAuth {
|
|
36
37
|
isLoaded: boolean;
|
|
37
38
|
isSignedIn: boolean;
|
|
39
|
+
/** Three-state auth status (`loading` | `signed_in` | `signed_out`); same source as isLoaded/isSignedIn. */
|
|
40
|
+
status: AuthStatus;
|
|
41
|
+
/** The active session, or null while loading or signed out. */
|
|
42
|
+
session: AtlasSession | null;
|
|
38
43
|
userId: string | null;
|
|
39
44
|
sessionId: string | null;
|
|
40
45
|
orgId: string | null;
|
package/dist/accessors.js
CHANGED
|
@@ -20,9 +20,11 @@ export function getSession() {
|
|
|
20
20
|
}
|
|
21
21
|
export function getAuth() {
|
|
22
22
|
const atlas = useAtlas();
|
|
23
|
-
return derived([atlas.status, atlas.claims], ([$status, $claims]) => ({
|
|
23
|
+
return derived([atlas.status, atlas.session, atlas.claims], ([$status, $session, $claims]) => ({
|
|
24
24
|
isLoaded: $status !== 'loading',
|
|
25
25
|
isSignedIn: $status === 'signed_in',
|
|
26
|
+
status: $status,
|
|
27
|
+
session: $session,
|
|
26
28
|
userId: $claims?.sub ?? null,
|
|
27
29
|
sessionId: $claims?.sid ?? null,
|
|
28
30
|
orgId: $claims?.org_id ?? null,
|
package/dist/appearance.d.ts
CHANGED
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
* replacement is a support burden where every override starts by rebuilding the
|
|
18
18
|
* component's base styles from scratch.
|
|
19
19
|
*/
|
|
20
|
+
import { type AppearanceRender, type DesignAppearance, type DesignWarn, type InstanceDesign } from '@atlasauth/design/runtime';
|
|
21
|
+
export { instanceDesignFrom } from '@atlasauth/design/runtime';
|
|
22
|
+
export type { AppearanceRender, AtlasDesign, InstanceDesign, PresetId } from '@atlasauth/design/runtime';
|
|
20
23
|
export interface DesignTokens {
|
|
21
24
|
colorPrimary?: string;
|
|
22
25
|
colorText?: string;
|
|
@@ -29,9 +32,13 @@ export interface DesignTokens {
|
|
|
29
32
|
}
|
|
30
33
|
/** Every element a customer may target. Named for what it IS, not where it sits. */
|
|
31
34
|
export type ElementKey = 'root' | 'card' | 'headerTitle' | 'headerSubtitle' | 'formField' | 'formFieldLabel' | 'formFieldInput' | 'formFieldError' | 'formButtonPrimary' | 'formButtonSecondary' | 'socialButton' | 'dividerText' | 'footerAction' | 'avatar' | 'menu' | 'menuItem' | 'badge';
|
|
32
|
-
export interface Appearance {
|
|
33
|
-
/** A prebuilt theme to start from. */
|
|
35
|
+
export interface Appearance extends DesignAppearance {
|
|
36
|
+
/** A prebuilt token theme to start from (pre-design-system themes). */
|
|
34
37
|
baseTheme?: keyof typeof THEMES;
|
|
38
|
+
/**
|
|
39
|
+
* Legacy design tokens. Without a design they set the `--atlas-*` variables
|
|
40
|
+
* exactly as before; with one they map onto it.
|
|
41
|
+
*/
|
|
35
42
|
variables?: DesignTokens;
|
|
36
43
|
elements?: Partial<Record<ElementKey, string>>;
|
|
37
44
|
}
|
|
@@ -83,3 +90,21 @@ export declare function cssVariables(tokens: Required<DesignTokens>): Record<str
|
|
|
83
90
|
* specificity, which is the behaviour anyone writing Tailwind expects.
|
|
84
91
|
*/
|
|
85
92
|
export declare function classFor(element: ElementKey, appearance: Appearance | undefined, base: string): string;
|
|
93
|
+
/**
|
|
94
|
+
* The legacy tokens the developer chose EXPLICITLY: a non-default `baseTheme`
|
|
95
|
+
* plus `variables`. Only these map onto a design — the token defaults must not
|
|
96
|
+
* overwrite the dashboard design.
|
|
97
|
+
*/
|
|
98
|
+
export declare function explicitTokens(appearance?: Appearance): DesignTokens;
|
|
99
|
+
/**
|
|
100
|
+
* Turn an `appearance` (+ the instance's dashboard design from the Frontend
|
|
101
|
+
* API) into root attributes and a stylesheet — the same semantics as
|
|
102
|
+
* `@atlasauth/react`. Precedence, lowest first: dashboard design < `preset` <
|
|
103
|
+
* `variables` (and a non-default `baseTheme`) < `design`. Without a dashboard
|
|
104
|
+
* design and without `preset`/`design`, the output is exactly the
|
|
105
|
+
* pre-design-system one: the token variables, no stylesheet, no attributes.
|
|
106
|
+
*/
|
|
107
|
+
export declare function renderAppearance(appearance: Appearance | undefined, instanceDesign: InstanceDesign | null | undefined, options?: {
|
|
108
|
+
frontendApi?: string;
|
|
109
|
+
warn?: DesignWarn;
|
|
110
|
+
}): AppearanceRender;
|
package/dist/appearance.js
CHANGED
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
* replacement is a support burden where every override starts by rebuilding the
|
|
18
18
|
* component's base styles from scratch.
|
|
19
19
|
*/
|
|
20
|
+
import { renderDesignAppearance, } from '@atlasauth/design/runtime';
|
|
21
|
+
export { instanceDesignFrom } from '@atlasauth/design/runtime';
|
|
20
22
|
export const DEFAULT_TOKENS = {
|
|
21
23
|
colorPrimary: '#5b5bd6',
|
|
22
24
|
colorText: '#1c1c1f',
|
|
@@ -89,3 +91,36 @@ export function classFor(element, appearance, base) {
|
|
|
89
91
|
const override = appearance?.elements?.[element];
|
|
90
92
|
return override ? `${base} ${override}` : base;
|
|
91
93
|
}
|
|
94
|
+
// ---- the design system ---------------------------------------------------------
|
|
95
|
+
/**
|
|
96
|
+
* The legacy tokens the developer chose EXPLICITLY: a non-default `baseTheme`
|
|
97
|
+
* plus `variables`. Only these map onto a design — the token defaults must not
|
|
98
|
+
* overwrite the dashboard design.
|
|
99
|
+
*/
|
|
100
|
+
export function explicitTokens(appearance) {
|
|
101
|
+
const out = {};
|
|
102
|
+
if (appearance?.baseTheme && appearance.baseTheme !== 'default') {
|
|
103
|
+
Object.assign(out, THEMES[appearance.baseTheme]);
|
|
104
|
+
}
|
|
105
|
+
for (const [key, value] of Object.entries(appearance?.variables ?? {})) {
|
|
106
|
+
if (value !== undefined)
|
|
107
|
+
out[key] = value;
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Turn an `appearance` (+ the instance's dashboard design from the Frontend
|
|
113
|
+
* API) into root attributes and a stylesheet — the same semantics as
|
|
114
|
+
* `@atlasauth/react`. Precedence, lowest first: dashboard design < `preset` <
|
|
115
|
+
* `variables` (and a non-default `baseTheme`) < `design`. Without a dashboard
|
|
116
|
+
* design and without `preset`/`design`, the output is exactly the
|
|
117
|
+
* pre-design-system one: the token variables, no stylesheet, no attributes.
|
|
118
|
+
*/
|
|
119
|
+
export function renderAppearance(appearance, instanceDesign, options = {}) {
|
|
120
|
+
return renderDesignAppearance(appearance, instanceDesign, {
|
|
121
|
+
tokens: explicitTokens(appearance),
|
|
122
|
+
legacyStyle: () => cssVariables(resolveTokens(appearance)),
|
|
123
|
+
frontendApi: options.frontendApi,
|
|
124
|
+
warn: options.warn,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<script>import { onDestroy, onMount } from "svelte";
|
|
2
2
|
import { initAtlas } from "../context";
|
|
3
|
-
import {
|
|
3
|
+
import { renderAppearance } from "../appearance";
|
|
4
4
|
export let publishableKey;
|
|
5
5
|
export let frontendApi = void 0;
|
|
6
6
|
export let appearance = void 0;
|
|
@@ -13,11 +13,28 @@ const client = initAtlas({
|
|
|
13
13
|
localization,
|
|
14
14
|
fetchImpl
|
|
15
15
|
});
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
let instanceDesign = null;
|
|
17
|
+
$: rendered = renderAppearance(appearance, instanceDesign, { frontendApi });
|
|
18
|
+
$: style = Object.entries(rendered.style).map(([name, value]) => `${name}: ${value}`).join("; ");
|
|
19
|
+
$: designStyle = rendered.css ? `<style data-atlas-design="${rendered.key}">${rendered.css}</style>` : "";
|
|
20
|
+
onMount(() => {
|
|
21
|
+
void client.boot();
|
|
22
|
+
if (appearance?.instanceDesign !== false) {
|
|
23
|
+
void client.loadInstanceDesign().then((design) => {
|
|
24
|
+
if (design) instanceDesign = design;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
18
28
|
onDestroy(() => client.destroy());
|
|
19
29
|
</script>
|
|
20
30
|
|
|
21
|
-
<
|
|
31
|
+
<svelte:head>
|
|
32
|
+
{@html designStyle}
|
|
33
|
+
{#each rendered.fontStylesheets as href (href)}
|
|
34
|
+
<link rel="stylesheet" {href} data-atlas-font="" />
|
|
35
|
+
{/each}
|
|
36
|
+
</svelte:head>
|
|
37
|
+
|
|
38
|
+
<div class="atlas-root" {style} {...rendered.attributes}>
|
|
22
39
|
<slot />
|
|
23
40
|
</div>
|
package/dist/context.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Readable } from 'svelte/store';
|
|
2
2
|
import { type ProviderToken } from '@atlasauth/js';
|
|
3
3
|
import { type Catalog } from './i18n';
|
|
4
|
-
import type
|
|
4
|
+
import { type Appearance, type InstanceDesign } from './appearance';
|
|
5
5
|
import type { AtlasOrganizationMembership, AtlasSession, AtlasUser, SessionClaims } from './types';
|
|
6
6
|
/**
|
|
7
7
|
* §10.1 the Svelte peer of React's `<AtlasProvider>`.
|
|
@@ -80,6 +80,11 @@ export interface AtlasClient extends AtlasStores {
|
|
|
80
80
|
* `<AtlasProvider>` also calls it in `onMount`, and a second call is a no-op.
|
|
81
81
|
*/
|
|
82
82
|
boot(): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* The design configured in the Atlas dashboard (`GET /v1/appearance`), or null
|
|
85
|
+
* when there is none or it cannot be read. `<AtlasProvider>` applies it.
|
|
86
|
+
*/
|
|
87
|
+
loadInstanceDesign(): Promise<InstanceDesign | null>;
|
|
83
88
|
/** Cancel the scheduled refresh — call from a component's `onDestroy`. */
|
|
84
89
|
destroy(): void;
|
|
85
90
|
}
|
package/dist/context.js
CHANGED
|
@@ -2,6 +2,7 @@ import { getContext, setContext } from 'svelte';
|
|
|
2
2
|
import { derived, get, writable } from 'svelte/store';
|
|
3
3
|
import { FapiClient, TokenCache, getProviderToken as fetchProviderToken, msUntilRefresh, readExpiry, readRedirectResult, stripRedirectParams, } from '@atlasauth/js';
|
|
4
4
|
import { EN_US, withFallback } from './i18n';
|
|
5
|
+
import { instanceDesignFrom } from './appearance';
|
|
5
6
|
/** The context key. A `Symbol` so it cannot collide with an app's own context. */
|
|
6
7
|
export const ATLAS_KEY = Symbol('atlas');
|
|
7
8
|
function decodeClaims(jwt) {
|
|
@@ -196,6 +197,15 @@ export function createAtlasClient(options) {
|
|
|
196
197
|
}
|
|
197
198
|
await reload();
|
|
198
199
|
}
|
|
200
|
+
async function loadInstanceDesign() {
|
|
201
|
+
try {
|
|
202
|
+
const response = await call('/v1/appearance');
|
|
203
|
+
return response.ok ? instanceDesignFrom(await response.json()) : null;
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
199
209
|
function destroy() {
|
|
200
210
|
if (timer)
|
|
201
211
|
clearTimeout(timer);
|
|
@@ -222,6 +232,7 @@ export function createAtlasClient(options) {
|
|
|
222
232
|
setActiveOrganization,
|
|
223
233
|
reload,
|
|
224
234
|
boot,
|
|
235
|
+
loadInstanceDesign,
|
|
225
236
|
destroy,
|
|
226
237
|
};
|
|
227
238
|
// Kick off the browser boot immediately unless the caller opts out. Guarded to
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlasauth/svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"svelte": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -21,9 +21,10 @@
|
|
|
21
21
|
"./package.json": "./package.json"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@atlasauth/
|
|
24
|
+
"@atlasauth/design": "0.1.0",
|
|
25
25
|
"@atlasauth/authz": "0.1.1",
|
|
26
|
-
"@atlasauth/
|
|
26
|
+
"@atlasauth/js": "0.4.0",
|
|
27
|
+
"@atlasauth/backend": "0.3.0"
|
|
27
28
|
},
|
|
28
29
|
"peerDependencies": {
|
|
29
30
|
"svelte": ">=4",
|