-
Notifications
You must be signed in to change notification settings - Fork 49
/
subscribeCurrentUserUpdates.js
50 lines (42 loc) · 1.08 KB
/
subscribeCurrentUserUpdates.js
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
const { ActionCable } = require('@sorare/actioncable');
const token = process.env['JWT_TOKEN'];
const jwtAud = process.env['JWT_AUD'];
if (!token) {
throw new Error('Missing JWT_TOKEN environment variable');
}
if (!jwtAud) {
throw new Error('Missing JWT_AUD environment variable');
}
const cable = new ActionCable({
headers: {
'Authorization': `Bearer ${token}`,
'JWT-AUD': jwtAud,
}
});
cable.subscribe('currentUserWasUpdated { slug nickname }', {
connected() {
console.log("connected");
},
disconnected(error) {
console.log("disconnected", error);
process.exit(1);
},
rejected(error) {
console.log("rejected", error);
process.exit(1);
},
received(data) {
if (data?.result?.errors?.length > 0) {
console.log('error', data?.result?.errors);
process.exit(1);
return;
}
const currentUserWasUpdated = data?.result?.data?.currentUserWasUpdated;
if (!currentUserWasUpdated) {
return;
}
const { slug } = currentUserWasUpdated;
console.log('current user was updated', slug);
process.exit(0);
}
});