Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 66c16d1

Browse files
committed
chore: split out onauth type from main config (#1023)
1 parent 4c9140a commit 66c16d1

File tree

3 files changed

+50
-41
lines changed

3 files changed

+50
-41
lines changed

packages/core/src/actor/config.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,40 @@ type CreateVars<S, CP, CS, V, I, AD, DB> =
172172
}
173173
| Record<never, never>;
174174

175+
// Creates auth config
176+
//
177+
// This must have only one or the other or else AD will not be able to be inferred
178+
type OnAuth<CP, AD> =
179+
| {
180+
/**
181+
* Called on the HTTP server before clients can interact with the actor.
182+
*
183+
* Only called for public endpoints. Calls to actors from within the backend
184+
* do not trigger this handler.
185+
*
186+
* Data returned from this handler will be available on `c.conn.auth`.
187+
*
188+
* This function is required for any public HTTP endpoint access. Use this hook
189+
* to validate client credentials and return authentication data that will be
190+
* available on connections. This runs on the HTTP server (not the actor)
191+
* in order to reduce load on the actor & prevent denial of server attacks
192+
* against individual actors.
193+
*
194+
* If you need access to actor state for authentication, use onBeforeConnect
195+
* with an empty onAuth function instead.
196+
*
197+
* You can also provide your own authentication middleware on your router if you
198+
* choose, then use onAuth to pass the authentication data (e.g. user ID) to the
199+
* actor itself.
200+
*
201+
* @param opts Authentication options including request and intent
202+
* @returns Authentication data to attach to connections (must be serializable)
203+
* @throws Throw an error to deny access to the actor
204+
*/
205+
onAuth: (opts: OnAuthOptions<CP>) => AD | Promise<AD>;
206+
}
207+
| Record<never, never>;
208+
175209
export interface Actions<S, CP, CS, V, I, AD, DB> {
176210
[Action: string]: (
177211
c: ActionContext<S, CP, CS, V, I, AD, DB>,
@@ -189,7 +223,7 @@ export interface Actions<S, CP, CS, V, I, AD, DB> {
189223
*/
190224
export type AuthIntent = "get" | "create" | "connect" | "action" | "message";
191225

192-
interface OnAuthOptions<CP> {
226+
export interface OnAuthOptions<CP = unknown> {
193227
req: Request;
194228
/**
195229
* @experimental
@@ -208,33 +242,6 @@ interface BaseActorConfig<
208242
DB,
209243
R extends Actions<S, CP, CS, V, I, AD, DB>,
210244
> {
211-
/**
212-
* Called on the HTTP server before clients can interact with the actor.
213-
*
214-
* Only called for public endpoints. Calls to actors from within the backend
215-
* do not trigger this handler.
216-
*
217-
* Data returned from this handler will be available on `c.conn.auth`.
218-
*
219-
* This function is required for any public HTTP endpoint access. Use this hook
220-
* to validate client credentials and return authentication data that will be
221-
* available on connections. This runs on the HTTP server (not the actor)
222-
* in order to reduce load on the actor & prevent denial of server attacks
223-
* against individual actors.
224-
*
225-
* If you need access to actor state for authentication, use onBeforeConnect
226-
* with an empty onAuth function instead.
227-
*
228-
* You can also provide your own authentication middleware on your router if you
229-
* choose, then use onAuth to pass the authentication data (e.g. user ID) to the
230-
* actor itself.
231-
*
232-
* @param opts Authentication options including request and intent
233-
* @returns Authentication data to attach to connections (must be serializable)
234-
* @throws Throw an error to deny access to the actor
235-
*/
236-
onAuth?: (opts: OnAuthOptions<CP>) => AD | Promise<AD>;
237-
238245
/**
239246
* Called when the actor is first initialized.
240247
*
@@ -389,6 +396,7 @@ export type ActorConfig<S, CP, CS, V, I, AD, DB> = Omit<
389396
| "db"
390397
> &
391398
BaseActorConfig<S, CP, CS, V, I, AD, DB, Actions<S, CP, CS, V, I, AD, DB>> &
399+
OnAuth<CP, AD> &
392400
CreateState<S, CP, CS, V, I, AD, DB> &
393401
CreateConnState<S, CP, CS, V, I, AD, DB> &
394402
CreateVars<S, CP, CS, V, I, AD, DB> &
@@ -424,6 +432,7 @@ export type ActorConfigInput<
424432
| "db"
425433
> &
426434
BaseActorConfig<S, CP, CS, V, I, AD, DB, R> &
435+
OnAuth<CP, AD> &
427436
CreateState<S, CP, CS, V, I, AD, DB> &
428437
CreateConnState<S, CP, CS, V, I, AD, DB> &
429438
CreateVars<S, CP, CS, V, I, AD, DB> &

packages/core/src/actor/mod.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ import {
66
} from "./config";
77
import { ActorDefinition } from "./definition";
88

9-
export type { ActorContext } from "./context";
10-
export { UserError, type UserErrorOptions } from "./errors";
11-
export type { Conn } from "./connection";
12-
export type { ActionContext } from "./action";
13-
export type { ActorConfig, OnConnectOptions } from "./config";
14-
export type { Encoding } from "@/actor/protocol/serde";
15-
export type {
16-
ActorDefinition,
17-
AnyActorDefinition,
18-
ActorContextOf,
19-
ActionContextOf,
20-
} from "./definition";
219

2210
export function actor<
2311
S,
@@ -43,3 +31,15 @@ export function actor<
4331
return new ActorDefinition(config);
4432
}
4533
export type { ActorKey } from "@/manager/protocol/query";
34+
export type { ActorContext } from "./context";
35+
export { UserError, type UserErrorOptions } from "./errors";
36+
export type { Conn } from "./connection";
37+
export type { ActionContext } from "./action";
38+
export type * from "./config";
39+
export type { Encoding } from "@/actor/protocol/serde";
40+
export type {
41+
ActorDefinition,
42+
AnyActorDefinition,
43+
ActorContextOf,
44+
ActionContextOf,
45+
} from "./definition";

packages/core/src/manager/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export async function authenticateRequest(
6464
intents: Set<AuthIntent>,
6565
params: unknown,
6666
): Promise<unknown> {
67-
if (!actorDefinition.config.onAuth) {
67+
if (!("onAuth" in actorDefinition.config)) {
6868
throw new errors.Forbidden(
6969
"Actor requires authentication but no onAuth handler is defined",
7070
);

0 commit comments

Comments
 (0)