@authon/nuxt 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 ADDED
@@ -0,0 +1,105 @@
1
+ # @authon/nuxt
2
+
3
+ Nuxt 3 module for [Authon](https://authon.dev) — auto-imported composables, components, and server middleware.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @authon/nuxt
9
+ # or
10
+ pnpm add @authon/nuxt
11
+ ```
12
+
13
+ Requires `nuxt >= 3.0.0`.
14
+
15
+ ## Quick Start
16
+
17
+ ### 1. Add Module
18
+
19
+ ```ts
20
+ // nuxt.config.ts
21
+ export default defineNuxtConfig({
22
+ modules: ['@authon/nuxt'],
23
+ authon: {
24
+ publishableKey: process.env.NUXT_PUBLIC_AUTHON_KEY,
25
+ },
26
+ });
27
+ ```
28
+
29
+ ### 2. Use in Pages
30
+
31
+ All composables and components are auto-imported:
32
+
33
+ ```vue
34
+ <!-- pages/index.vue -->
35
+ <template>
36
+ <div>
37
+ <SignedIn>
38
+ <UserButton />
39
+ <p>Welcome, {{ user?.displayName }}</p>
40
+ </SignedIn>
41
+ <SignedOut>
42
+ <button @click="openSignIn()">Sign In</button>
43
+ </SignedOut>
44
+ </div>
45
+ </template>
46
+
47
+ <script setup>
48
+ const { user, openSignIn } = useAuthon();
49
+ </script>
50
+ ```
51
+
52
+ ### 3. Server-side
53
+
54
+ ```ts
55
+ // server/api/profile.get.ts
56
+ export default defineEventHandler(async (event) => {
57
+ const user = await requireAuthonUser(event);
58
+ return { user };
59
+ });
60
+ ```
61
+
62
+ ## API Reference
63
+
64
+ ### Module Options
65
+
66
+ ```ts
67
+ // nuxt.config.ts
68
+ authon: {
69
+ publishableKey: string;
70
+ secretKey?: string; // For server-side verification
71
+ apiUrl?: string;
72
+ publicRoutes?: string[]; // Routes that don't require auth
73
+ }
74
+ ```
75
+
76
+ ### Auto-imported Composables
77
+
78
+ | Composable | Description |
79
+ |------------|-------------|
80
+ | `useAuthon()` | Full auth state and actions |
81
+ | `useUser()` | Current user and loading state |
82
+
83
+ ### Auto-imported Components
84
+
85
+ | Component | Description |
86
+ |-----------|-------------|
87
+ | `<SignedIn>` | Renders slot only when signed in |
88
+ | `<SignedOut>` | Renders slot only when signed out |
89
+ | `<UserButton>` | Avatar dropdown with sign-out |
90
+ | `<Protect>` | Conditional rendering with fallback slot |
91
+
92
+ ### Server Utilities
93
+
94
+ | Function | Description |
95
+ |----------|-------------|
96
+ | `requireAuthonUser(event)` | Get and verify the current user (throws 401 if unauthenticated) |
97
+ | `getAuthonUser(event)` | Get the current user or null |
98
+
99
+ ## Documentation
100
+
101
+ [authon.dev/docs](https://authon.dev/docs)
102
+
103
+ ## License
104
+
105
+ [MIT](../../LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,98 @@
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
+ authonModule: () => authonModule,
24
+ createAuthMiddleware: () => createAuthMiddleware,
25
+ createAuthonPlugin: () => createAuthonPlugin,
26
+ default: () => authonModule,
27
+ useAuthon: () => useAuthon,
28
+ useUser: () => useUser
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/module.ts
33
+ function authonModule(options) {
34
+ return {
35
+ name: "@authon/nuxt",
36
+ options
37
+ };
38
+ }
39
+
40
+ // src/plugin.ts
41
+ var import_js = require("@authon/js");
42
+ function createAuthonPlugin(publishableKey, config) {
43
+ const client = new import_js.Authon(publishableKey, config);
44
+ const state = {
45
+ client,
46
+ user: null,
47
+ isSignedIn: false,
48
+ isLoading: true
49
+ };
50
+ client.on("signedIn", (user) => {
51
+ state.user = user;
52
+ state.isSignedIn = true;
53
+ state.isLoading = false;
54
+ });
55
+ client.on("signedOut", () => {
56
+ state.user = null;
57
+ state.isSignedIn = false;
58
+ });
59
+ client.on("error", () => {
60
+ state.isLoading = false;
61
+ });
62
+ state.isLoading = false;
63
+ return state;
64
+ }
65
+
66
+ // src/composables.ts
67
+ function useAuthon() {
68
+ throw new Error(
69
+ "useAuthon() must be used within a Nuxt app with the Authon plugin installed. Access the client via useNuxtApp().$authon instead."
70
+ );
71
+ }
72
+ function useUser() {
73
+ const { user, isLoading } = useAuthon();
74
+ return { user, isLoading };
75
+ }
76
+
77
+ // src/middleware.ts
78
+ function createAuthMiddleware(authon, redirectTo = "/sign-in") {
79
+ return (to, _from) => {
80
+ if (to.path === redirectTo) return void 0;
81
+ if (!authon.isSignedIn) {
82
+ return {
83
+ path: redirectTo,
84
+ query: { redirect: to.fullPath }
85
+ };
86
+ }
87
+ return void 0;
88
+ };
89
+ }
90
+ // Annotate the CommonJS export names for ESM import in node:
91
+ 0 && (module.exports = {
92
+ authonModule,
93
+ createAuthMiddleware,
94
+ createAuthonPlugin,
95
+ useAuthon,
96
+ useUser
97
+ });
98
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/module.ts","../src/plugin.ts","../src/composables.ts","../src/middleware.ts"],"sourcesContent":["export { authonModule as default } from './module';\nexport { authonModule } from './module';\nexport type { AuthonModuleOptions } from './module';\nexport { createAuthonPlugin } from './plugin';\nexport type { AuthonPluginState } from './plugin';\nexport { useAuthon, useUser } from './composables';\nexport { createAuthMiddleware } from './middleware';\n","import type { AuthonConfig } from '@authon/js';\n\nexport interface AuthonModuleOptions {\n publishableKey: string;\n config?: Omit<AuthonConfig, 'mode'>;\n globalMiddleware?: boolean;\n}\n\n/**\n * Nuxt 3 module definition for Authon.\n *\n * Usage in nuxt.config.ts:\n * ```ts\n * export default defineNuxtConfig({\n * modules: ['@authon/nuxt'],\n * authon: {\n * publishableKey: 'pk_live_...',\n * globalMiddleware: false,\n * }\n * })\n * ```\n *\n * Since tsup can't process Nuxt module macros (defineNuxtModule, etc.),\n * this module exports a setup function that users call in their Nuxt plugin.\n */\nexport function authonModule(options: AuthonModuleOptions) {\n return {\n name: '@authon/nuxt',\n options,\n };\n}\n","import { Authon } from '@authon/js';\nimport type { AuthonConfig } from '@authon/js';\nimport type { AuthonUser } from '@authon/shared';\n\nexport interface AuthonPluginState {\n client: Authon;\n user: AuthonUser | null;\n isSignedIn: boolean;\n isLoading: boolean;\n}\n\n/**\n * Creates an Authon client instance for use as a Nuxt plugin.\n *\n * Usage in plugins/authon.client.ts:\n * ```ts\n * import { createAuthonPlugin } from '@authon/nuxt'\n *\n * export default defineNuxtPlugin(() => {\n * const authon = createAuthonPlugin('pk_live_...', { theme: 'auto' })\n * return { provide: { authon } }\n * })\n * ```\n */\nexport function createAuthonPlugin(\n publishableKey: string,\n config?: Omit<AuthonConfig, 'mode'>,\n): AuthonPluginState {\n const client = new Authon(publishableKey, config);\n const state: AuthonPluginState = {\n client,\n user: null,\n isSignedIn: false,\n isLoading: true,\n };\n\n client.on('signedIn', (user) => {\n state.user = user as AuthonUser;\n state.isSignedIn = true;\n state.isLoading = false;\n });\n\n client.on('signedOut', () => {\n state.user = null;\n state.isSignedIn = false;\n });\n\n client.on('error', () => {\n state.isLoading = false;\n });\n\n state.isLoading = false;\n\n return state;\n}\n","import type { AuthonUser } from '@authon/shared';\nimport type { AuthonPluginState } from './plugin';\n\n/**\n * Access the Authon client and auth state.\n *\n * Requires the Authon plugin to be installed via `createAuthonPlugin`.\n *\n * Usage:\n * ```ts\n * const { isSignedIn, user, client } = useAuthon()\n * await client.openSignIn()\n * ```\n */\nexport function useAuthon(): AuthonPluginState {\n // In a real Nuxt app, users access this via useNuxtApp().$authon\n // This composable is a convenience wrapper that users should adapt\n // to their own plugin setup.\n throw new Error(\n 'useAuthon() must be used within a Nuxt app with the Authon plugin installed. ' +\n 'Access the client via useNuxtApp().$authon instead.',\n );\n}\n\n/**\n * Get the current user and loading state.\n *\n * Usage:\n * ```ts\n * const { user, isLoading } = useUser()\n * ```\n */\nexport function useUser(): { user: AuthonUser | null; isLoading: boolean } {\n const { user, isLoading } = useAuthon();\n return { user, isLoading };\n}\n","import type { AuthonPluginState } from './plugin';\n\n/**\n * Route middleware factory for protecting authenticated routes.\n *\n * Usage in middleware/auth.ts:\n * ```ts\n * import { createAuthMiddleware } from '@authon/nuxt'\n *\n * export default defineNuxtRouteMiddleware((to, from) => {\n * const { $authon } = useNuxtApp()\n * return createAuthMiddleware($authon, '/login')(to, from)\n * })\n * ```\n */\nexport function createAuthMiddleware(\n authon: AuthonPluginState,\n redirectTo = '/sign-in',\n) {\n return (\n to: { path: string; fullPath: string },\n _from: { path: string },\n ): { path: string; query: { redirect: string } } | undefined => {\n if (to.path === redirectTo) return undefined;\n\n if (!authon.isSignedIn) {\n return {\n path: redirectTo,\n query: { redirect: to.fullPath },\n };\n }\n\n return undefined;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACyBO,SAAS,aAAa,SAA8B;AACzD,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,EACF;AACF;;;AC9BA,gBAAuB;AAwBhB,SAAS,mBACd,gBACA,QACmB;AACnB,QAAM,SAAS,IAAI,iBAAO,gBAAgB,MAAM;AAChD,QAAM,QAA2B;AAAA,IAC/B;AAAA,IACA,MAAM;AAAA,IACN,YAAY;AAAA,IACZ,WAAW;AAAA,EACb;AAEA,SAAO,GAAG,YAAY,CAAC,SAAS;AAC9B,UAAM,OAAO;AACb,UAAM,aAAa;AACnB,UAAM,YAAY;AAAA,EACpB,CAAC;AAED,SAAO,GAAG,aAAa,MAAM;AAC3B,UAAM,OAAO;AACb,UAAM,aAAa;AAAA,EACrB,CAAC;AAED,SAAO,GAAG,SAAS,MAAM;AACvB,UAAM,YAAY;AAAA,EACpB,CAAC;AAED,QAAM,YAAY;AAElB,SAAO;AACT;;;ACxCO,SAAS,YAA+B;AAI7C,QAAM,IAAI;AAAA,IACR;AAAA,EAEF;AACF;AAUO,SAAS,UAA2D;AACzE,QAAM,EAAE,MAAM,UAAU,IAAI,UAAU;AACtC,SAAO,EAAE,MAAM,UAAU;AAC3B;;;ACpBO,SAAS,qBACd,QACA,aAAa,YACb;AACA,SAAO,CACL,IACA,UAC8D;AAC9D,QAAI,GAAG,SAAS,WAAY,QAAO;AAEnC,QAAI,CAAC,OAAO,YAAY;AACtB,aAAO;AAAA,QACL,MAAM;AAAA,QACN,OAAO,EAAE,UAAU,GAAG,SAAS;AAAA,MACjC;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -0,0 +1,102 @@
1
+ import { AuthonConfig, Authon } from '@authon/js';
2
+ import { AuthonUser } from '@authon/shared';
3
+
4
+ interface AuthonModuleOptions {
5
+ publishableKey: string;
6
+ config?: Omit<AuthonConfig, 'mode'>;
7
+ globalMiddleware?: boolean;
8
+ }
9
+ /**
10
+ * Nuxt 3 module definition for Authon.
11
+ *
12
+ * Usage in nuxt.config.ts:
13
+ * ```ts
14
+ * export default defineNuxtConfig({
15
+ * modules: ['@authon/nuxt'],
16
+ * authon: {
17
+ * publishableKey: 'pk_live_...',
18
+ * globalMiddleware: false,
19
+ * }
20
+ * })
21
+ * ```
22
+ *
23
+ * Since tsup can't process Nuxt module macros (defineNuxtModule, etc.),
24
+ * this module exports a setup function that users call in their Nuxt plugin.
25
+ */
26
+ declare function authonModule(options: AuthonModuleOptions): {
27
+ name: string;
28
+ options: AuthonModuleOptions;
29
+ };
30
+
31
+ interface AuthonPluginState {
32
+ client: Authon;
33
+ user: AuthonUser | null;
34
+ isSignedIn: boolean;
35
+ isLoading: boolean;
36
+ }
37
+ /**
38
+ * Creates an Authon client instance for use as a Nuxt plugin.
39
+ *
40
+ * Usage in plugins/authon.client.ts:
41
+ * ```ts
42
+ * import { createAuthonPlugin } from '@authon/nuxt'
43
+ *
44
+ * export default defineNuxtPlugin(() => {
45
+ * const authon = createAuthonPlugin('pk_live_...', { theme: 'auto' })
46
+ * return { provide: { authon } }
47
+ * })
48
+ * ```
49
+ */
50
+ declare function createAuthonPlugin(publishableKey: string, config?: Omit<AuthonConfig, 'mode'>): AuthonPluginState;
51
+
52
+ /**
53
+ * Access the Authon client and auth state.
54
+ *
55
+ * Requires the Authon plugin to be installed via `createAuthonPlugin`.
56
+ *
57
+ * Usage:
58
+ * ```ts
59
+ * const { isSignedIn, user, client } = useAuthon()
60
+ * await client.openSignIn()
61
+ * ```
62
+ */
63
+ declare function useAuthon(): AuthonPluginState;
64
+ /**
65
+ * Get the current user and loading state.
66
+ *
67
+ * Usage:
68
+ * ```ts
69
+ * const { user, isLoading } = useUser()
70
+ * ```
71
+ */
72
+ declare function useUser(): {
73
+ user: AuthonUser | null;
74
+ isLoading: boolean;
75
+ };
76
+
77
+ /**
78
+ * Route middleware factory for protecting authenticated routes.
79
+ *
80
+ * Usage in middleware/auth.ts:
81
+ * ```ts
82
+ * import { createAuthMiddleware } from '@authon/nuxt'
83
+ *
84
+ * export default defineNuxtRouteMiddleware((to, from) => {
85
+ * const { $authon } = useNuxtApp()
86
+ * return createAuthMiddleware($authon, '/login')(to, from)
87
+ * })
88
+ * ```
89
+ */
90
+ declare function createAuthMiddleware(authon: AuthonPluginState, redirectTo?: string): (to: {
91
+ path: string;
92
+ fullPath: string;
93
+ }, _from: {
94
+ path: string;
95
+ }) => {
96
+ path: string;
97
+ query: {
98
+ redirect: string;
99
+ };
100
+ } | undefined;
101
+
102
+ export { type AuthonModuleOptions, type AuthonPluginState, authonModule, createAuthMiddleware, createAuthonPlugin, authonModule as default, useAuthon, useUser };
@@ -0,0 +1,102 @@
1
+ import { AuthonConfig, Authon } from '@authon/js';
2
+ import { AuthonUser } from '@authon/shared';
3
+
4
+ interface AuthonModuleOptions {
5
+ publishableKey: string;
6
+ config?: Omit<AuthonConfig, 'mode'>;
7
+ globalMiddleware?: boolean;
8
+ }
9
+ /**
10
+ * Nuxt 3 module definition for Authon.
11
+ *
12
+ * Usage in nuxt.config.ts:
13
+ * ```ts
14
+ * export default defineNuxtConfig({
15
+ * modules: ['@authon/nuxt'],
16
+ * authon: {
17
+ * publishableKey: 'pk_live_...',
18
+ * globalMiddleware: false,
19
+ * }
20
+ * })
21
+ * ```
22
+ *
23
+ * Since tsup can't process Nuxt module macros (defineNuxtModule, etc.),
24
+ * this module exports a setup function that users call in their Nuxt plugin.
25
+ */
26
+ declare function authonModule(options: AuthonModuleOptions): {
27
+ name: string;
28
+ options: AuthonModuleOptions;
29
+ };
30
+
31
+ interface AuthonPluginState {
32
+ client: Authon;
33
+ user: AuthonUser | null;
34
+ isSignedIn: boolean;
35
+ isLoading: boolean;
36
+ }
37
+ /**
38
+ * Creates an Authon client instance for use as a Nuxt plugin.
39
+ *
40
+ * Usage in plugins/authon.client.ts:
41
+ * ```ts
42
+ * import { createAuthonPlugin } from '@authon/nuxt'
43
+ *
44
+ * export default defineNuxtPlugin(() => {
45
+ * const authon = createAuthonPlugin('pk_live_...', { theme: 'auto' })
46
+ * return { provide: { authon } }
47
+ * })
48
+ * ```
49
+ */
50
+ declare function createAuthonPlugin(publishableKey: string, config?: Omit<AuthonConfig, 'mode'>): AuthonPluginState;
51
+
52
+ /**
53
+ * Access the Authon client and auth state.
54
+ *
55
+ * Requires the Authon plugin to be installed via `createAuthonPlugin`.
56
+ *
57
+ * Usage:
58
+ * ```ts
59
+ * const { isSignedIn, user, client } = useAuthon()
60
+ * await client.openSignIn()
61
+ * ```
62
+ */
63
+ declare function useAuthon(): AuthonPluginState;
64
+ /**
65
+ * Get the current user and loading state.
66
+ *
67
+ * Usage:
68
+ * ```ts
69
+ * const { user, isLoading } = useUser()
70
+ * ```
71
+ */
72
+ declare function useUser(): {
73
+ user: AuthonUser | null;
74
+ isLoading: boolean;
75
+ };
76
+
77
+ /**
78
+ * Route middleware factory for protecting authenticated routes.
79
+ *
80
+ * Usage in middleware/auth.ts:
81
+ * ```ts
82
+ * import { createAuthMiddleware } from '@authon/nuxt'
83
+ *
84
+ * export default defineNuxtRouteMiddleware((to, from) => {
85
+ * const { $authon } = useNuxtApp()
86
+ * return createAuthMiddleware($authon, '/login')(to, from)
87
+ * })
88
+ * ```
89
+ */
90
+ declare function createAuthMiddleware(authon: AuthonPluginState, redirectTo?: string): (to: {
91
+ path: string;
92
+ fullPath: string;
93
+ }, _from: {
94
+ path: string;
95
+ }) => {
96
+ path: string;
97
+ query: {
98
+ redirect: string;
99
+ };
100
+ } | undefined;
101
+
102
+ export { type AuthonModuleOptions, type AuthonPluginState, authonModule, createAuthMiddleware, createAuthonPlugin, authonModule as default, useAuthon, useUser };
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ // src/module.ts
2
+ function authonModule(options) {
3
+ return {
4
+ name: "@authon/nuxt",
5
+ options
6
+ };
7
+ }
8
+
9
+ // src/plugin.ts
10
+ import { Authon } from "@authon/js";
11
+ function createAuthonPlugin(publishableKey, config) {
12
+ const client = new Authon(publishableKey, config);
13
+ const state = {
14
+ client,
15
+ user: null,
16
+ isSignedIn: false,
17
+ isLoading: true
18
+ };
19
+ client.on("signedIn", (user) => {
20
+ state.user = user;
21
+ state.isSignedIn = true;
22
+ state.isLoading = false;
23
+ });
24
+ client.on("signedOut", () => {
25
+ state.user = null;
26
+ state.isSignedIn = false;
27
+ });
28
+ client.on("error", () => {
29
+ state.isLoading = false;
30
+ });
31
+ state.isLoading = false;
32
+ return state;
33
+ }
34
+
35
+ // src/composables.ts
36
+ function useAuthon() {
37
+ throw new Error(
38
+ "useAuthon() must be used within a Nuxt app with the Authon plugin installed. Access the client via useNuxtApp().$authon instead."
39
+ );
40
+ }
41
+ function useUser() {
42
+ const { user, isLoading } = useAuthon();
43
+ return { user, isLoading };
44
+ }
45
+
46
+ // src/middleware.ts
47
+ function createAuthMiddleware(authon, redirectTo = "/sign-in") {
48
+ return (to, _from) => {
49
+ if (to.path === redirectTo) return void 0;
50
+ if (!authon.isSignedIn) {
51
+ return {
52
+ path: redirectTo,
53
+ query: { redirect: to.fullPath }
54
+ };
55
+ }
56
+ return void 0;
57
+ };
58
+ }
59
+ export {
60
+ authonModule,
61
+ createAuthMiddleware,
62
+ createAuthonPlugin,
63
+ authonModule as default,
64
+ useAuthon,
65
+ useUser
66
+ };
67
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/module.ts","../src/plugin.ts","../src/composables.ts","../src/middleware.ts"],"sourcesContent":["import type { AuthonConfig } from '@authon/js';\n\nexport interface AuthonModuleOptions {\n publishableKey: string;\n config?: Omit<AuthonConfig, 'mode'>;\n globalMiddleware?: boolean;\n}\n\n/**\n * Nuxt 3 module definition for Authon.\n *\n * Usage in nuxt.config.ts:\n * ```ts\n * export default defineNuxtConfig({\n * modules: ['@authon/nuxt'],\n * authon: {\n * publishableKey: 'pk_live_...',\n * globalMiddleware: false,\n * }\n * })\n * ```\n *\n * Since tsup can't process Nuxt module macros (defineNuxtModule, etc.),\n * this module exports a setup function that users call in their Nuxt plugin.\n */\nexport function authonModule(options: AuthonModuleOptions) {\n return {\n name: '@authon/nuxt',\n options,\n };\n}\n","import { Authon } from '@authon/js';\nimport type { AuthonConfig } from '@authon/js';\nimport type { AuthonUser } from '@authon/shared';\n\nexport interface AuthonPluginState {\n client: Authon;\n user: AuthonUser | null;\n isSignedIn: boolean;\n isLoading: boolean;\n}\n\n/**\n * Creates an Authon client instance for use as a Nuxt plugin.\n *\n * Usage in plugins/authon.client.ts:\n * ```ts\n * import { createAuthonPlugin } from '@authon/nuxt'\n *\n * export default defineNuxtPlugin(() => {\n * const authon = createAuthonPlugin('pk_live_...', { theme: 'auto' })\n * return { provide: { authon } }\n * })\n * ```\n */\nexport function createAuthonPlugin(\n publishableKey: string,\n config?: Omit<AuthonConfig, 'mode'>,\n): AuthonPluginState {\n const client = new Authon(publishableKey, config);\n const state: AuthonPluginState = {\n client,\n user: null,\n isSignedIn: false,\n isLoading: true,\n };\n\n client.on('signedIn', (user) => {\n state.user = user as AuthonUser;\n state.isSignedIn = true;\n state.isLoading = false;\n });\n\n client.on('signedOut', () => {\n state.user = null;\n state.isSignedIn = false;\n });\n\n client.on('error', () => {\n state.isLoading = false;\n });\n\n state.isLoading = false;\n\n return state;\n}\n","import type { AuthonUser } from '@authon/shared';\nimport type { AuthonPluginState } from './plugin';\n\n/**\n * Access the Authon client and auth state.\n *\n * Requires the Authon plugin to be installed via `createAuthonPlugin`.\n *\n * Usage:\n * ```ts\n * const { isSignedIn, user, client } = useAuthon()\n * await client.openSignIn()\n * ```\n */\nexport function useAuthon(): AuthonPluginState {\n // In a real Nuxt app, users access this via useNuxtApp().$authon\n // This composable is a convenience wrapper that users should adapt\n // to their own plugin setup.\n throw new Error(\n 'useAuthon() must be used within a Nuxt app with the Authon plugin installed. ' +\n 'Access the client via useNuxtApp().$authon instead.',\n );\n}\n\n/**\n * Get the current user and loading state.\n *\n * Usage:\n * ```ts\n * const { user, isLoading } = useUser()\n * ```\n */\nexport function useUser(): { user: AuthonUser | null; isLoading: boolean } {\n const { user, isLoading } = useAuthon();\n return { user, isLoading };\n}\n","import type { AuthonPluginState } from './plugin';\n\n/**\n * Route middleware factory for protecting authenticated routes.\n *\n * Usage in middleware/auth.ts:\n * ```ts\n * import { createAuthMiddleware } from '@authon/nuxt'\n *\n * export default defineNuxtRouteMiddleware((to, from) => {\n * const { $authon } = useNuxtApp()\n * return createAuthMiddleware($authon, '/login')(to, from)\n * })\n * ```\n */\nexport function createAuthMiddleware(\n authon: AuthonPluginState,\n redirectTo = '/sign-in',\n) {\n return (\n to: { path: string; fullPath: string },\n _from: { path: string },\n ): { path: string; query: { redirect: string } } | undefined => {\n if (to.path === redirectTo) return undefined;\n\n if (!authon.isSignedIn) {\n return {\n path: redirectTo,\n query: { redirect: to.fullPath },\n };\n }\n\n return undefined;\n };\n}\n"],"mappings":";AAyBO,SAAS,aAAa,SAA8B;AACzD,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,EACF;AACF;;;AC9BA,SAAS,cAAc;AAwBhB,SAAS,mBACd,gBACA,QACmB;AACnB,QAAM,SAAS,IAAI,OAAO,gBAAgB,MAAM;AAChD,QAAM,QAA2B;AAAA,IAC/B;AAAA,IACA,MAAM;AAAA,IACN,YAAY;AAAA,IACZ,WAAW;AAAA,EACb;AAEA,SAAO,GAAG,YAAY,CAAC,SAAS;AAC9B,UAAM,OAAO;AACb,UAAM,aAAa;AACnB,UAAM,YAAY;AAAA,EACpB,CAAC;AAED,SAAO,GAAG,aAAa,MAAM;AAC3B,UAAM,OAAO;AACb,UAAM,aAAa;AAAA,EACrB,CAAC;AAED,SAAO,GAAG,SAAS,MAAM;AACvB,UAAM,YAAY;AAAA,EACpB,CAAC;AAED,QAAM,YAAY;AAElB,SAAO;AACT;;;ACxCO,SAAS,YAA+B;AAI7C,QAAM,IAAI;AAAA,IACR;AAAA,EAEF;AACF;AAUO,SAAS,UAA2D;AACzE,QAAM,EAAE,MAAM,UAAU,IAAI,UAAU;AACtC,SAAO,EAAE,MAAM,UAAU;AAC3B;;;ACpBO,SAAS,qBACd,QACA,aAAa,YACb;AACA,SAAO,CACL,IACA,UAC8D;AAC9D,QAAI,GAAG,SAAS,WAAY,QAAO;AAEnC,QAAI,CAAC,OAAO,YAAY;AACtB,aAAO;AAAA,QACL,MAAM;AAAA,QACN,OAAO,EAAE,UAAU,GAAG,SAAS;AAAA,MACjC;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@authon/nuxt",
3
+ "version": "0.1.0",
4
+ "description": "Authon Nuxt 3 module — auto-imported composables and middleware",
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/nuxt"
26
+ },
27
+ "homepage": "https://github.com/mikusnuz/authon-sdk/tree/main/packages/nuxt",
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "keywords": ["authon", "nuxt", "auth", "module"],
32
+ "dependencies": {
33
+ "@authon/js": "workspace:*",
34
+ "@authon/shared": "workspace:*"
35
+ },
36
+ "peerDependencies": {
37
+ "nuxt": "^3.0.0"
38
+ },
39
+ "devDependencies": {
40
+ "tsup": "^8.0.0",
41
+ "typescript": "^5.9.3"
42
+ }
43
+ }