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

fix: prevent loading page on reconnect #33770

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
12 changes: 12 additions & 0 deletions apps/meteor/app/authentication/server/startup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ import { setAvatarFromServiceWithValidation } from '../../../lib/server/function
import { notifyOnSettingChangedById } from '../../../lib/server/lib/notifyListener';
import * as Mailer from '../../../mailer/server/api';
import { settings } from '../../../settings/server';
import { getBaseUserFields } from '../../../utils/server/functions/getBaseUserFields';
import { safeGetMeteorUser } from '../../../utils/server/functions/safeGetMeteorUser';
import { isValidAttemptByUser, isValidLoginAttemptByIp } from '../lib/restrictLoginAttempts';

Accounts.config({
forbidClientAccountCreation: true,
});
/**
* Accounts calls `_initServerPublications` and holds the `_defaultPublishFields`, without Object.assign its not possible
* to extend the projection
*
* the idea is to send all required fields to the client during login
* we tried `defaultFieldsSelector` , but it changes all Meteor.userAsync projections which is undesirable
*
*
* we are removing the status here because meteor send 'offline'
*/
Object.assign(Accounts._defaultPublishFields.projection, (({ status, ...rest }) => rest)(getBaseUserFields()));

Meteor.startup(() => {
settings.watchMultiple(['Accounts_LoginExpiration', 'Site_Name', 'From_Email'], () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<
CachedCollectionManager.onLogin(() => {
void this.init();
});
Accounts.onLogout(() => {
this.ready.set(false);
});
}

protected get eventName(): `${Name}-changed` | `${string}/${Name}-changed` {
Expand Down Expand Up @@ -342,8 +345,6 @@ export class CachedCollection<T extends { _id: string }, U = T> extends Emitter<
}

async init() {
this.ready.set(false);

if (await this.loadFromCache()) {
this.trySync();
} else {
Expand Down
8 changes: 3 additions & 5 deletions apps/meteor/client/startup/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ Meteor.startup(() => {
return;
}

// TODO: TEMPORARY UNTIL THIS IS FIXED
// BACKEND MS SHOULD SEND USER DATA AFTER LOGIN
// if (Meteor.loggingIn()) {
// return;
// }
if (Meteor.loggingIn()) {
return;
}

const user = await synchronizeUserData(uid);
if (!user) {
Expand Down
27 changes: 25 additions & 2 deletions ee/apps/ddp-streamer/src/DDPStreamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import crypto from 'crypto';

import { MeteorService, Presence, ServiceClass } from '@rocket.chat/core-services';
import { InstanceStatus } from '@rocket.chat/instance-status';
import { Users } from '@rocket.chat/models';
import polka from 'polka';
import { throttle } from 'underscore';
import WebSocket from 'ws';
Expand Down Expand Up @@ -123,12 +124,34 @@ export class DDPStreamer extends ServiceClass {
}
});

server.on(DDP_EVENTS.LOGGED, (info) => {
async function sendUserData(client: Client, userId: string) {
// TODO figure out what fields to send. maybe to to export function getBaseUserFields to a package
const loggedUser = await Users.findOneById(userId, {
projection: { name: 1, username: 1, settings: 1, roles: 1, active: 1, statusLivechat: 1, statusDefault: 1, status: 1 },
});
if (!loggedUser) {
return;
}

// using setImmediate here so login's method result is sent before we send the user data
setImmediate(async () => server.added(client, 'users', userId, loggedUser));
}
server.on(DDP_EVENTS.LOGGED, async (info: Client) => {
const { userId, connection } = info;

Presence.newConnection(userId, connection.id, nodeID);
if (!userId) {
throw new Error('User not logged in');
}

await Presence.newConnection(userId, connection.id, nodeID);

this.updateConnections();

// mimic Meteor's default publication that sends user data after login
await sendUserData(info, userId);

server.emit('presence', { userId, connection });

this.api?.broadcast('accounts.login', { userId, connection });
});

Expand Down
Loading