Last active
November 9, 2021 16:13
-
-
Save bookercodes/290c7a8e8bcf4769caf9a2aaeea87944 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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