Skip to content

Commit 76ad99b

Browse files
committed
fix: pass auth token to browser WebSocket connection
When the server is started with --auth-token, the browser client now passes the token via query parameter (?token=...) when connecting to the ORPC WebSocket endpoint. The token is read from VITE_AUTH_TOKEN environment variable at build time. Change-Id: Ic0e253cf128cae361af5032b0dbc0decb92dbd5f Signed-off-by: Thomas Kosiewski <tk@coder.com>
1 parent 1531544 commit 76ad99b

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/browser/orpc/react.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ export const ORPCProvider = (props: ORPCProviderProps) => {
5757
const API_BASE = import.meta.env.VITE_BACKEND_URL ?? window.location.origin;
5858
const WS_BASE = API_BASE.replace("http://", "ws://").replace("https://", "wss://");
5959

60-
const ws = new WebSocket(`${WS_BASE}/orpc/ws`);
60+
// Build WebSocket URL, including auth token if provided via env var
61+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
62+
// @ts-ignore - import.meta is available in Vite
63+
const authToken: string | undefined = import.meta.env.VITE_AUTH_TOKEN;
64+
const wsUrl = authToken
65+
? `${WS_BASE}/orpc/ws?token=${encodeURIComponent(authToken)}`
66+
: `${WS_BASE}/orpc/ws`;
67+
68+
const ws = new WebSocket(wsUrl);
6169
const link = new WebSocketLink({
6270
websocket: ws,
6371
});

0 commit comments

Comments
 (0)