@vite-hub/auth 0.0.2 → 0.0.4
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/LICENSE +201 -0
- package/README.md +79 -34
- package/dist/agent.d.ts +1 -0
- package/dist/agent.js +1 -1
- package/dist/host-declarations.d.ts +13 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +6 -1
- package/dist/nuxt.d.ts +38 -0
- package/dist/nuxt.js +61 -0
- package/dist/runtime/empty-definition.d.ts +1 -1
- package/dist/runtime/nuxt.d.ts +4 -0
- package/dist/runtime/nuxt.js +8 -0
- package/dist/{server-Diw3mAbX.js → server-BZn8iQrB.js} +51 -3
- package/dist/server.d.ts +5 -2
- package/dist/server.js +2 -2
- package/dist/{types-B0fg9XFd.d.ts → types-jaRlW3D1.d.ts} +12 -1
- package/dist/vite-Cmey8ODS.js +1018 -0
- package/dist/vite.d.ts +18 -3
- package/dist/vite.js +2 -993
- package/dist/vue.d.ts +38 -0
- package/dist/vue.js +34 -0
- package/package.json +20 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -9,48 +9,63 @@
|
|
|
9
9
|
|
|
10
10
|
`@vite-hub/auth` defines one server-owned Auth Definition and turns it into a Better Auth server for ViteHub apps.
|
|
11
11
|
|
|
12
|
+
Most applications install `vite-hub` and enable Auth through the framework preset. Install this owner package directly when a focused Vite integration, library, or manual host needs Auth without the rest of ViteHub. Better Auth still owns providers, client plugins, adapters, and sign-in UI.
|
|
13
|
+
|
|
12
14
|
## Install
|
|
13
15
|
|
|
14
16
|
```sh
|
|
15
17
|
pnpm add @vite-hub/auth better-auth
|
|
18
|
+
pnpm add -D vite
|
|
16
19
|
```
|
|
17
20
|
|
|
18
|
-
##
|
|
21
|
+
## First local session
|
|
19
22
|
|
|
20
23
|
```ts
|
|
21
24
|
// server/auth.ts
|
|
22
|
-
import { defineAuth } from "@vite-hub/auth"
|
|
25
|
+
import { defineAuth } from "@vite-hub/auth";
|
|
23
26
|
|
|
24
27
|
export default defineAuth({
|
|
25
28
|
appName: "My app",
|
|
26
|
-
}
|
|
29
|
+
emailAndPassword: { enabled: true },
|
|
30
|
+
});
|
|
27
31
|
```
|
|
28
32
|
|
|
29
33
|
```ts
|
|
30
34
|
// vite.config.ts
|
|
31
|
-
import { hubAuth } from "@vite-hub/auth/vite"
|
|
32
|
-
import { defineConfig } from "vite"
|
|
35
|
+
import { hubAuth } from "@vite-hub/auth/vite";
|
|
36
|
+
import { defineConfig } from "vite";
|
|
33
37
|
|
|
34
38
|
export default defineConfig({
|
|
35
39
|
plugins: [hubAuth()],
|
|
36
|
-
})
|
|
40
|
+
});
|
|
37
41
|
```
|
|
38
42
|
|
|
39
|
-
|
|
43
|
+
Start the Vite Development Server, then create a local session through the mounted Better Auth route:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
pnpm vite
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
curl -i http://localhost:5173/api/auth/sign-up/email \
|
|
51
|
+
-H 'content-type: application/json' \
|
|
52
|
+
--data '{"name":"Local User","email":"local@example.com","password":"passwordpassword"}'
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Successful output has status `200`, a `better-auth.session_token` cookie, and the new user's email. `hubAuth()` discovers `server/auth.ts`, exposes `/api/auth/**`, and generates the Nitro route handler for production builds.
|
|
56
|
+
|
|
57
|
+
This default proves discovery and the session route locally. It is not durable storage. Configure a concrete Better Auth database adapter before relying on sessions across restarts or replicas.
|
|
40
58
|
|
|
41
59
|
For host routes that need runtime secrets, provider credentials, request-derived origins, or route guarding, keep that policy in the Auth Definition.
|
|
42
60
|
|
|
43
61
|
```ts
|
|
44
62
|
// vite.config.ts
|
|
45
|
-
import { hubAuth } from "@vite-hub/auth/vite"
|
|
46
|
-
import { env, hubEnv } from "@vite-hub/env/vite"
|
|
47
|
-
import { defineConfig } from "vite"
|
|
63
|
+
import { hubAuth } from "@vite-hub/auth/vite";
|
|
64
|
+
import { env, hubEnv } from "@vite-hub/env/vite";
|
|
65
|
+
import { defineConfig } from "vite";
|
|
48
66
|
|
|
49
67
|
export default defineConfig({
|
|
50
|
-
plugins: [
|
|
51
|
-
hubEnv(),
|
|
52
|
-
hubAuth(),
|
|
53
|
-
],
|
|
68
|
+
plugins: [hubEnv(), hubAuth()],
|
|
54
69
|
env: {
|
|
55
70
|
server: {
|
|
56
71
|
auth: {
|
|
@@ -62,12 +77,12 @@ export default defineConfig({
|
|
|
62
77
|
},
|
|
63
78
|
},
|
|
64
79
|
},
|
|
65
|
-
})
|
|
80
|
+
});
|
|
66
81
|
```
|
|
67
82
|
|
|
68
83
|
```ts
|
|
69
84
|
// server/auth.ts
|
|
70
|
-
import { defineAuth } from "@vite-hub/auth"
|
|
85
|
+
import { defineAuth } from "@vite-hub/auth";
|
|
71
86
|
|
|
72
87
|
export default defineAuth(({ env, requestOrigin }) => ({
|
|
73
88
|
appName: "My app",
|
|
@@ -87,12 +102,31 @@ export default defineAuth(({ env, requestOrigin }) => ({
|
|
|
87
102
|
clientSecret: env.auth.github.clientSecret.unseal(),
|
|
88
103
|
},
|
|
89
104
|
},
|
|
90
|
-
}))
|
|
105
|
+
}));
|
|
91
106
|
```
|
|
92
107
|
|
|
93
108
|
When `@vite-hub/env` is installed, the Auth Definition callback receives the typed server env. It also receives `requestOrigin`, so same-origin apps do not need a separate auth URL environment variable.
|
|
94
109
|
|
|
95
|
-
The generated Nitro middleware uses `access.signIn` when one of the configured `access.routes` needs a browser redirect.
|
|
110
|
+
The generated Nitro middleware uses `access.signIn` when one of the configured `access.routes` needs a browser redirect. Route objects can add an `authorize` callback when a valid session is not sufficient:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
export default defineAuth({
|
|
114
|
+
access: {
|
|
115
|
+
routes: [
|
|
116
|
+
{
|
|
117
|
+
route: "/_vitehub/**",
|
|
118
|
+
authorize: ({ user }) => user.isAdmin === true,
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
route: "/api/_vitehub/console/**",
|
|
122
|
+
authorize: ({ user }) => user.isAdmin === true,
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
ViteHub runs `authorize` only after authentication. Return `true` to allow the request, `false` for a `403`, or a `Response` for a custom result. The callback receives the authenticated `user`, `session`, and request; role semantics remain owned by the host.
|
|
96
130
|
|
|
97
131
|
## Vite Integration
|
|
98
132
|
|
|
@@ -103,56 +137,57 @@ Set `route: false` only when a host integration mounts Auth itself.
|
|
|
103
137
|
```ts
|
|
104
138
|
export default defineAuth({
|
|
105
139
|
route: false,
|
|
106
|
-
})
|
|
140
|
+
});
|
|
107
141
|
```
|
|
108
142
|
|
|
109
143
|
Manual hosts can mount the stable `#vitehub/auth/server` handler directly.
|
|
110
144
|
|
|
111
|
-
Better Auth options stay top-level. ViteHub-owned wiring reserves `access`, `database`, `secondaryStorage`, `basePath`, `route`, and `runtime`. The Better Auth fields `baseURL`, `secret`, and `secrets` are runtime-only: return them from the Definition callback or place them under `runtime`. `access.routes` must be static route strings or `{ method, route }` objects so the Vite Integration can register Nitro middleware.
|
|
145
|
+
Better Auth options stay top-level. ViteHub-owned wiring reserves `access`, `database`, `secondaryStorage`, `basePath`, `route`, and `runtime`. The Better Auth fields `baseURL`, `secret`, and `secrets` are runtime-only: return them from the Definition callback or place them under `runtime`. `access.routes` must be static route strings or `{ authorize, method, route }` objects so the Vite Integration can register Nitro middleware.
|
|
112
146
|
|
|
113
147
|
```ts
|
|
114
148
|
export default defineAuth({
|
|
115
149
|
basePath: "/auth",
|
|
116
150
|
database: { name: "auth", dedicated: true },
|
|
117
151
|
secondaryStorage: { store: "auth" },
|
|
118
|
-
})
|
|
152
|
+
});
|
|
119
153
|
```
|
|
120
154
|
|
|
121
155
|
`database` and `secondaryStorage` are placement metadata. ViteHub removes metadata-shaped values before calling `betterAuth()`, so they do not create Database or KV adapters. When Better Auth needs persistent storage, return its concrete `database` or `secondaryStorage` adapter from the Definition callback or `runtime`.
|
|
122
156
|
|
|
123
157
|
## Client-side Auth
|
|
124
158
|
|
|
125
|
-
|
|
159
|
+
Vue apps can use ViteHub's Better Auth client and normalized session composable directly. `useUserSession()` exposes the current `user` and `session`, sign-in and sign-out actions, and `loggedIn`, `pending`, and `ready` computed refs.
|
|
126
160
|
|
|
127
161
|
```ts
|
|
128
162
|
// lib/auth-client.ts
|
|
129
|
-
import {
|
|
163
|
+
import { useUserSession } from "@vite-hub/auth/vue";
|
|
130
164
|
|
|
131
|
-
export const
|
|
165
|
+
export const session = useUserSession();
|
|
132
166
|
```
|
|
133
167
|
|
|
134
168
|
That default talks to `/api/auth` on the same origin. If the Auth Definition uses a custom `basePath`, pass the same path to the Better Auth client.
|
|
135
169
|
|
|
136
170
|
```ts
|
|
137
171
|
// server/auth.ts
|
|
138
|
-
import { defineAuth } from "@vite-hub/auth"
|
|
172
|
+
import { defineAuth } from "@vite-hub/auth";
|
|
139
173
|
|
|
140
174
|
export default defineAuth({
|
|
141
175
|
appName: "My app",
|
|
142
176
|
basePath: "/auth",
|
|
143
|
-
})
|
|
177
|
+
});
|
|
144
178
|
```
|
|
145
179
|
|
|
146
180
|
```ts
|
|
147
181
|
// lib/auth-client.ts
|
|
148
|
-
import { createAuthClient } from "
|
|
182
|
+
import { createAuthClient, useUserSession } from "@vite-hub/auth/vue";
|
|
149
183
|
|
|
150
184
|
export const authClient = createAuthClient({
|
|
151
185
|
basePath: "/auth",
|
|
152
|
-
})
|
|
186
|
+
});
|
|
187
|
+
export const userSession = useUserSession(authClient);
|
|
153
188
|
```
|
|
154
189
|
|
|
155
|
-
|
|
190
|
+
The exported `createAuthClient` keeps Better Auth's option and plugin inference for custom base paths and client plugins. The zero-config composables use ViteHub's shared default client. React, Svelte, Solid, and other framework clients remain available from Better Auth's framework entrypoints.
|
|
156
191
|
|
|
157
192
|
Read [Better Auth's client docs](https://www.better-auth.com/docs/concepts/client) for framework entrypoints and session helpers.
|
|
158
193
|
|
|
@@ -162,15 +197,15 @@ Use `authenticated()` when an Agent should derive its Agent Invoker from the cur
|
|
|
162
197
|
|
|
163
198
|
```ts
|
|
164
199
|
// server/support.agent.ts
|
|
165
|
-
import { defineAgent } from "@vite-hub/agent"
|
|
166
|
-
import { authenticated } from "@vite-hub/auth/agent"
|
|
200
|
+
import { defineAgent } from "@vite-hub/agent";
|
|
201
|
+
import { authenticated } from "@vite-hub/auth/agent";
|
|
167
202
|
|
|
168
203
|
export default defineAgent({
|
|
169
204
|
invoker: authenticated(),
|
|
170
205
|
driver: {
|
|
171
206
|
run: ({ invoker }) => ({ invoker }),
|
|
172
207
|
},
|
|
173
|
-
})
|
|
208
|
+
});
|
|
174
209
|
```
|
|
175
210
|
|
|
176
211
|
By default, the Auth Package reads the same-app Better Auth session from the request and maps the Auth User to an Agent Invoker with `kind: "authUser"`. Customize `id`, `kind`, `label`, or `meta` for normal identity shaping, and use `source` or `map` when the Agent consumes JWTs, bearer tokens, OAuth/OIDC provider output, or product-specific identity.
|
|
@@ -178,9 +213,19 @@ By default, the Auth Package reads the same-app Better Auth session from the req
|
|
|
178
213
|
When authentication is required but unavailable, `authenticated()` throws `ViteHubError` with code `AUTHENTICATION_REQUIRED`. HTTP adapters map that code to `401`; the transport status is not part of the error object.
|
|
179
214
|
|
|
180
215
|
```ts
|
|
181
|
-
import { ViteHubError } from "@vite-hub/runtime"
|
|
216
|
+
import { ViteHubError } from "@vite-hub/runtime";
|
|
182
217
|
|
|
183
|
-
const error = new ViteHubError("AUTHENTICATION_REQUIRED", "Sign in to use this Agent.")
|
|
218
|
+
const error = new ViteHubError("AUTHENTICATION_REQUIRED", "Sign in to use this Agent.");
|
|
184
219
|
```
|
|
185
220
|
|
|
186
221
|
`error.toJSON()` includes `name`, `code`, and `message`, while omitting `cause` and stack data. If a default Better Auth request or session operation fails, `authenticated()` and `requireAuth()` throw the same shared error with code `AUTH_PROVIDER_OPERATION_FAILED` and safe operation details; raw provider diagnostics remain available only through `cause`. Existing ViteHub errors and structural `AbortError` objects keep their identity. Missing APIs, malformed responses, invalid Auth configuration, and invalid custom callback results are programmer or provider-contract defects, so ViteHub keeps those `TypeError`s outside the operational provider boundary.
|
|
222
|
+
|
|
223
|
+
## Production checks
|
|
224
|
+
|
|
225
|
+
- Supply a strong Better Auth secret from Server Env at request time; never put it in Public Env, client code, Agent input, logs, or traces.
|
|
226
|
+
- Set request-aware `baseURL` or use `requestOrigin` so callback and cookie origins match the deployed host.
|
|
227
|
+
- Configure a concrete Better Auth database adapter and migrations. ViteHub `database` and `secondaryStorage` fields are placement metadata, not adapters.
|
|
228
|
+
- Protect application and Console page/API routes explicitly. Authentication proves a session; application-owned `authorize` callbacks define roles and permissions.
|
|
229
|
+
- Keep automatic `/api/auth/**` exposure enabled unless the host mounts the stable handler itself, and verify the chosen `basePath` on both server and client.
|
|
230
|
+
|
|
231
|
+
Read the complete [Auth guide](https://vitehub.dev/docs/server-primitives/auth), [Auth Users and Agent Invokers](https://vitehub.dev/docs/concepts/auth-users-and-agent-invokers), the [host support matrix](https://vitehub.dev/docs/frameworks-hosts/support-matrix), and the project's [pre-1.0 security policy](https://github.com/vite-hub/vitehub/blob/main/SECURITY.md).
|
package/dist/agent.d.ts
CHANGED
package/dist/agent.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _ as
|
|
1
|
+
import { _ as getAuthenticationSession, u as getAuthForRequest, v as invalidAuthenticationErrorOptions, y as throwAuthenticationProviderError } from "./server-BZn8iQrB.js";
|
|
2
2
|
import { ViteHubError } from "@vite-hub/runtime";
|
|
3
3
|
//#region src/agent.ts
|
|
4
4
|
function authenticationRequired(message = "[vitehub] Authentication required.") {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference path="./host-declarations.d.ts" />
|
|
2
|
+
import { A as AuthViteHubOptions, C as AuthRuntimeOnlyOptions, D as AuthSecondaryStorageConfiguration, E as AuthRuntimeOptionsResolver, F as ViteHubAuth, M as ResolvedAuthDatabaseConfiguration, N as ResolvedAuthSecondaryStorageConfiguration, O as AuthSecondaryStorageReference, P as ResolvedAuthViteConfig, S as AuthRuntimeEnv, T as AuthRuntimeOptions, a as AuthAccessRoute, b as AuthRuntimeConfiguration, c as AuthBetterAuthRuntimeOptions, d as AuthDefinition, f as AuthDefinitionInput, g as AuthRequest, h as AuthModuleOptions, i as AuthAccessConfiguration, j as DiscoveredAuthDefinition, k as AuthSignInConfiguration, l as AuthDatabaseConfiguration, m as AuthDefinitionResolver, n as AuthAccessAuthorize, o as AuthAccessRouteConfiguration, p as AuthDefinitionOptions, r as AuthAccessAuthorizeResult, s as AuthBetterAuthOptions, t as AuthAccessAuthorizationContext, u as AuthDatabaseReference, v as AuthReservedOption, w as AuthRuntimeOption, x as AuthRuntimeContext, y as AuthResolvedDefinitionOptions } from "./types-jaRlW3D1.js";
|
|
2
3
|
|
|
3
4
|
//#region src/definition.d.ts
|
|
4
5
|
declare function defineAuth<const TOptions extends AuthBetterAuthOptions>(options: AuthDefinitionOptions<TOptions>): AuthDefinition<AuthDefinitionOptions<TOptions>>;
|
|
5
6
|
declare function defineAuth<const TOptions extends AuthResolvedDefinitionOptions>(options: AuthDefinitionResolver<TOptions>): AuthDefinition<AuthDefinitionResolver<TOptions>>;
|
|
6
7
|
//#endregion
|
|
7
|
-
export { type AuthAccessConfiguration, type AuthAccessRoute, type AuthAccessRouteConfiguration, type AuthBetterAuthOptions, type AuthBetterAuthRuntimeOptions, type AuthDatabaseConfiguration, type AuthDatabaseReference, type AuthDefinition, type AuthDefinitionInput, type AuthDefinitionOptions, type AuthDefinitionResolver, type AuthModuleOptions, type AuthRequest, type AuthReservedOption, type AuthResolvedDefinitionOptions, type AuthRuntimeConfiguration, type AuthRuntimeContext, type AuthRuntimeEnv, type AuthRuntimeOnlyOptions, type AuthRuntimeOption, type AuthRuntimeOptions, type AuthRuntimeOptionsResolver, type AuthSecondaryStorageConfiguration, type AuthSecondaryStorageReference, type AuthSignInConfiguration, type AuthViteHubOptions, type DiscoveredAuthDefinition, type ResolvedAuthDatabaseConfiguration, type ResolvedAuthSecondaryStorageConfiguration, type ResolvedAuthViteConfig, type ViteHubAuth, defineAuth };
|
|
8
|
+
export { type AuthAccessAuthorizationContext, type AuthAccessAuthorize, type AuthAccessAuthorizeResult, type AuthAccessConfiguration, type AuthAccessRoute, type AuthAccessRouteConfiguration, type AuthBetterAuthOptions, type AuthBetterAuthRuntimeOptions, type AuthDatabaseConfiguration, type AuthDatabaseReference, type AuthDefinition, type AuthDefinitionInput, type AuthDefinitionOptions, type AuthDefinitionResolver, type AuthModuleOptions, type AuthRequest, type AuthReservedOption, type AuthResolvedDefinitionOptions, type AuthRuntimeConfiguration, type AuthRuntimeContext, type AuthRuntimeEnv, type AuthRuntimeOnlyOptions, type AuthRuntimeOption, type AuthRuntimeOptions, type AuthRuntimeOptionsResolver, type AuthSecondaryStorageConfiguration, type AuthSecondaryStorageReference, type AuthSignInConfiguration, type AuthViteHubOptions, type DiscoveredAuthDefinition, type ResolvedAuthDatabaseConfiguration, type ResolvedAuthSecondaryStorageConfiguration, type ResolvedAuthViteConfig, type ViteHubAuth, defineAuth };
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,11 @@ const authRuntimeOptionNames = new Set([
|
|
|
5
5
|
"secret",
|
|
6
6
|
"secrets"
|
|
7
7
|
]);
|
|
8
|
-
const accessRouteKeys = new Set([
|
|
8
|
+
const accessRouteKeys = new Set([
|
|
9
|
+
"authorize",
|
|
10
|
+
"method",
|
|
11
|
+
"route"
|
|
12
|
+
]);
|
|
9
13
|
const databaseReferenceKeys = new Set(["dedicated", "name"]);
|
|
10
14
|
const secondaryStorageReferenceKeys = new Set(["store"]);
|
|
11
15
|
function isPlainObject(value) {
|
|
@@ -54,6 +58,7 @@ function validateAuthAccessRoute(value, label) {
|
|
|
54
58
|
const unknownKey = Object.keys(value).find((key) => !accessRouteKeys.has(key));
|
|
55
59
|
if (unknownKey) throw new TypeError(`\`defineAuth()\` ${label} does not support the "${unknownKey}" option.`);
|
|
56
60
|
validateRouteValue(value.route, `${label}.route`);
|
|
61
|
+
if (typeof value.authorize !== "undefined" && typeof value.authorize !== "function") throw new TypeError(`\`defineAuth()\` ${label}.authorize must be a function.`);
|
|
57
62
|
if (typeof value.method !== "undefined") validateStringValue(value.method, `${label}.method`);
|
|
58
63
|
}
|
|
59
64
|
function validateAuthAccess(value) {
|
package/dist/nuxt.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/// <reference path="./host-declarations.d.ts" />
|
|
2
|
+
import { h as AuthModuleOptions } from "./types-jaRlW3D1.js";
|
|
3
|
+
|
|
4
|
+
//#region src/nuxt.d.ts
|
|
5
|
+
interface AuthNuxtModuleOptions {
|
|
6
|
+
auth?: AuthModuleOptions;
|
|
7
|
+
env?: false | {
|
|
8
|
+
projectRoot?: string;
|
|
9
|
+
};
|
|
10
|
+
importsFrom?: string;
|
|
11
|
+
nitro?: boolean;
|
|
12
|
+
}
|
|
13
|
+
type NuxtLike = {
|
|
14
|
+
hook?: (name: "nitro:config", handler: (nitroConfig: Record<string, unknown>) => Promise<void>) => void;
|
|
15
|
+
options: {
|
|
16
|
+
alias?: Record<string, string>;
|
|
17
|
+
imports?: {
|
|
18
|
+
imports?: Array<{
|
|
19
|
+
from: string;
|
|
20
|
+
name: string;
|
|
21
|
+
}>;
|
|
22
|
+
};
|
|
23
|
+
nitro?: {
|
|
24
|
+
alias?: Record<string, string>;
|
|
25
|
+
plugins?: string[];
|
|
26
|
+
};
|
|
27
|
+
rootDir?: string;
|
|
28
|
+
serverDir?: string;
|
|
29
|
+
vite?: {
|
|
30
|
+
auth?: AuthModuleOptions;
|
|
31
|
+
plugins?: unknown[];
|
|
32
|
+
root?: string;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
declare function hubAuthNuxt(options?: AuthNuxtModuleOptions, nuxt?: NuxtLike): void;
|
|
37
|
+
//#endregion
|
|
38
|
+
export { AuthNuxtModuleOptions, hubAuthNuxt as default };
|
package/dist/nuxt.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { a as hubAuth, i as createAuthNitroConfig } from "./vite-Cmey8ODS.js";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { createEnvImportAliases, hubEnv } from "@vite-hub/env/vite";
|
|
5
|
+
//#region src/nuxt.ts
|
|
6
|
+
const composables = [
|
|
7
|
+
"useAuthClient",
|
|
8
|
+
"useSession",
|
|
9
|
+
"useSignIn",
|
|
10
|
+
"useSignUp",
|
|
11
|
+
"useUserSession"
|
|
12
|
+
];
|
|
13
|
+
const nitroRegistered = /* @__PURE__ */ new WeakSet();
|
|
14
|
+
function hubAuthNuxt(options = {}, nuxt) {
|
|
15
|
+
if (!nuxt) return;
|
|
16
|
+
nuxt.options.vite ??= {};
|
|
17
|
+
nuxt.options.vite.plugins ??= [];
|
|
18
|
+
const vitePlugins = nuxt.options.vite.plugins.flat(Infinity);
|
|
19
|
+
const viteRoot = nuxt.options.vite.root || nuxt.options.rootDir || process.cwd();
|
|
20
|
+
const configuredEnvProjectRoot = resolve(viteRoot, options.env === false ? "." : options.env?.projectRoot || ".");
|
|
21
|
+
const existingEnvPlugin = vitePlugins.find((plugin) => plugin?.name === "@vite-hub/env/vite");
|
|
22
|
+
const envPlugin = existingEnvPlugin || (options.env === false ? void 0 : hubEnv(options.env));
|
|
23
|
+
const envProjectRoot = envPlugin?.api.resolveProjectRoot(viteRoot) || configuredEnvProjectRoot;
|
|
24
|
+
if (options.env !== false && options.env?.projectRoot && existingEnvPlugin && envProjectRoot !== configuredEnvProjectRoot) throw new TypeError("`@vite-hub/auth/nuxt` env.projectRoot must match the installed `@vite-hub/env/vite` plugin.");
|
|
25
|
+
if (options.env !== false && !existingEnvPlugin) nuxt.options.vite.plugins.push(envPlugin);
|
|
26
|
+
const authPlugin = vitePlugins.find((plugin) => plugin?.name === "@vite-hub/auth/vite") || hubAuth(options.auth);
|
|
27
|
+
if (!vitePlugins.some((plugin) => plugin?.name === "@vite-hub/auth/vite")) nuxt.options.vite.plugins.push(authPlugin);
|
|
28
|
+
if (options.nitro !== false && !nitroRegistered.has(nuxt)) {
|
|
29
|
+
nitroRegistered.add(nuxt);
|
|
30
|
+
nuxt.hook?.("nitro:config", async (nitroConfig) => {
|
|
31
|
+
Object.assign(nitroConfig, createAuthNitroConfig(authPlugin, {
|
|
32
|
+
nitro: nitroConfig,
|
|
33
|
+
projectRoot: viteRoot,
|
|
34
|
+
serverDirs: nuxt.options.serverDir ? [nuxt.options.serverDir] : void 0,
|
|
35
|
+
viteAuth: nuxt.options.vite?.auth
|
|
36
|
+
}));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
nuxt.options.imports ??= {};
|
|
40
|
+
nuxt.options.imports.imports ??= [];
|
|
41
|
+
for (const name of composables) if (!nuxt.options.imports.imports.some((entry) => entry.name === name)) nuxt.options.imports.imports.push({
|
|
42
|
+
from: options.importsFrom || "@vite-hub/auth/vue",
|
|
43
|
+
name
|
|
44
|
+
});
|
|
45
|
+
if (options.env === false) return;
|
|
46
|
+
const envAliases = createEnvImportAliases({ projectRoot: envProjectRoot });
|
|
47
|
+
const runtimePlugin = fileURLToPath(new URL("./runtime/nuxt.js", import.meta.url));
|
|
48
|
+
nuxt.options.alias = {
|
|
49
|
+
...envAliases,
|
|
50
|
+
...nuxt.options.alias
|
|
51
|
+
};
|
|
52
|
+
nuxt.options.nitro ??= {};
|
|
53
|
+
nuxt.options.nitro.alias = {
|
|
54
|
+
...envAliases,
|
|
55
|
+
...nuxt.options.nitro.alias
|
|
56
|
+
};
|
|
57
|
+
nuxt.options.nitro.plugins ??= [];
|
|
58
|
+
if (!nuxt.options.nitro.plugins.includes(runtimePlugin)) nuxt.options.nitro.plugins.push(runtimePlugin);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { hubAuthNuxt as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { useServerEnv } from "#vitehub/env/server";
|
|
2
|
+
import { setAuthRuntimeEnvResolver } from "@vite-hub/auth/server";
|
|
3
|
+
//#region src/runtime/nuxt.ts
|
|
4
|
+
function viteHubAuthNuxtRuntime() {
|
|
5
|
+
setAuthRuntimeEnvResolver(useServerEnv);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { viteHubAuthNuxtRuntime as default };
|