-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (60 loc) · 3.15 KB
/
script.js
File metadata and controls
77 lines (60 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Login form handler with token generation
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('loginWrapper');
const errorDisplay = document.getElementById('errorDisplay');
form.addEventListener('submit', async function(e) {
e.preventDefault();
const serverURL = document.getElementById('serverURL').value.trim();
const username = document.getElementById('username').value.trim();
const password = document.getElementById('password').value;
errorDisplay.style.display = 'none';
const submitBtn = document.getElementById('submitBtn');
const originalText = submitBtn.textContent;
submitBtn.textContent = 'Connecting...';
submitBtn.disabled = true;
try {
// test connection using Subsonic API ping
const testUrl = `${serverURL}/rest/ping.view?u=${encodeURIComponent(username)}&p=${encodeURIComponent(password)}&v=1.16.1&c=NaviOverlay&f=json`;
const response = await fetch(testUrl, {
method: 'GET'
});
if (response.ok) {
const data = await response.json();
// check if subsonic response is successful
if (data['subsonic-response'] && data['subsonic-response'].status === 'ok') {
// generating secure token
submitBtn.textContent = 'Generating overlay...';
const credentials = {
serverURL: serverURL,
username: username,
password: password
};
// Send credentials to server to generate token
const tokenResponse = await fetch('/api/generate-token', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ credentials })
});
if (tokenResponse.ok) {
const tokenData = await tokenResponse.json();
window.location.href = tokenData.overlayUrl;
} else {
throw new Error('Failed to generate overlay link');
}
} else {
throw new Error('Authentication failed');
}
} else {
throw new Error('Server connection failed');
}
} catch (error) {
console.error('Login error:', error);
errorDisplay.style.display = 'block';
errorDisplay.textContent = 'Please check your credentials and server URL';
submitBtn.textContent = originalText;
submitBtn.disabled = false;
}
});
});