-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathvalidateInviteToken.ts
More file actions
55 lines (47 loc) · 1.49 KB
/
validateInviteToken.ts
File metadata and controls
55 lines (47 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Invites, Rooms } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';
import { settings } from '../../../settings/server';
export const validateInviteToken = async (token: string) => {
if (!token || typeof token !== 'string') {
throw new Meteor.Error('error-invalid-token', 'The invite token is invalid.', {
method: 'validateInviteToken',
field: 'token',
});
}
const inviteData = await Invites.findOneById(token);
if (!inviteData) {
throw new Meteor.Error('error-invalid-token', 'The invite token is invalid.', {
method: 'validateInviteToken',
field: 'token',
});
}
const room = await Rooms.findOneById(inviteData.rid);
if (!room) {
throw new Meteor.Error('error-invalid-room', 'The invite token is invalid.', {
method: 'validateInviteToken',
field: 'rid',
});
}
if (settings.get('ABAC_Enabled') && room?.abacAttributes?.length) {
throw new Meteor.Error('error-invalid-room', 'Room is ABAC managed', {
method: 'validateInviteToken',
field: 'rid',
});
}
if (inviteData.expires && new Date(inviteData.expires).getTime() <= Date.now()) {
throw new Meteor.Error('error-invite-expired', 'The invite token has expired.', {
method: 'validateInviteToken',
field: 'expires',
});
}
if (inviteData.maxUses > 0 && inviteData.uses >= inviteData.maxUses) {
throw new Meteor.Error('error-invite-expired', 'The invite token has expired.', {
method: 'validateInviteToken',
field: 'maxUses',
});
}
return {
inviteData,
room,
};
};