The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Headers from "effect/http/Headers"; |
| 2 | import * as RpcMiddleware from "effect/rpc/RpcMiddleware"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | import { CurrentActor } from "./CurrentActor.context.ts"; |
| 5 | |
| 6 | /** The request has no session. */ |
| 7 | export class Unauthorized extends Schema.TaggedError<Unauthorized>()("Unauthorized", {}) {} |
| 8 | |
| 9 | /** The cookie that carries a customer's session in a browser. */ |
| 10 | export const SESSION_COOKIE = "bank_session"; |
| 11 | |
| 12 | /** |
| 13 | * Authenticates a request, from a customer or a support agent, and provides |
| 14 | * who is making it as the {@link CurrentActor}. Authorization is up to each |
| 15 | * command's `decide` and each view's `validate`. |
| 16 | * |
| 17 | * Browsers authenticate with the session cookie, which also reaches live |
| 18 | * reads: a WebSocket upgrade carries cookies, never a header a client sets. |
| 19 | * Other clients (tests, support tooling) send `Authorization` instead. |
| 20 | */ |
| 21 | export class Session extends RpcMiddleware.Service<Session, { provides: CurrentActor }>()( |
| 22 | "Bank/Session", |
| 23 | { error: Unauthorized }, |
| 24 | ) {} |
| 25 | |
| 26 | /** Client side, for clients that are not browsers: send this token with every call. */ |
| 27 | export const sessionClient = (token: string) => |
| 28 | RpcMiddleware.layerClient(Session, ({ request, next }) => |
| 29 | next({ ...request, headers: Headers.set(request.headers, "authorization", `Bearer ${token}`) }), |
| 30 | ); |