@beignet/provider-auth-better-auth 0.0.12 → 0.0.13
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/CHANGELOG.md +6 -0
- package/README.md +76 -70
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -65,7 +65,10 @@ application's ports type:
|
|
|
65
65
|
|
|
66
66
|
```ts
|
|
67
67
|
// ports/auth.ts
|
|
68
|
-
import type {
|
|
68
|
+
import type {
|
|
69
|
+
AuthPort as BeignetAuthPort,
|
|
70
|
+
AuthSession as BeignetAuthSession,
|
|
71
|
+
} from "@beignet/core/ports";
|
|
69
72
|
|
|
70
73
|
export type AuthUser = {
|
|
71
74
|
id: string;
|
|
@@ -77,14 +80,21 @@ export type AuthUser = {
|
|
|
77
80
|
export type AuthSessionMetadata = unknown;
|
|
78
81
|
|
|
79
82
|
export type AuthPort = BeignetAuthPort<AuthUser, AuthSessionMetadata>;
|
|
83
|
+
export type AuthSession = BeignetAuthSession<AuthUser, AuthSessionMetadata>;
|
|
80
84
|
```
|
|
81
85
|
|
|
82
86
|
```ts
|
|
83
87
|
// ports/index.ts
|
|
88
|
+
import type { ActivityActor, GatePort } from "@beignet/core/ports";
|
|
84
89
|
import type { AuthPort } from "./auth";
|
|
85
90
|
|
|
91
|
+
export type AppAuthorizationContext = {
|
|
92
|
+
actor: ActivityActor;
|
|
93
|
+
};
|
|
94
|
+
|
|
86
95
|
export type AppPorts = {
|
|
87
96
|
auth: AuthPort;
|
|
97
|
+
gate: GatePort<AppAuthorizationContext, []>;
|
|
88
98
|
// ...other ports (db, mailer, eventBus, etc.)
|
|
89
99
|
};
|
|
90
100
|
```
|
|
@@ -105,42 +115,72 @@ export const providers = [
|
|
|
105
115
|
```
|
|
106
116
|
|
|
107
117
|
```ts
|
|
108
|
-
//
|
|
109
|
-
import {
|
|
110
|
-
import {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
// app-context.ts
|
|
119
|
+
import type { ActivityActor, BoundGate } from "@beignet/core/ports";
|
|
120
|
+
import type { InferProviderPorts } from "@beignet/core/providers";
|
|
121
|
+
import type { TraceContext } from "@beignet/core/tracing";
|
|
122
|
+
import type { AppPorts } from "@/ports";
|
|
123
|
+
import type { AuthSession } from "@/ports/auth";
|
|
124
|
+
import type { providers } from "@/server/providers";
|
|
125
|
+
|
|
126
|
+
export type AppRuntimePorts = AppPorts & InferProviderPorts<typeof providers>;
|
|
127
|
+
|
|
128
|
+
export type AppContext = {
|
|
129
|
+
actor: ActivityActor;
|
|
130
|
+
auth: AuthSession | null;
|
|
131
|
+
gate: BoundGate<[]>;
|
|
132
|
+
requestId: string;
|
|
133
|
+
ports: AppRuntimePorts;
|
|
134
|
+
} & Partial<TraceContext>;
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
// infra/app-ports.ts
|
|
139
|
+
import { createGate, definePorts } from "@beignet/core/ports";
|
|
140
|
+
import type { AppPorts } from "@/ports";
|
|
119
141
|
|
|
120
|
-
// App-owned authorization gate. Register feature policies here.
|
|
121
142
|
const gate = createGate({ policies: [] });
|
|
122
143
|
|
|
123
|
-
const appPorts = definePorts({
|
|
124
|
-
gate,
|
|
125
|
-
|
|
144
|
+
export const appPorts = definePorts<AppPorts>()({
|
|
145
|
+
bound: { gate },
|
|
146
|
+
deferred: ["auth"],
|
|
126
147
|
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
// server/context.ts
|
|
152
|
+
import { createAnonymousActor, createUserActor } from "@beignet/core/ports";
|
|
153
|
+
import { defineServerContext } from "@beignet/core/server";
|
|
154
|
+
import type { AppContext, AppRuntimePorts } from "@/app-context";
|
|
155
|
+
|
|
156
|
+
export const appContext = defineServerContext<AppContext, AppRuntimePorts>()({
|
|
157
|
+
gate: (ports) => ports.gate,
|
|
158
|
+
request: async ({ req, ports, requestId, trace }) => {
|
|
159
|
+
const auth = await ports.auth.getSession(req);
|
|
127
160
|
|
|
128
|
-
|
|
161
|
+
return {
|
|
162
|
+
actor: auth ? createUserActor(auth.user.id) : createAnonymousActor(),
|
|
163
|
+
auth,
|
|
164
|
+
requestId,
|
|
165
|
+
...trace,
|
|
166
|
+
ports,
|
|
167
|
+
};
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
// server/index.ts
|
|
174
|
+
import { createNextServer } from "@beignet/next";
|
|
175
|
+
import { appPorts } from "@/infra/app-ports";
|
|
176
|
+
import { appContext } from "@/server/context";
|
|
177
|
+
import { routes } from "@/server/routes";
|
|
178
|
+
import { providers } from "./providers";
|
|
179
|
+
|
|
180
|
+
export const server = await createNextServer({
|
|
129
181
|
ports: appPorts,
|
|
130
182
|
providers,
|
|
131
|
-
context:
|
|
132
|
-
gate: (ports) => ports.gate,
|
|
133
|
-
request: async ({ req, ports }) => {
|
|
134
|
-
const auth = await ports.auth.getSession(req);
|
|
135
|
-
|
|
136
|
-
return {
|
|
137
|
-
actor: auth ? createUserActor(auth.user.id) : createAnonymousActor(),
|
|
138
|
-
auth,
|
|
139
|
-
requestId: req.headers.get("x-request-id") ?? crypto.randomUUID(),
|
|
140
|
-
ports,
|
|
141
|
-
};
|
|
142
|
-
},
|
|
143
|
-
},
|
|
183
|
+
context: appContext,
|
|
144
184
|
routes,
|
|
145
185
|
});
|
|
146
186
|
```
|
|
@@ -396,39 +436,17 @@ See the [Better Auth documentation](https://better-auth.com) for details on rout
|
|
|
396
436
|
```ts
|
|
397
437
|
import { betterAuth } from "better-auth";
|
|
398
438
|
import { createNextServer } from "@beignet/next";
|
|
399
|
-
import {
|
|
400
|
-
createAnonymousActor,
|
|
401
|
-
createGate,
|
|
402
|
-
createUserActor,
|
|
403
|
-
definePorts,
|
|
404
|
-
} from "@beignet/core/ports";
|
|
405
439
|
import { createAuthBetterAuthProvider } from "@beignet/provider-auth-better-auth";
|
|
406
|
-
import
|
|
440
|
+
import { appPorts } from "@/infra/app-ports";
|
|
441
|
+
import { appContext } from "@/server/context";
|
|
407
442
|
import { routes } from "@/server/routes";
|
|
408
443
|
|
|
409
444
|
const auth = betterAuth({ database: db });
|
|
410
445
|
|
|
411
|
-
|
|
412
|
-
const gate = createGate({ policies: [] });
|
|
413
|
-
|
|
414
|
-
const appPorts = definePorts({ gate });
|
|
415
|
-
|
|
416
|
-
const server = await createNextServer<AppContext>({
|
|
446
|
+
const server = await createNextServer({
|
|
417
447
|
ports: appPorts,
|
|
418
448
|
providers: [createAuthBetterAuthProvider(auth)],
|
|
419
|
-
context:
|
|
420
|
-
gate: (ports) => ports.gate,
|
|
421
|
-
request: async ({ req, ports }) => {
|
|
422
|
-
const auth = await ports.auth.getSession(req);
|
|
423
|
-
|
|
424
|
-
return {
|
|
425
|
-
actor: auth ? createUserActor(auth.user.id) : createAnonymousActor(),
|
|
426
|
-
auth,
|
|
427
|
-
requestId: req.headers.get("x-request-id") ?? crypto.randomUUID(),
|
|
428
|
-
ports,
|
|
429
|
-
};
|
|
430
|
-
},
|
|
431
|
-
},
|
|
449
|
+
context: appContext,
|
|
432
450
|
routes,
|
|
433
451
|
});
|
|
434
452
|
```
|
|
@@ -449,23 +467,11 @@ const authHook = {
|
|
|
449
467
|
},
|
|
450
468
|
};
|
|
451
469
|
|
|
452
|
-
const server = await createNextServer
|
|
470
|
+
const server = await createNextServer({
|
|
453
471
|
ports: appPorts,
|
|
454
472
|
providers: [createAuthBetterAuthProvider(auth)],
|
|
455
473
|
hooks: [authHook],
|
|
456
|
-
context:
|
|
457
|
-
gate: (ports) => ports.gate,
|
|
458
|
-
request: async ({ req, ports }) => {
|
|
459
|
-
const auth = await ports.auth.getSession(req);
|
|
460
|
-
|
|
461
|
-
return {
|
|
462
|
-
actor: auth ? createUserActor(auth.user.id) : createAnonymousActor(),
|
|
463
|
-
auth,
|
|
464
|
-
requestId: req.headers.get("x-request-id") ?? crypto.randomUUID(),
|
|
465
|
-
ports,
|
|
466
|
-
};
|
|
467
|
-
},
|
|
468
|
-
},
|
|
474
|
+
context: appContext,
|
|
469
475
|
routes,
|
|
470
476
|
});
|
|
471
477
|
```
|
package/package.json
CHANGED