Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new api for send a refresh-token #1216

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,10 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
event,
});

if (event.type === 'token.expiring' && !this.tokenManager.isStatic()) {
this.refreshToken();
}

if (event.type === 'user.presence.changed' || event.type === 'user.updated' || event.type === 'user.deleted') {
this._handleUserEvent(event);
}
Expand Down Expand Up @@ -3367,4 +3371,15 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
async commitMessage(id: string) {
return await this.post<APIResponse & MessageResponse>(this.baseURL + `/messages/${id}/commit`);
}

/**
* Calls the token provider to retrieve a new token
*
* This is an internal method, you only need to use it if, for some reason, the existing refresh logic of the client doesn't suit your needs.
*
*/
async refreshToken() {
const token = await this.tokenManager.loadToken();
return this.wsConnection?.refreshToken(token);
}
}
6 changes: 6 additions & 0 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,10 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
}
}, this.connectionCheckTimeout);
};

refreshToken = (token: string) => {
// you can expect `token.ack` event if backend registered the new token for the WS connection
const d = JSON.stringify({ type: 'token.refresh', api_key: this.client.key, token });
this.ws?.send(d);
};
}
2 changes: 2 additions & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export const EVENT_MAP = {
'user.updated': true,
'user.watching.start': true,
'user.watching.stop': true,
'token.expiring': true,
'token.ack': true,

// local events
'channels.queried': true,
Expand Down
Loading