Skip to content

Commit 84f5d75

Browse files
committed
Removing API key from export function
1 parent dc13290 commit 84f5d75

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

io/emoncms/88-emoncms.html

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,36 @@
8585
category: 'config',
8686
defaults: {
8787
server: {value:"http://localhost",required:true},
88-
apikey: {value:"",required:true},
88+
// apikey: {value:"",required:true},
8989
name: {value:""}
9090
},
9191
label: function() {
9292
return this.name||this.server;
93+
},
94+
oneditprepare: function() {
95+
$.getJSON('emoncms-server/'+this.id,function(data) {
96+
if (data.apikey) {
97+
$('#node-config-input-apikey').val(data.apikey);
98+
}
99+
});
100+
},
101+
oneditsave: function() {
102+
var newApikey = $('#node-config-input-apikey').val();
103+
var credentials = {};
104+
credentials.apikey = newApikey;
105+
$.ajax({
106+
url: 'emoncms-server/'+this.id,
107+
type: 'POST',
108+
data: credentials,
109+
success:function(result){}
110+
});
111+
},
112+
ondelete: function() {
113+
$.ajax({
114+
url: 'emoncms-server/'+this.id,
115+
type: 'DELETE',
116+
success: function(result) {}
117+
});
93118
}
94119
});
95120
</script>

io/emoncms/88-emoncms.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,50 @@ var RED = require(process.env.NODE_RED_HOME+"/red/red");
1919
function EmoncmsServerNode(n) {
2020
RED.nodes.createNode(this,n);
2121
this.server = n.server;
22-
this.apikey = n.apikey;
2322
this.name = n.name;
23+
var credentials = RED.nodes.getCredentials(n.id);
24+
if (credentials) {
25+
this.apikey = credentials.apikey;
26+
}
27+
2428
}
2529
RED.nodes.registerType("emoncms-server",EmoncmsServerNode);
2630

31+
var querystring = require('querystring');
32+
33+
RED.app.get('/emoncms-server/:id',function(req,res) {
34+
var credentials = RED.nodes.getCredentials(req.params.id);
35+
if (credentials) {
36+
res.send(JSON.stringify({apikey:credentials.apikey}));
37+
} else {
38+
res.send(JSON.stringify({}));
39+
}
40+
});
41+
42+
RED.app.delete('/emoncms-server/:id',function(req,res) {
43+
RED.nodes.deleteCredentials(req.params.id);
44+
res.send(200);
45+
});
46+
47+
RED.app.post('/emoncms-server/:id',function(req,res) {
48+
49+
var body = "";
50+
req.on('data', function(chunk) {
51+
body+=chunk;
52+
});
53+
req.on('end', function(){
54+
var newCreds = querystring.parse(body);
55+
var credentials = RED.nodes.getCredentials(req.params.id)||{};
56+
if (newCreds.apikey == null || newCreds.apikey == "") {
57+
delete credentials.apikey;
58+
} else {
59+
credentials.apikey = newCreds.apikey;
60+
}
61+
RED.nodes.addCredentials(req.params.id,credentials);
62+
res.send(200);
63+
});
64+
});
65+
2766
function Emoncms(n) {
2867
RED.nodes.createNode(this,n);
2968
this.emonServer = n.emonServer;

0 commit comments

Comments
 (0)