-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (71 loc) · 1.84 KB
/
server.js
File metadata and controls
79 lines (71 loc) · 1.84 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const express = require('express');
const axios = require('axios');
const app = express();
const appID = '{appID}';
const apiKey = '{apiKey}';
const agentUID = '{agentUID}';
const url = 'https://api.cometchat.com/v1';
const headers = {
'Content-Type': 'application/json',
appid: appID,
apikey: apiKey,
};
app.get('/api/create', (req, res) => {
const data = {
uid: new Date().getTime(),
name: 'customer',
};
axios
.post(`${url}/users`, JSON.stringify(data), {
headers,
})
.then(response => {
requestAuthToken(response.data.data.uid)
.then(token => {
console.log('Success:' + JSON.stringify(token));
res.json(token);
})
.catch(error => console.error('Error:', error));
})
.catch(error => console.error('Error:', error));
});
app.get('/api/auth', (req, res) => {
const uid = req.query.uid;
requestAuthToken(uid)
.then(token => {
console.log('Success:' + JSON.stringify(token));
res.json(token);
})
.catch(error => console.error('Error:', error));
});
const requestAuthToken = uid => {
return new Promise((resolve, reject) => {
axios
.post(`${url}/users/${uid}/auth_tokens`, null, {
headers,
})
.then(response => {
console.log('New Auth Token:', response.data);
resolve(response.data.data);
})
.catch(error => reject(error));
});
};
app.get('/api/users', (req, res) => {
axios
.get(`${url}/users`, {
headers,
})
.then(response => {
const { data } = response.data;
const filterAgentData = data.filter(data => {
return data.uid !== agentUID;
});
res.json(filterAgentData);
})
.catch(error => console.error('Error:', error));
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});