mirror of
https://codeberg.org/uzu/strudel
synced 2026-07-13 06:19:33 -04:00
MQTT - if password isn't provided, prompt for one
Helpful for cases where you don't want to risk putting a password in the code.
This commit is contained in:
@@ -23,6 +23,9 @@ function onMessageArrived(message) {
|
||||
|
||||
function onFailure(err) {
|
||||
console.error('Connection failed: ', err);
|
||||
if (typeof window !== 'undefined') {
|
||||
document.cookie = 'mqtt_pass=';
|
||||
}
|
||||
}
|
||||
|
||||
Pattern.prototype.mqtt = function (
|
||||
@@ -35,12 +38,17 @@ Pattern.prototype.mqtt = function (
|
||||
) {
|
||||
const key = host + '-' + client;
|
||||
let connected = false;
|
||||
let password_entered = false;
|
||||
|
||||
if (!client) {
|
||||
client = 'strudel-' + String(Math.floor(Math.random() * 1000000));
|
||||
}
|
||||
function onConnect() {
|
||||
console.log('Connected to mqtt broker');
|
||||
connected = true;
|
||||
if (password_entered) {
|
||||
document.cookie = 'mqtt_pass=' + password;
|
||||
}
|
||||
}
|
||||
|
||||
let cx;
|
||||
@@ -58,6 +66,17 @@ Pattern.prototype.mqtt = function (
|
||||
|
||||
if (username) {
|
||||
props.userName = username;
|
||||
if (typeof password === 'undefined' && typeof window !== 'undefined') {
|
||||
const cookie = /mqtt_pass=(\w+)/.exec(window.document.cookie);
|
||||
if (cookie) {
|
||||
password = cookie[1];
|
||||
}
|
||||
if (typeof password === 'undefined') {
|
||||
password = prompt('Please enter MQTT server password');
|
||||
password_entered = true;
|
||||
}
|
||||
}
|
||||
|
||||
props.password = password;
|
||||
}
|
||||
cx.connect(props);
|
||||
|
||||
Reference in New Issue
Block a user