@@ -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+
175209export 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 */
190224export 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 > &
0 commit comments