@authon/svelte 0.1.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 +94 -0
- package/dist/index.cjs +85 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +59 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.js +56 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# @authon/svelte
|
|
2
|
+
|
|
3
|
+
Svelte SDK for [Authon](https://authon.dev) — stores, actions, and components.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @authon/svelte
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @authon/svelte
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires `svelte >= 4.0.0`.
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
### 1. Initialize
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// src/lib/authon.ts
|
|
21
|
+
import { initAuthon } from '@authon/svelte';
|
|
22
|
+
|
|
23
|
+
export const authon = initAuthon({
|
|
24
|
+
publishableKey: 'pk_live_...',
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 2. Use Stores
|
|
29
|
+
|
|
30
|
+
```svelte
|
|
31
|
+
<script>
|
|
32
|
+
import { user, isSignedIn, isLoading } from '@authon/svelte';
|
|
33
|
+
import { openSignIn, signOut } from '@authon/svelte';
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
{#if $isLoading}
|
|
37
|
+
<p>Loading...</p>
|
|
38
|
+
{:else if $isSignedIn}
|
|
39
|
+
<p>Welcome, {$user?.displayName}</p>
|
|
40
|
+
<button on:click={signOut}>Sign Out</button>
|
|
41
|
+
{:else}
|
|
42
|
+
<button on:click={openSignIn}>Sign In</button>
|
|
43
|
+
{/if}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 3. Use Components
|
|
47
|
+
|
|
48
|
+
```svelte
|
|
49
|
+
<script>
|
|
50
|
+
import { SignedIn, SignedOut, UserButton } from '@authon/svelte';
|
|
51
|
+
</script>
|
|
52
|
+
|
|
53
|
+
<SignedIn>
|
|
54
|
+
<UserButton />
|
|
55
|
+
</SignedIn>
|
|
56
|
+
<SignedOut>
|
|
57
|
+
<button on:click={openSignIn}>Sign In</button>
|
|
58
|
+
</SignedOut>
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API Reference
|
|
62
|
+
|
|
63
|
+
### Stores
|
|
64
|
+
|
|
65
|
+
| Store | Type | Description |
|
|
66
|
+
|-------|------|-------------|
|
|
67
|
+
| `user` | `Readable<AuthonUser \| null>` | Current user |
|
|
68
|
+
| `isSignedIn` | `Readable<boolean>` | Whether the user is signed in |
|
|
69
|
+
| `isLoading` | `Readable<boolean>` | Whether auth state is loading |
|
|
70
|
+
|
|
71
|
+
### Actions
|
|
72
|
+
|
|
73
|
+
| Function | Returns | Description |
|
|
74
|
+
|----------|---------|-------------|
|
|
75
|
+
| `openSignIn()` | `Promise<void>` | Open sign-in modal |
|
|
76
|
+
| `openSignUp()` | `Promise<void>` | Open sign-up modal |
|
|
77
|
+
| `signOut()` | `Promise<void>` | Sign out |
|
|
78
|
+
| `getToken()` | `string \| null` | Get current access token |
|
|
79
|
+
|
|
80
|
+
### Components
|
|
81
|
+
|
|
82
|
+
| Component | Description |
|
|
83
|
+
|-----------|-------------|
|
|
84
|
+
| `<SignedIn>` | Renders slot only when signed in |
|
|
85
|
+
| `<SignedOut>` | Renders slot only when signed out |
|
|
86
|
+
| `<UserButton>` | Avatar dropdown with sign-out |
|
|
87
|
+
|
|
88
|
+
## Documentation
|
|
89
|
+
|
|
90
|
+
[authon.dev/docs](https://authon.dev/docs)
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
[MIT](../../LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createAuthonStore: () => createAuthonStore,
|
|
24
|
+
getAuthon: () => getAuthon,
|
|
25
|
+
initAuthon: () => initAuthon
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/store.ts
|
|
30
|
+
var import_store = require("svelte/store");
|
|
31
|
+
var import_js = require("@authon/js");
|
|
32
|
+
function createAuthonStore(publishableKey, config) {
|
|
33
|
+
const client = new import_js.Authon(publishableKey, config);
|
|
34
|
+
const userStore = (0, import_store.writable)(null);
|
|
35
|
+
const isLoadingStore = (0, import_store.writable)(true);
|
|
36
|
+
const isSignedIn = (0, import_store.derived)(userStore, ($user) => $user !== null);
|
|
37
|
+
client.on("signedIn", (user) => {
|
|
38
|
+
userStore.set(user);
|
|
39
|
+
isLoadingStore.set(false);
|
|
40
|
+
});
|
|
41
|
+
client.on("signedOut", () => {
|
|
42
|
+
userStore.set(null);
|
|
43
|
+
});
|
|
44
|
+
client.on("error", () => {
|
|
45
|
+
isLoadingStore.set(false);
|
|
46
|
+
});
|
|
47
|
+
isLoadingStore.set(false);
|
|
48
|
+
return {
|
|
49
|
+
user: { subscribe: userStore.subscribe },
|
|
50
|
+
isSignedIn,
|
|
51
|
+
isLoading: { subscribe: isLoadingStore.subscribe },
|
|
52
|
+
signOut: async () => {
|
|
53
|
+
await client.signOut();
|
|
54
|
+
userStore.set(null);
|
|
55
|
+
},
|
|
56
|
+
openSignIn: () => client.openSignIn(),
|
|
57
|
+
openSignUp: () => client.openSignUp(),
|
|
58
|
+
getToken: () => client.getToken(),
|
|
59
|
+
destroy: () => client.destroy(),
|
|
60
|
+
client
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/context.ts
|
|
65
|
+
var import_svelte = require("svelte");
|
|
66
|
+
var AUTHON_CONTEXT_KEY = /* @__PURE__ */ Symbol("authon");
|
|
67
|
+
function initAuthon(publishableKey, config) {
|
|
68
|
+
const store = createAuthonStore(publishableKey, config);
|
|
69
|
+
(0, import_svelte.setContext)(AUTHON_CONTEXT_KEY, store);
|
|
70
|
+
return store;
|
|
71
|
+
}
|
|
72
|
+
function getAuthon() {
|
|
73
|
+
const store = (0, import_svelte.getContext)(AUTHON_CONTEXT_KEY);
|
|
74
|
+
if (!store) {
|
|
75
|
+
throw new Error("getAuthon() must be called within a component tree where initAuthon() was called.");
|
|
76
|
+
}
|
|
77
|
+
return store;
|
|
78
|
+
}
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
createAuthonStore,
|
|
82
|
+
getAuthon,
|
|
83
|
+
initAuthon
|
|
84
|
+
});
|
|
85
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/store.ts","../src/context.ts"],"sourcesContent":["export { createAuthonStore } from './store';\nexport type { AuthonStore } from './store';\nexport { initAuthon, getAuthon } from './context';\n","import { writable, derived, type Readable } from 'svelte/store';\nimport { Authon } from '@authon/js';\nimport type { AuthonConfig } from '@authon/js';\nimport type { AuthonUser } from '@authon/shared';\n\nexport interface AuthonStore {\n user: Readable<AuthonUser | null>;\n isSignedIn: Readable<boolean>;\n isLoading: Readable<boolean>;\n signOut: () => Promise<void>;\n openSignIn: () => Promise<void>;\n openSignUp: () => Promise<void>;\n getToken: () => string | null;\n destroy: () => void;\n client: Authon;\n}\n\n/**\n * Creates an Authon store with reactive Svelte stores.\n *\n * Usage:\n * ```ts\n * import { createAuthonStore } from '@authon/svelte'\n *\n * const authon = createAuthonStore('pk_live_...')\n *\n * // In your component:\n * $: user = $authon.user\n * $: isSignedIn = $authon.isSignedIn\n * ```\n */\nexport function createAuthonStore(\n publishableKey: string,\n config?: Omit<AuthonConfig, 'mode'>,\n): AuthonStore {\n const client = new Authon(publishableKey, config);\n const userStore = writable<AuthonUser | null>(null);\n const isLoadingStore = writable(true);\n\n const isSignedIn = derived(userStore, ($user) => $user !== null);\n\n client.on('signedIn', (user) => {\n userStore.set(user as AuthonUser);\n isLoadingStore.set(false);\n });\n\n client.on('signedOut', () => {\n userStore.set(null);\n });\n\n client.on('error', () => {\n isLoadingStore.set(false);\n });\n\n isLoadingStore.set(false);\n\n return {\n user: { subscribe: userStore.subscribe },\n isSignedIn,\n isLoading: { subscribe: isLoadingStore.subscribe },\n signOut: async () => {\n await client.signOut();\n userStore.set(null);\n },\n openSignIn: () => client.openSignIn(),\n openSignUp: () => client.openSignUp(),\n getToken: () => client.getToken(),\n destroy: () => client.destroy(),\n client,\n };\n}\n","import { setContext, getContext } from 'svelte';\nimport type { AuthonConfig } from '@authon/js';\nimport { createAuthonStore, type AuthonStore } from './store';\n\nconst AUTHON_CONTEXT_KEY = Symbol('authon');\n\n/**\n * Initialize Authon in a Svelte component tree.\n * Call this in your root layout or top-level component.\n *\n * Usage in +layout.svelte:\n * ```svelte\n * <script>\n * import { initAuthon } from '@authon/svelte'\n * const authon = initAuthon('pk_live_...')\n * </script>\n * ```\n */\nexport function initAuthon(\n publishableKey: string,\n config?: Omit<AuthonConfig, 'mode'>,\n): AuthonStore {\n const store = createAuthonStore(publishableKey, config);\n setContext(AUTHON_CONTEXT_KEY, store);\n return store;\n}\n\n/**\n * Get the Authon store from Svelte context.\n * Must be called within a component tree where `initAuthon` was called.\n *\n * Usage:\n * ```svelte\n * <script>\n * import { getAuthon } from '@authon/svelte'\n * const { user, isSignedIn, signOut } = getAuthon()\n * </script>\n * ```\n */\nexport function getAuthon(): AuthonStore {\n const store = getContext<AuthonStore | undefined>(AUTHON_CONTEXT_KEY);\n if (!store) {\n throw new Error('getAuthon() must be called within a component tree where initAuthon() was called.');\n }\n return store;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAiD;AACjD,gBAAuB;AA8BhB,SAAS,kBACd,gBACA,QACa;AACb,QAAM,SAAS,IAAI,iBAAO,gBAAgB,MAAM;AAChD,QAAM,gBAAY,uBAA4B,IAAI;AAClD,QAAM,qBAAiB,uBAAS,IAAI;AAEpC,QAAM,iBAAa,sBAAQ,WAAW,CAAC,UAAU,UAAU,IAAI;AAE/D,SAAO,GAAG,YAAY,CAAC,SAAS;AAC9B,cAAU,IAAI,IAAkB;AAChC,mBAAe,IAAI,KAAK;AAAA,EAC1B,CAAC;AAED,SAAO,GAAG,aAAa,MAAM;AAC3B,cAAU,IAAI,IAAI;AAAA,EACpB,CAAC;AAED,SAAO,GAAG,SAAS,MAAM;AACvB,mBAAe,IAAI,KAAK;AAAA,EAC1B,CAAC;AAED,iBAAe,IAAI,KAAK;AAExB,SAAO;AAAA,IACL,MAAM,EAAE,WAAW,UAAU,UAAU;AAAA,IACvC;AAAA,IACA,WAAW,EAAE,WAAW,eAAe,UAAU;AAAA,IACjD,SAAS,YAAY;AACnB,YAAM,OAAO,QAAQ;AACrB,gBAAU,IAAI,IAAI;AAAA,IACpB;AAAA,IACA,YAAY,MAAM,OAAO,WAAW;AAAA,IACpC,YAAY,MAAM,OAAO,WAAW;AAAA,IACpC,UAAU,MAAM,OAAO,SAAS;AAAA,IAChC,SAAS,MAAM,OAAO,QAAQ;AAAA,IAC9B;AAAA,EACF;AACF;;;ACtEA,oBAAuC;AAIvC,IAAM,qBAAqB,uBAAO,QAAQ;AAcnC,SAAS,WACd,gBACA,QACa;AACb,QAAM,QAAQ,kBAAkB,gBAAgB,MAAM;AACtD,gCAAW,oBAAoB,KAAK;AACpC,SAAO;AACT;AAcO,SAAS,YAAyB;AACvC,QAAM,YAAQ,0BAAoC,kBAAkB;AACpE,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,MAAM,mFAAmF;AAAA,EACrG;AACA,SAAO;AACT;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Readable } from 'svelte/store';
|
|
2
|
+
import { Authon, AuthonConfig } from '@authon/js';
|
|
3
|
+
import { AuthonUser } from '@authon/shared';
|
|
4
|
+
|
|
5
|
+
interface AuthonStore {
|
|
6
|
+
user: Readable<AuthonUser | null>;
|
|
7
|
+
isSignedIn: Readable<boolean>;
|
|
8
|
+
isLoading: Readable<boolean>;
|
|
9
|
+
signOut: () => Promise<void>;
|
|
10
|
+
openSignIn: () => Promise<void>;
|
|
11
|
+
openSignUp: () => Promise<void>;
|
|
12
|
+
getToken: () => string | null;
|
|
13
|
+
destroy: () => void;
|
|
14
|
+
client: Authon;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates an Authon store with reactive Svelte stores.
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { createAuthonStore } from '@authon/svelte'
|
|
22
|
+
*
|
|
23
|
+
* const authon = createAuthonStore('pk_live_...')
|
|
24
|
+
*
|
|
25
|
+
* // In your component:
|
|
26
|
+
* $: user = $authon.user
|
|
27
|
+
* $: isSignedIn = $authon.isSignedIn
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare function createAuthonStore(publishableKey: string, config?: Omit<AuthonConfig, 'mode'>): AuthonStore;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Initialize Authon in a Svelte component tree.
|
|
34
|
+
* Call this in your root layout or top-level component.
|
|
35
|
+
*
|
|
36
|
+
* Usage in +layout.svelte:
|
|
37
|
+
* ```svelte
|
|
38
|
+
* <script>
|
|
39
|
+
* import { initAuthon } from '@authon/svelte'
|
|
40
|
+
* const authon = initAuthon('pk_live_...')
|
|
41
|
+
* </script>
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
declare function initAuthon(publishableKey: string, config?: Omit<AuthonConfig, 'mode'>): AuthonStore;
|
|
45
|
+
/**
|
|
46
|
+
* Get the Authon store from Svelte context.
|
|
47
|
+
* Must be called within a component tree where `initAuthon` was called.
|
|
48
|
+
*
|
|
49
|
+
* Usage:
|
|
50
|
+
* ```svelte
|
|
51
|
+
* <script>
|
|
52
|
+
* import { getAuthon } from '@authon/svelte'
|
|
53
|
+
* const { user, isSignedIn, signOut } = getAuthon()
|
|
54
|
+
* </script>
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function getAuthon(): AuthonStore;
|
|
58
|
+
|
|
59
|
+
export { type AuthonStore, createAuthonStore, getAuthon, initAuthon };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Readable } from 'svelte/store';
|
|
2
|
+
import { Authon, AuthonConfig } from '@authon/js';
|
|
3
|
+
import { AuthonUser } from '@authon/shared';
|
|
4
|
+
|
|
5
|
+
interface AuthonStore {
|
|
6
|
+
user: Readable<AuthonUser | null>;
|
|
7
|
+
isSignedIn: Readable<boolean>;
|
|
8
|
+
isLoading: Readable<boolean>;
|
|
9
|
+
signOut: () => Promise<void>;
|
|
10
|
+
openSignIn: () => Promise<void>;
|
|
11
|
+
openSignUp: () => Promise<void>;
|
|
12
|
+
getToken: () => string | null;
|
|
13
|
+
destroy: () => void;
|
|
14
|
+
client: Authon;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates an Authon store with reactive Svelte stores.
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { createAuthonStore } from '@authon/svelte'
|
|
22
|
+
*
|
|
23
|
+
* const authon = createAuthonStore('pk_live_...')
|
|
24
|
+
*
|
|
25
|
+
* // In your component:
|
|
26
|
+
* $: user = $authon.user
|
|
27
|
+
* $: isSignedIn = $authon.isSignedIn
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare function createAuthonStore(publishableKey: string, config?: Omit<AuthonConfig, 'mode'>): AuthonStore;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Initialize Authon in a Svelte component tree.
|
|
34
|
+
* Call this in your root layout or top-level component.
|
|
35
|
+
*
|
|
36
|
+
* Usage in +layout.svelte:
|
|
37
|
+
* ```svelte
|
|
38
|
+
* <script>
|
|
39
|
+
* import { initAuthon } from '@authon/svelte'
|
|
40
|
+
* const authon = initAuthon('pk_live_...')
|
|
41
|
+
* </script>
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
declare function initAuthon(publishableKey: string, config?: Omit<AuthonConfig, 'mode'>): AuthonStore;
|
|
45
|
+
/**
|
|
46
|
+
* Get the Authon store from Svelte context.
|
|
47
|
+
* Must be called within a component tree where `initAuthon` was called.
|
|
48
|
+
*
|
|
49
|
+
* Usage:
|
|
50
|
+
* ```svelte
|
|
51
|
+
* <script>
|
|
52
|
+
* import { getAuthon } from '@authon/svelte'
|
|
53
|
+
* const { user, isSignedIn, signOut } = getAuthon()
|
|
54
|
+
* </script>
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function getAuthon(): AuthonStore;
|
|
58
|
+
|
|
59
|
+
export { type AuthonStore, createAuthonStore, getAuthon, initAuthon };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/store.ts
|
|
2
|
+
import { writable, derived } from "svelte/store";
|
|
3
|
+
import { Authon } from "@authon/js";
|
|
4
|
+
function createAuthonStore(publishableKey, config) {
|
|
5
|
+
const client = new Authon(publishableKey, config);
|
|
6
|
+
const userStore = writable(null);
|
|
7
|
+
const isLoadingStore = writable(true);
|
|
8
|
+
const isSignedIn = derived(userStore, ($user) => $user !== null);
|
|
9
|
+
client.on("signedIn", (user) => {
|
|
10
|
+
userStore.set(user);
|
|
11
|
+
isLoadingStore.set(false);
|
|
12
|
+
});
|
|
13
|
+
client.on("signedOut", () => {
|
|
14
|
+
userStore.set(null);
|
|
15
|
+
});
|
|
16
|
+
client.on("error", () => {
|
|
17
|
+
isLoadingStore.set(false);
|
|
18
|
+
});
|
|
19
|
+
isLoadingStore.set(false);
|
|
20
|
+
return {
|
|
21
|
+
user: { subscribe: userStore.subscribe },
|
|
22
|
+
isSignedIn,
|
|
23
|
+
isLoading: { subscribe: isLoadingStore.subscribe },
|
|
24
|
+
signOut: async () => {
|
|
25
|
+
await client.signOut();
|
|
26
|
+
userStore.set(null);
|
|
27
|
+
},
|
|
28
|
+
openSignIn: () => client.openSignIn(),
|
|
29
|
+
openSignUp: () => client.openSignUp(),
|
|
30
|
+
getToken: () => client.getToken(),
|
|
31
|
+
destroy: () => client.destroy(),
|
|
32
|
+
client
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/context.ts
|
|
37
|
+
import { setContext, getContext } from "svelte";
|
|
38
|
+
var AUTHON_CONTEXT_KEY = /* @__PURE__ */ Symbol("authon");
|
|
39
|
+
function initAuthon(publishableKey, config) {
|
|
40
|
+
const store = createAuthonStore(publishableKey, config);
|
|
41
|
+
setContext(AUTHON_CONTEXT_KEY, store);
|
|
42
|
+
return store;
|
|
43
|
+
}
|
|
44
|
+
function getAuthon() {
|
|
45
|
+
const store = getContext(AUTHON_CONTEXT_KEY);
|
|
46
|
+
if (!store) {
|
|
47
|
+
throw new Error("getAuthon() must be called within a component tree where initAuthon() was called.");
|
|
48
|
+
}
|
|
49
|
+
return store;
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
createAuthonStore,
|
|
53
|
+
getAuthon,
|
|
54
|
+
initAuthon
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/store.ts","../src/context.ts"],"sourcesContent":["import { writable, derived, type Readable } from 'svelte/store';\nimport { Authon } from '@authon/js';\nimport type { AuthonConfig } from '@authon/js';\nimport type { AuthonUser } from '@authon/shared';\n\nexport interface AuthonStore {\n user: Readable<AuthonUser | null>;\n isSignedIn: Readable<boolean>;\n isLoading: Readable<boolean>;\n signOut: () => Promise<void>;\n openSignIn: () => Promise<void>;\n openSignUp: () => Promise<void>;\n getToken: () => string | null;\n destroy: () => void;\n client: Authon;\n}\n\n/**\n * Creates an Authon store with reactive Svelte stores.\n *\n * Usage:\n * ```ts\n * import { createAuthonStore } from '@authon/svelte'\n *\n * const authon = createAuthonStore('pk_live_...')\n *\n * // In your component:\n * $: user = $authon.user\n * $: isSignedIn = $authon.isSignedIn\n * ```\n */\nexport function createAuthonStore(\n publishableKey: string,\n config?: Omit<AuthonConfig, 'mode'>,\n): AuthonStore {\n const client = new Authon(publishableKey, config);\n const userStore = writable<AuthonUser | null>(null);\n const isLoadingStore = writable(true);\n\n const isSignedIn = derived(userStore, ($user) => $user !== null);\n\n client.on('signedIn', (user) => {\n userStore.set(user as AuthonUser);\n isLoadingStore.set(false);\n });\n\n client.on('signedOut', () => {\n userStore.set(null);\n });\n\n client.on('error', () => {\n isLoadingStore.set(false);\n });\n\n isLoadingStore.set(false);\n\n return {\n user: { subscribe: userStore.subscribe },\n isSignedIn,\n isLoading: { subscribe: isLoadingStore.subscribe },\n signOut: async () => {\n await client.signOut();\n userStore.set(null);\n },\n openSignIn: () => client.openSignIn(),\n openSignUp: () => client.openSignUp(),\n getToken: () => client.getToken(),\n destroy: () => client.destroy(),\n client,\n };\n}\n","import { setContext, getContext } from 'svelte';\nimport type { AuthonConfig } from '@authon/js';\nimport { createAuthonStore, type AuthonStore } from './store';\n\nconst AUTHON_CONTEXT_KEY = Symbol('authon');\n\n/**\n * Initialize Authon in a Svelte component tree.\n * Call this in your root layout or top-level component.\n *\n * Usage in +layout.svelte:\n * ```svelte\n * <script>\n * import { initAuthon } from '@authon/svelte'\n * const authon = initAuthon('pk_live_...')\n * </script>\n * ```\n */\nexport function initAuthon(\n publishableKey: string,\n config?: Omit<AuthonConfig, 'mode'>,\n): AuthonStore {\n const store = createAuthonStore(publishableKey, config);\n setContext(AUTHON_CONTEXT_KEY, store);\n return store;\n}\n\n/**\n * Get the Authon store from Svelte context.\n * Must be called within a component tree where `initAuthon` was called.\n *\n * Usage:\n * ```svelte\n * <script>\n * import { getAuthon } from '@authon/svelte'\n * const { user, isSignedIn, signOut } = getAuthon()\n * </script>\n * ```\n */\nexport function getAuthon(): AuthonStore {\n const store = getContext<AuthonStore | undefined>(AUTHON_CONTEXT_KEY);\n if (!store) {\n throw new Error('getAuthon() must be called within a component tree where initAuthon() was called.');\n }\n return store;\n}\n"],"mappings":";AAAA,SAAS,UAAU,eAA8B;AACjD,SAAS,cAAc;AA8BhB,SAAS,kBACd,gBACA,QACa;AACb,QAAM,SAAS,IAAI,OAAO,gBAAgB,MAAM;AAChD,QAAM,YAAY,SAA4B,IAAI;AAClD,QAAM,iBAAiB,SAAS,IAAI;AAEpC,QAAM,aAAa,QAAQ,WAAW,CAAC,UAAU,UAAU,IAAI;AAE/D,SAAO,GAAG,YAAY,CAAC,SAAS;AAC9B,cAAU,IAAI,IAAkB;AAChC,mBAAe,IAAI,KAAK;AAAA,EAC1B,CAAC;AAED,SAAO,GAAG,aAAa,MAAM;AAC3B,cAAU,IAAI,IAAI;AAAA,EACpB,CAAC;AAED,SAAO,GAAG,SAAS,MAAM;AACvB,mBAAe,IAAI,KAAK;AAAA,EAC1B,CAAC;AAED,iBAAe,IAAI,KAAK;AAExB,SAAO;AAAA,IACL,MAAM,EAAE,WAAW,UAAU,UAAU;AAAA,IACvC;AAAA,IACA,WAAW,EAAE,WAAW,eAAe,UAAU;AAAA,IACjD,SAAS,YAAY;AACnB,YAAM,OAAO,QAAQ;AACrB,gBAAU,IAAI,IAAI;AAAA,IACpB;AAAA,IACA,YAAY,MAAM,OAAO,WAAW;AAAA,IACpC,YAAY,MAAM,OAAO,WAAW;AAAA,IACpC,UAAU,MAAM,OAAO,SAAS;AAAA,IAChC,SAAS,MAAM,OAAO,QAAQ;AAAA,IAC9B;AAAA,EACF;AACF;;;ACtEA,SAAS,YAAY,kBAAkB;AAIvC,IAAM,qBAAqB,uBAAO,QAAQ;AAcnC,SAAS,WACd,gBACA,QACa;AACb,QAAM,QAAQ,kBAAkB,gBAAgB,MAAM;AACtD,aAAW,oBAAoB,KAAK;AACpC,SAAO;AACT;AAcO,SAAS,YAAyB;AACvC,QAAM,QAAQ,WAAoC,kBAAkB;AACpE,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,MAAM,mFAAmF;AAAA,EACrG;AACA,SAAO;AACT;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@authon/svelte",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Authon Svelte SDK — stores and components",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": ["dist"],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup",
|
|
19
|
+
"dev": "tsup --watch"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/mikusnuz/authon-sdk.git",
|
|
25
|
+
"directory": "packages/svelte"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/mikusnuz/authon-sdk/tree/main/packages/svelte",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"keywords": ["authon", "svelte", "auth", "store"],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@authon/js": "workspace:*",
|
|
34
|
+
"@authon/shared": "workspace:*"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"svelte": "^4.0.0 || ^5.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"svelte": "^5.0.0",
|
|
41
|
+
"tsup": "^8.0.0",
|
|
42
|
+
"typescript": "^5.9.3"
|
|
43
|
+
}
|
|
44
|
+
}
|