Skip to content

Instantly share code, notes, and snippets.

@ianjennings
Created August 4, 2017 17:08
Show Gist options
  • Save ianjennings/9e65b3d5fbdf466c228526e8bc4a70ce to your computer and use it in GitHub Desktop.
Save ianjennings/9e65b3d5fbdf466c228526e8bc4a70ce to your computer and use it in GitHub Desktop.
// runs on setup
let request = require('request');const config = require('./test-grant-config.js');
let PubNub = require('pubnub');
let channel = 'chan' + new Date().getTime();
let myUUID = 'uuid' + new Date().getTime();;
let myAuthKey = "auth" + new Date().getTime();;
request.post({
url: 'https://pubsub.pubnub.com/v1/blocks/sub-key/sub-c-67db0e7a-50be-11e7-bf50-02ee2ddab7fe/auther',
json: {
authKey: myAuthKey,
channel: channel,
uuid: myUUID
}
}, (err, httpResponse, body) => {
console.log('pfunct response', body)
let pn = new PubNub({
authKey: myAuthKey,
publishKey: 'pub-c-c6303bb2-8bf8-4417-aac7-e83b52237ea6',
subscribeKey: 'sub-c-67db0e7a-50be-11e7-bf50-02ee2ddab7fe'
});
pn.addListener({
message: (message) => {
console.log('incoming', message)
}
});
pn.subscribe({
channels: [channel]
});
console.log('publishing')
pn.publish({
channel: channel,
message: 'test'
}, function(a,b,c){
console.log(a,b,c)
})
});
export default (request, response) => {
const pubnub = require('pubnub');
let bodyString = request.body;
response.status = 200;
return request.json().then((body) => {
body.valid = false;
if(body.uuid == "ian" && body.authKey == 12345) {
body.valid = true;
}
let chan = body.channel;
let myAuthKey = body.authKey;
let myUUID = body.uuid;
console.log(chan, myAuthKey, myUUID)
return pubnub.grant({
channels: [chan],
read: true, // false to disallow
write: true, // false to disallow,
authKeys: [myAuthKey],
ttl: 0
}).then((res) => {
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