-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
87 lines (73 loc) · 2.25 KB
/
server.js
File metadata and controls
87 lines (73 loc) · 2.25 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
80
81
82
83
84
85
86
87
require('dotenv').config({ path: '.env' });
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const Chatkit = require('@pusher/chatkit-server');
const app = express();
const chatkit = new Chatkit.default({
instanceLocator: process.env.CHATKIT_INSTANCE_LOCATOR,
key: process.env.CHATKIT_SECRET_KEY,
});
var corsOptions = {
origin: '*'
}
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/delete', (req, res) => {
const userId = req.body.userId;
// console.log(userId);
chatkit
.asyncDeleteUser({
userId: userId
})
.then(() => {
chatkit.getUser({
userId: userId,
})
.then(user => {
console.error("User was not deleted!");
// console.log('got a user', user);
})
.catch(err => {
if (err.error === 'services/chatkit/not_found/user_not_found') {
console.log("User deleted successfully");
res.sendStatus(200);
} else {
res.status.apply(err.status).json(err);
}
});
})
.catch(err => {
console.error(err);
});
});
app.post('/users', (req, res) => {
const { userId } = req.body;
chatkit
.createUser({
id: userId,
name: userId,
})
.then(() => {
res.sendStatus(201);
})
.catch(err => {
if (err.error === 'services/chatkit/user_already_exists') {
// console.log(`User already exists: ${userId}`);
res.sendStatus(200);
} else {
res.status(err.status).json(err);
}
});
});
app.post('/authenticate', (req, res) => {
const authData = chatkit.authenticate({
userId: req.query.user_id,
});
res.status(authData.status).send(authData.body);
});
app.set('port', process.env.PORT || 5200);
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});