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,6 +9,10 @@ export class TetherClient {
|
|||||||
private websocketHandler: WebSocketHandler = new WebSocketHandler();
|
private websocketHandler: WebSocketHandler = new WebSocketHandler();
|
||||||
private subscribedQueries = new Map<string, { callback: (data: any) => void, params: any }>();
|
private subscribedQueries = new Map<string, { callback: (data: any) => void, params: any }>();
|
||||||
private pendingMutations = new Map<string, PendingMutation>();
|
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) => {
|
connect = (url: string) => {
|
||||||
this.websocketHandler.startConnection(url);
|
this.websocketHandler.startConnection(url);
|
||||||
@@ -27,7 +31,15 @@ export class TetherClient {
|
|||||||
this.pendingMutations.delete(incoming_id);
|
this.pendingMutations.delete(incoming_id);
|
||||||
pending.resolve(data);
|
pending.resolve(data);
|
||||||
};
|
};
|
||||||
|
this.websocketHandler.onAuth = (data) => {
|
||||||
|
this.authenticated = true;
|
||||||
|
this.userInfo.set('user_id', data.user_id);
|
||||||
|
};
|
||||||
this.websocketHandler.onOpen = () => {
|
this.websocketHandler.onOpen = () => {
|
||||||
|
this.websocketHandler.send(JSON.stringify({
|
||||||
|
type: 'auth',
|
||||||
|
token: this.authMethod?.()
|
||||||
|
}));
|
||||||
this.subscribedQueries.forEach(({ params }, queryName) => {
|
this.subscribedQueries.forEach(({ params }, queryName) => {
|
||||||
this.websocketHandler.send(JSON.stringify({
|
this.websocketHandler.send(JSON.stringify({
|
||||||
type: 'subscribe',
|
type: 'subscribe',
|
||||||
@@ -42,6 +54,7 @@ export class TetherClient {
|
|||||||
pending.reject(new Error('Connection closed'));
|
pending.reject(new Error('Connection closed'));
|
||||||
});
|
});
|
||||||
this.pendingMutations.clear();
|
this.pendingMutations.clear();
|
||||||
|
this.authenticated = false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -83,4 +96,8 @@ export class TetherClient {
|
|||||||
}));
|
}));
|
||||||
return promise;
|
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 reconnectInterval: number = 1000;
|
||||||
private sendQueue: string[] = [];
|
private sendQueue: string[] = [];
|
||||||
public onMutation: (mutation_id: string, data: any) => void = () => {};
|
public onMutation: (mutation_id: string, data: any) => void = () => {};
|
||||||
|
public onAuth: (data: any) => void = () => {};
|
||||||
startConnection = (url: string) => {
|
startConnection = (url: string) => {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.ws = new WebSocket(url);
|
this.ws = new WebSocket(url);
|
||||||
@@ -44,6 +45,8 @@ export class WebSocketHandler {
|
|||||||
this.onMutation(data.mutation_id || '', data.data);
|
this.onMutation(data.mutation_id || '', data.data);
|
||||||
} else if (data.type === 'error') {
|
} else if (data.type === 'error') {
|
||||||
console.error(data.error);
|
console.error(data.error);
|
||||||
|
} else if (data.type === 'auth') {
|
||||||
|
this.onAuth(data.data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user