@procedures/better-auth 0.0.0-stage → 0.12.2
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 +21 -0
- package/README.md +76 -2
- package/dist/.tsbuildinfo +1 -0
- package/dist/auth.d.ts +22 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +40 -0
- package/dist/auth.js.map +1 -0
- package/dist/config.d.ts +46 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +44 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/principal-of.d.ts +51 -0
- package/dist/principal-of.d.ts.map +1 -0
- package/dist/principal-of.js +84 -0
- package/dist/principal-of.js.map +1 -0
- package/dist/principals.d.ts +59 -0
- package/dist/principals.d.ts.map +1 -0
- package/dist/principals.js +112 -0
- package/dist/principals.js.map +1 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +63 -3
- package/src/auth.ts +55 -0
- package/src/better-auth.test.ts +274 -0
- package/src/config.ts +77 -0
- package/src/index.ts +5 -0
- package/src/principal-of.ts +104 -0
- package/src/principals.ts +150 -0
- package/src/types.ts +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Mark Russell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,77 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @procedures/better-auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@procedures/better-auth)
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @procedures/better-auth @procedures/client mongodb
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
The sign-in of a procedures server with [Better Auth](https://www.better-auth.com) over MongoDB, and the request hook that gives the principal of each request.
|
|
10
|
+
|
|
11
|
+
- The people sign in with GitHub, Google or a passkey. The app stores no password.
|
|
12
|
+
- Better Auth keeps the sessions in `httpOnly` cookies, and does the OAuth state, PKCE and the CSRF checks.
|
|
13
|
+
- The organizations of Better Auth are the groups of a user.
|
|
14
|
+
- An agent token lets a program act as the agent, or for one user with the rights of that user.
|
|
15
|
+
|
|
16
|
+
The package has no procedures. The package root registers nothing.
|
|
17
|
+
|
|
18
|
+
## The server
|
|
19
|
+
|
|
20
|
+
`betterAuthConfig` reads the environment. `createBetterAuth` makes the server. Give Better Auth its own database: its `user` collection must not mix with the data of the app.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { betterAuthConfig, createBetterAuth } from "@procedures/better-auth";
|
|
24
|
+
import { toNodeHandler } from "better-auth/node";
|
|
25
|
+
|
|
26
|
+
const config = betterAuthConfig(process.env, { port: 9091, trustedOrigins: ["https://app.example.com"], appName: "Example" });
|
|
27
|
+
if (typeof config === "string") throw new Error(config);
|
|
28
|
+
const auth = createBetterAuth(client.db("example-auth"), client, config);
|
|
29
|
+
app.all("/auth/*path", toNodeHandler(auth)); // the routes belong to the app
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
| Variable | Use |
|
|
33
|
+
| --- | --- |
|
|
34
|
+
| `BETTER_AUTH_SECRET` | The secret that signs the session cookies, with 16 characters or more. It is necessary. |
|
|
35
|
+
| `AUTH_BASE_URL` | The public origin of the server. The default is `http://localhost:<port>`. |
|
|
36
|
+
| `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET` | The sign-in with GitHub. |
|
|
37
|
+
| `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET` | The sign-in with Google. |
|
|
38
|
+
| `PASSKEY_RP_ID` | The domain of the passkeys. The default is the host of the first trusted origin. |
|
|
39
|
+
| `AUTH_TEST_ACCOUNTS=1` | Accounts with an email and a password, for the tests. Their email counts as verified. Production refuses it. |
|
|
40
|
+
|
|
41
|
+
## The principal of a request
|
|
42
|
+
|
|
43
|
+
`SessionPrincipals` gives the user of a session cookie: the id, the verified email, the name and the groups. A short memo keeps the boot of a page to one session lookup.
|
|
44
|
+
|
|
45
|
+
`betterAuthPrincipalOf` gives the request hook (`principalOf`) of a procedures server. The app maps each kind of principal to a capability:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { betterAuthPrincipalOf, SessionPrincipals, VIA } from "@procedures/better-auth";
|
|
49
|
+
|
|
50
|
+
const READ_ONLY = { effects: { writes: [] } };
|
|
51
|
+
const principals = new SessionPrincipals({ auth, db: client.db("example-auth") });
|
|
52
|
+
const server = createProcedureServer({
|
|
53
|
+
// No `token`: the hook checks the agent token, and a cookie session has no bearer token
|
|
54
|
+
principalOf: betterAuthPrincipalOf({
|
|
55
|
+
principals,
|
|
56
|
+
agentToken: process.env.AGENT_TOKEN,
|
|
57
|
+
// The agent acts only for users who own no group
|
|
58
|
+
actAs: (user) => !user.groups.some((group) => group.role === "owner"),
|
|
59
|
+
// A visitor without a session can read, and change nothing
|
|
60
|
+
anonymous: READ_ONLY,
|
|
61
|
+
}),
|
|
62
|
+
capability: (request) => (request.principal?.via === VIA.anonymous ? READ_ONLY : {}),
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
| The request has | The principal |
|
|
67
|
+
| --- | --- |
|
|
68
|
+
| The agent token as the bearer token | `{ id: "agent", via: "agent-token" }` |
|
|
69
|
+
| The agent token and the header `x-act-as: <user id>` | The user, with `via: "agent-as"` and the claims `agent: true` and `actAs`. The `actAs` policy must permit the user. Without the policy, the hook refuses the request. |
|
|
70
|
+
| A session cookie | The user, with `via: "better-auth"` and the claims `email`, `name` and `groups` |
|
|
71
|
+
| Nothing of the above | With `anonymous`: `{ id: "anonymous", via: "anonymous" }` and that narrowing. Without it (the default): no principal, and the server refuses the request. |
|
|
72
|
+
|
|
73
|
+
- An act-as request for an unknown user gets the anonymous principal (`via: "anonymous"`), or a refusal without `anonymous`. A refused act-as gets no principal. It does not get the principal of the agent.
|
|
74
|
+
- A bearer token that is not the agent token goes on to the token check of the server.
|
|
75
|
+
- Do not give the server a `token` with this hook. The token check of the server operates after the hook, and a cookie session has no bearer token.
|
|
76
|
+
- The procedures read the principal as `ctx.principal`. The rights of the app stay in the app.
|
|
77
|
+
- The hook compares the agent token in constant time with `tokenMatches` of `@procedures/client`.
|