-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (40 loc) · 1.15 KB
/
server.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
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: 'v1:us1:52a0e31f-35bc-4c0c-b451-4379b15cb0e4',
key: '44f5b450-a46d-4839-967a-9aad1661a89a:fFjQR9pClZ4CouyNmnJMmhHCtalfRIk12B4lYaJ3xVY=',
})
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(error => {
if(error.error === 'services/chatkit/user_already_exists'){
res.sendStatus(200)
} else{
res.status(error.status).json(error)
}
})
})
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.error(err)
} else {
console.log(`Running on port ${PORT}`)
}
})