Skip to content

Commit b834d4f

Browse files
Http endpoint
1 parent 0c25cc4 commit b834d4f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

vapid-configuration.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,33 @@ module.exports = function (RED) {
88
}
99
RED.nodes.registerType('vapid-configuration', VapidConfigurationNode)
1010
}
11+
module.exports = function (RED) {
12+
const webpush = require('web-push');
13+
14+
function VapidConfigurationNode (config) {
15+
RED.nodes.createNode(this, config)
16+
this.subject = config.subject
17+
this.publicKey = config.publicKey
18+
this.privateKey = config.privateKey
19+
this.gcmApiKey = config.gcmApiKey
20+
}
21+
RED.nodes.registerType('vapid-configuration', VapidConfigurationNode)
22+
23+
// Make the key pair generation available to the config screen (in the flow editor)
24+
RED.httpAdmin.get('/vapid_configuration/generate_key_pair', RED.auth.needsPermission('vapid-configuration.write'), async function(req, res){
25+
try {
26+
// Generate a VAPID keypair
27+
const vapidKeys = webpush.generateVAPIDKeys();
28+
29+
// Return public key and private key to the config screen (since they need to be stored in the node's credentials)
30+
res.json({
31+
publicKey: vapidKeys.publicKey,
32+
privateKey: vapidKeys.privateKey
33+
})
34+
}
35+
catch (err) {
36+
console.log("Error while generating VAPID keypair: " + err)
37+
res.status(500).json({error: 'Error while generating VAPID keypair'})
38+
}
39+
});
40+
}

0 commit comments

Comments
 (0)