Skip to content

Commit

Permalink
feat: anonymous support
Browse files Browse the repository at this point in the history
  • Loading branch information
ciekawy committed Jul 8, 2023
1 parent 639d15e commit e1c92c5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions projects/stream-chat-angular/src/lib/chat-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ export class ChatClientService<
* Creates a [`StreamChat`](https://github.com/GetStream/stream-chat-js/blob/668b3e5521339f4e14fc657834531b4c8bf8176b/src/client.ts#L124) instance using the provided `apiKey`, and connects a user with the given meta data and token. More info about [connecting users](https://getstream.io/chat/docs/javascript/init_and_users/?language=javascript) can be found in the platform documentation.
* @param apiKey
* @param userOrId
* @param userTokenOrProvider You can provide a token, or the keyword 'guest' to connect as [guest user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#guest-users)
* @param userTokenOrProvider You can provide:<ul>
* <li> a token,
* <li> the keyword 'guest' to connect as [guest user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#guest-users)
* <li> the keyword 'anonymous' to connect as [anonymous user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#anonymous-users)
* </ul>
* @param clientOptions Setting to provide to the Stream client instance
*/
async init(
apiKey: string,
userOrId: string | OwnUserResponse<T> | UserResponse<T>,
userTokenOrProvider: TokenOrProvider | 'guest',
userTokenOrProvider: TokenOrProvider | 'anonymous' | 'guest',
clientOptions?: StreamChatOptions
): ConnectAPIResponse<T> {
this.chatClient = StreamChat.getInstance<T>(apiKey, clientOptions);
Expand All @@ -99,10 +103,13 @@ export class ChatClientService<
await this.ngZone.runOutsideAngular(async () => {
const user = typeof userOrId === 'string' ? { id: userOrId } : userOrId;
try {
result =
userTokenOrProvider === 'guest'
? await this.chatClient.setGuestUser(user)
: await this.chatClient.connectUser(user, userTokenOrProvider);
result = await (
{
guest: () => this.chatClient.setGuestUser(user),
anonymous: () => this.chatClient.connectAnonymousUser(),
}[`${userTokenOrProvider}`] ??
(() => this.chatClient.connectUser(user, userTokenOrProvider))
)();
} catch (error) {
this.notificationService.addPermanentNotification(
'streamChat.Error connecting to chat, refresh the page to try again.',
Expand Down

0 comments on commit e1c92c5

Please sign in to comment.