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] Clear push token on save user password #26466

Merged
merged 4 commits into from
Aug 4, 2022
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
6 changes: 6 additions & 0 deletions apps/meteor/server/models/raw/PushToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ export class PushTokenRaw extends BaseRaw<IPushToken> implements IPushTokenModel
authToken: { $nin: tokens },
});
}

removeAllByUserId(userId: string): Promise<DeleteResult> {
return this.deleteMany({
userId,
});
}
}
25 changes: 13 additions & 12 deletions apps/meteor/server/services/meteor/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,20 @@ if (disableOplog) {
// Re-implement meteor's reactivity that uses observe to disconnect sessions when the token
// associated was removed
processOnChange = (diff: Record<string, any>, id: string): void => {
if (!diff || !('services.resume.loginTokens' in diff)) {
return;
}
const loginTokens: undefined | { hashedToken: string }[] = diff['services.resume.loginTokens'];
if (loginTokens) {
const tokens = loginTokens.map(({ hashedToken }) => hashedToken);

const cbs = userCallbacks.get(id);
if (cbs) {
[...cbs]
.filter(({ hashedToken }) => !tokens.includes(hashedToken))
.forEach((item) => {
item.callbacks.removed(id);
cbs.delete(item);
});
}
const tokens = loginTokens?.map(({ hashedToken }) => hashedToken);

const cbs = userCallbacks.get(id);
if (cbs) {
[...cbs]
.filter(({ hashedToken }) => tokens === undefined || !tokens.includes(hashedToken))
.forEach((item) => {
item.callbacks.removed(id);
cbs.delete(item);
});
}
};
}
Expand Down
18 changes: 11 additions & 7 deletions apps/meteor/server/services/push/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ export class PushService extends ServiceClassInternal implements IPushService {
super();

this.onEvent('watch.users', async ({ id, diff }) => {
if (diff && 'services.resume.loginTokens' in diff) {
const tokens = diff['services.resume.loginTokens'].map(({ hashedToken }: { hashedToken: string }) => hashedToken);
this.cleanUpUserTokens(id, tokens);
if (!diff || !('services.resume.loginTokens' in diff)) {
return;
}
if (diff['services.resume.loginTokens'] === undefined) {
await PushToken.removeAllByUserId(id);
return;
}
const loginTokens = Array.isArray(diff['services.resume.loginTokens']) ? diff['services.resume.loginTokens'] : [];
const tokens = loginTokens.map(({ hashedToken }: { hashedToken: string }) => hashedToken);
if (tokens.length > 0) {
await PushToken.removeByUserIdExceptTokens(id, tokens);
}
});
}

private async cleanUpUserTokens(userId: string, tokens: string[]): Promise<void> {
await PushToken.removeByUserIdExceptTokens(userId, tokens);
}
}
14 changes: 11 additions & 3 deletions apps/meteor/tests/end-to-end/api/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ describe('[Users]', function () {
before((done) => clearCustomFields(done));
after((done) => clearCustomFields(done));

it('should create a new user', (done) => {
request
it('should create a new user', async () => {
await request
.post(api('users.create'))
.set(credentials)
.send({
Expand All @@ -120,8 +120,16 @@ describe('[Users]', function () {

targetUser._id = res.body.user._id;
targetUser.username = res.body.user.username;
});

await request
.post(api('login'))
.send({
user: apiUsername,
password,
})
.end(done);
.expect('Content-Type', 'application/json')
.expect(200);
});

it('should create a new user with custom fields', (done) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/model-typings/src/models/IPushTokenModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import type { IBaseModel } from './IBaseModel';

export interface IPushTokenModel extends IBaseModel<IPushToken> {
removeByUserIdExceptTokens(userId: string, tokens: string[]): Promise<DeleteResult>;

removeAllByUserId(userId: string): Promise<DeleteResult>;
}