From bebec8bfe424ba90111c7fc178a3493831e9812e Mon Sep 17 00:00:00 2001 From: wisplite Date: Sun, 26 Apr 2026 00:29:27 -0500 Subject: [PATCH] auth? --- src/index.ts | 17 +++++++++++++++++ src/utils/websocket.ts | 3 +++ 2 files changed, 20 insertions(+) diff --git a/src/index.ts b/src/index.ts index 15878d8..5629b5f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,11 @@ export class TetherClient { private websocketHandler: WebSocketHandler = new WebSocketHandler(); private subscribedQueries = new Map void, params: any }>(); private pendingMutations = new Map(); + private authMethod: Function | null = null; + private authenticated: boolean = false; + private userInfo: Map = new Map(); + connect = (url: string) => { this.websocketHandler.startConnection(url); this.websocketHandler.onQuery = (location, data) => { @@ -27,7 +31,15 @@ export class TetherClient { this.pendingMutations.delete(incoming_id); pending.resolve(data); }; + this.websocketHandler.onAuth = (data) => { + this.authenticated = true; + this.userInfo.set('user_id', data.user_id); + }; this.websocketHandler.onOpen = () => { + this.websocketHandler.send(JSON.stringify({ + type: 'auth', + token: this.authMethod?.() + })); this.subscribedQueries.forEach(({ params }, queryName) => { this.websocketHandler.send(JSON.stringify({ type: 'subscribe', @@ -42,6 +54,7 @@ export class TetherClient { pending.reject(new Error('Connection closed')); }); this.pendingMutations.clear(); + this.authenticated = false; }; }; @@ -83,4 +96,8 @@ export class TetherClient { })); return promise; }; + + setAuthMethod = (authMethod: Function) => { // Function that returns a token + this.authMethod = authMethod; + }; } \ No newline at end of file diff --git a/src/utils/websocket.ts b/src/utils/websocket.ts index 776f7ea..6dbadf3 100644 --- a/src/utils/websocket.ts +++ b/src/utils/websocket.ts @@ -9,6 +9,7 @@ export class WebSocketHandler { private reconnectInterval: number = 1000; private sendQueue: string[] = []; public onMutation: (mutation_id: string, data: any) => void = () => {}; + public onAuth: (data: any) => void = () => {}; startConnection = (url: string) => { this.url = url; this.ws = new WebSocket(url); @@ -44,6 +45,8 @@ export class WebSocketHandler { this.onMutation(data.mutation_id || '', data.data); } else if (data.type === 'error') { console.error(data.error); + } else if (data.type === 'auth') { + this.onAuth(data.data); } };