Skip to content

Instantly share code, notes, and snippets.

@bookercodes
Last active November 9, 2021 16:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bookercodes/290c7a8e8bcf4769caf9a2aaeea87944 to your computer and use it in GitHub Desktop.
Save bookercodes/290c7a8e8bcf4769caf9a2aaeea87944 to your computer and use it in GitHub Desktop.
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: 'YOUR INSTANCE LOCATOR',
key:
'YOUR 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(() => {
console.log(`User created: ${username}`)
res.sendStatus(201)
})
.catch(err => {
if (err.error === 'services/chatkit/user_already_exists') {
console.log(`User already exists: ${username}`)
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}`)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment