Skip to content

Instantly share code, notes, and snippets.

@ianjennings
Last active August 4, 2017 16:53
Show Gist options
  • Save ianjennings/e9c9b7048e3dc7ff3c77ee0d9189b79d to your computer and use it in GitHub Desktop.
Save ianjennings/e9c9b7048e3dc7ff3c77ee0d9189b79d to your computer and use it in GitHub Desktop.
let PubNub = require('pubnub');
let pubnub = new PubNub({
publishKey: 'asdfasdf',
subscribeKey: 'asdfsadf',
secretKey: 'asdfasdf'
});
const express = require('express')
const app = express()
const bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.get('/', function (req, res) {
res.send('Hello World!')
});
app.post('/setup', function (req, res) {
console.log('setup called');
// let gChan = new Date().getTime();
let gChan = req.body.channel;
let myUUID = req.body.uuid;
let myAuthKey = req.body.authKey;
let chanMeRW = [
gChan,
gChan + '-pnpres',
gChan + ':chat:public.*',
gChan + ':user:' + myUUID + ':read.*',
gChan + ':user:' + myUUID + ':write.*'
];
pubnub.grant({
channels: chanMeRW,
read: true, // false to disallow
write: true, // false to disallow,
authKeys: [myAuthKey],
ttl: 0
}, function (a,b,c) {
console.log('cb 1')
console.log(a,b,c)
res.send('It worked');
});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
export default (request, response) => {
const pubnub = require('pubnub');
const kvstore = require('kvstore');
const xhr = require('xhr');
let headersObject = request.headers;
let paramsObject = request.params;
let methodString = request.method;
let bodyString = request.body;
console.log('request',request); // Log the request envelope passed
// Query parameters passed are parsed into the request.params object for you
// console.log(paramsObject.a) // This would print "5" for query string "a=5
// Set the status code - by default it would return 200
response.status = 200;
return request.json().then((body) => {
let gChan = body.channel;
let myAuthKey = body.authKey;
let myUUID = body.uuid;
console.log(gChan, myAuthKey, myUUID)
let chanMeRW = [
gChan,
gChan + '-pnpres',
gChan + ':chat:public.*',
gChan + ':user:' + myUUID + ':read.*',
gChan + ':user:' + myUUID + ':write.*'
];
return pubnub.grant({
channels: chanMeRW,
read: true, // false to disallow
write: true, // false to disallow,
authKeys: [myAuthKey],
ttl: 0
}).then((res) => {
console.log('granted on', chanMeRW, myAuthKey);
return response.send(body);
}).catch((error) => {
// handle error
console.log('couldnt grant')
console.log(error)
return response.send(body);
});
}).catch((err) => {
// console.log(err)
return response.send("Malformed JSON body.");
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment