From 80c9c954741ffe45e08330d1601c86a437dfdf3c Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Thu, 21 Dec 2023 17:09:39 -0300 Subject: [PATCH 1/3] Update account types --- ee/packages/ddp-client/src/types/Account.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ee/packages/ddp-client/src/types/Account.ts b/ee/packages/ddp-client/src/types/Account.ts index 654ae2c2fb4d4..8d2284a6d4914 100644 --- a/ee/packages/ddp-client/src/types/Account.ts +++ b/ee/packages/ddp-client/src/types/Account.ts @@ -2,13 +2,20 @@ import { Emitter } from '@rocket.chat/emitter'; import type { ClientStream } from './ClientStream'; +type User = { + id: string; + username?: string; + token?: string; + tokenExpires?: Date; +} & Record; + export interface Account extends Emitter<{ uid: string | undefined; - user: Record | undefined; + user?: User; }> { uid?: string; - user?: Record; + user?: User; loginWithPassword(username: string, password: string): Promise; loginWithToken(token: string): Promise<{ id: string; @@ -21,12 +28,7 @@ export interface Account export class AccountImpl extends Emitter<{ uid: string | undefined; - user: { - id: string; - username?: string; - token?: string; - tokenExpires?: Date; - }; + user: User; }> implements Account { From 0429274fa46435f3407af60c1ebeaeeaa62e8c17 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Thu, 21 Dec 2023 17:10:36 -0300 Subject: [PATCH 2/3] Move login attempt to DDPSDK --- ee/packages/ddp-client/src/DDPSDK.ts | 3 +++ ee/packages/ddp-client/src/types/Account.ts | 5 ----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/ee/packages/ddp-client/src/DDPSDK.ts b/ee/packages/ddp-client/src/DDPSDK.ts index 61e0dc6fe28f7..445ce90d7c780 100644 --- a/ee/packages/ddp-client/src/DDPSDK.ts +++ b/ee/packages/ddp-client/src/DDPSDK.ts @@ -113,6 +113,9 @@ export class DDPSDK implements SDK { const sdk = new DDPSDK(connection, stream, account, timeoutControl, rest); connection.on('connected', () => { + if (account.user?.token) { + account.loginWithToken(account.user.token); + } [...stream.subscriptions.entries()].forEach(([, sub]) => { ddp.subscribeWithId(sub.id, sub.name, sub.params); }); diff --git a/ee/packages/ddp-client/src/types/Account.ts b/ee/packages/ddp-client/src/types/Account.ts index 8d2284a6d4914..4b5bbf80165c4 100644 --- a/ee/packages/ddp-client/src/types/Account.ts +++ b/ee/packages/ddp-client/src/types/Account.ts @@ -38,11 +38,6 @@ export class AccountImpl constructor(private readonly client: ClientStream) { super(); - this.client.on('connected', () => { - if (this.user?.token) { - this.loginWithToken(this.user.token); - } - }); client.onCollection('users', (data) => { if (data.collection !== 'users') { From 33b5d4155f9a23a855a405df7ec4f5db08f60b62 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Thu, 21 Dec 2023 17:16:31 -0300 Subject: [PATCH 3/3] Delete user on logout --- ee/packages/ddp-client/src/types/Account.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/ee/packages/ddp-client/src/types/Account.ts b/ee/packages/ddp-client/src/types/Account.ts index 4b5bbf80165c4..c8a51e4027864 100644 --- a/ee/packages/ddp-client/src/types/Account.ts +++ b/ee/packages/ddp-client/src/types/Account.ts @@ -114,6 +114,7 @@ export class AccountImpl wait: true, }); this.uid = undefined; + this.user = undefined; this.emit('uid', this.uid); } }