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:
Alex McLean 2025-01-24 11:13:49 +00:00 committed by GitHub
parent 52ebfa1d09
commit 9432f780f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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);