Skip to content

Commit de42e08

Browse files
authored
Merge pull request #450 from GetStream/448
448
2 parents 639d15e + 054ccec commit de42e08

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

projects/stream-chat-angular/src/lib/chat-client.service.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ describe('ChatClientService', () => {
6363
expect(mockChatClient.setGuestUser).toHaveBeenCalledWith({ id: userId });
6464
});
6565

66+
it('should connect user - anonymous user', async () => {
67+
mockChatClient.connectUser.calls.reset();
68+
await service.init(apiKey, undefined, 'anonymous');
69+
70+
expect(StreamChat.getInstance).toHaveBeenCalledWith(apiKey, undefined);
71+
const spy = jasmine.createSpy();
72+
service.appSettings$.subscribe(spy);
73+
74+
expect(spy).toHaveBeenCalledWith(undefined);
75+
expect(mockChatClient.connectUser).not.toHaveBeenCalled();
76+
expect(mockChatClient.connectAnonymousUser).toHaveBeenCalledWith();
77+
});
78+
6679
it(`should notify if connection wasn't successful`, async () => {
6780
const notificationService = TestBed.inject(NotificationService);
6881
const spy = jasmine.createSpy();

projects/stream-chat-angular/src/lib/chat-client.service.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,18 @@ export class ChatClientService<
8383
/**
8484
* 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.
8585
* @param apiKey
86-
* @param userOrId
87-
* @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)
86+
* @param userOrId you can emit this for anonymous logins
87+
* @param userTokenOrProvider You can provide:<ul>
88+
* <li> a token, </li>
89+
* <li> the keyword 'guest' to connect as [guest user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#guest-users) </li>
90+
* <li> the keyword 'anonymous' to connect as [anonymous user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#anonymous-users) </li>
91+
* </ul>
8892
* @param clientOptions Setting to provide to the Stream client instance
8993
*/
9094
async init(
9195
apiKey: string,
92-
userOrId: string | OwnUserResponse<T> | UserResponse<T>,
93-
userTokenOrProvider: TokenOrProvider | 'guest',
96+
userOrId: string | OwnUserResponse<T> | UserResponse<T> | undefined,
97+
userTokenOrProvider: TokenOrProvider | 'anonymous' | 'guest',
9498
clientOptions?: StreamChatOptions
9599
): ConnectAPIResponse<T> {
96100
this.chatClient = StreamChat.getInstance<T>(apiKey, clientOptions);
@@ -99,10 +103,14 @@ export class ChatClientService<
99103
await this.ngZone.runOutsideAngular(async () => {
100104
const user = typeof userOrId === 'string' ? { id: userOrId } : userOrId;
101105
try {
102-
result =
103-
userTokenOrProvider === 'guest'
104-
? await this.chatClient.setGuestUser(user)
105-
: await this.chatClient.connectUser(user, userTokenOrProvider);
106+
result = await (
107+
{
108+
guest: () => this.chatClient.setGuestUser(user!),
109+
anonymous: () => this.chatClient.connectAnonymousUser(),
110+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
111+
}[`${userTokenOrProvider}`] ??
112+
(() => this.chatClient.connectUser(user!, userTokenOrProvider))
113+
)();
106114
} catch (error) {
107115
this.notificationService.addPermanentNotification(
108116
'streamChat.Error connecting to chat, refresh the page to try again.',

projects/stream-chat-angular/src/lib/mocks/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ export type MockStreamChatClient = {
307307
getAppSettings: jasmine.Spy;
308308
disconnectUser: jasmine.Spy;
309309
queryChannels: jasmine.Spy;
310+
connectAnonymousUser: jasmine.Spy;
310311
};
311312

312313
export const mockStreamChatClient = (): MockStreamChatClient => {
@@ -338,6 +339,7 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
338339
const getUserAgent = jasmine
339340
.createSpy()
340341
.and.returnValue('stream-chat-javascript-client-browser-2.2.2');
342+
const connectAnonymousUser = jasmine.createSpy();
341343
/* eslint-enable jasmine/no-unsafe-spy */
342344
const user = mockCurrentUser();
343345
const on = (name: EventTypes | Function, handler: () => {}) => {
@@ -374,6 +376,7 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
374376
appSettings$,
375377
queryChannels,
376378
setGuestUser,
379+
connectAnonymousUser,
377380
};
378381
};
379382

0 commit comments

Comments
 (0)