mirror of
https://github.com/wisplite/tether-ts.git
synced 2026-05-01 06:22:41 -05:00
auth?
This commit is contained in:
@@ -9,7 +9,11 @@ export class TetherClient {
|
||||
private websocketHandler: WebSocketHandler = new WebSocketHandler();
|
||||
private subscribedQueries = new Map<string, { callback: (data: any) => void, params: any }>();
|
||||
private pendingMutations = new Map<string, PendingMutation>();
|
||||
private authMethod: Function | null = null;
|
||||
private authenticated: boolean = false;
|
||||
private userInfo: Map<string, any> = 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;
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user