Last active
April 18, 2018 17:55
-
-
Save bookercodes/e73062f82f623f129f207f4b52f3e7d4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(username, username) | |
+ .then(() => res.sendStatus(201)) | |
+ .catch(error => { | |
+ if (error.error_type === 'services/chatkit/user/user_already_exists') { | |
+ res.sendStatus(200) | |
+ } else { | |
+ res.status(error.statusCode).json(error) | |
+ } | |
+ }) | |
+}) | |
+app.post('/authenticate', (req, res) => { | |
+ const { grant_type } = req.body | |
+ res.json(chatkit.authenticate({ grant_type }, req.query.user_id)) | |
+}) | |
const PORT = 3001 | |
app.listen(PORT, err => { | |
if (err) { | |
console.error(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