-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (43 loc) · 1.31 KB
/
server.js
File metadata and controls
51 lines (43 loc) · 1.31 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const Chatkit = require('@pusher/chatkit-server');
require('dotenv').config();
const { INSTANCE_LOCATOR: instanceLocator, CHATKIT_KEY: key } = process.env;
if (!instanceLocator || !key) {
console.log('INSTANCE_LOCATOR or CHATKIT_KEY not set in environment.');
process.exit(1);
}
const app = express();
const chatkit = new Chatkit.default({
instanceLocator,
key,
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
app.post('/users', (req, res) => {
const { username } = req.body;
chatkit
.createUser({ id: username, name: username })
.then(() => res.sendStatus(201))
.catch(err => {
if (err.error === 'services/chatkit/user_already_exists') {
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);
});
const PORT = 3001;
app.listen(PORT, err => {
if (err) {
console.log(err);
} else {
console.log(`Running on port ${PORT}`);
}
});