Skip to content

Commit 4b04ddc

Browse files
committed
Refactor user data fetching and update API endpoints
- Changed user data fetching to use new API endpoints for user info, username, password, and 2FA management. - Enhanced error handling for Plex status loading and updated the logic for displaying Plex account information. - Updated variable names and response handling to align with the new API structure, improving code clarity and maintainability.
1 parent 9e6e153 commit 4b04ddc

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

frontend/static/js/user.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,35 @@ class UserModule {
3535

3636
async loadUserData() {
3737
try {
38-
const response = await fetch('./api/user');
39-
if (!response.ok) throw new Error('Failed to fetch user data');
38+
// Load user info
39+
const userResponse = await fetch('./api/user/info');
40+
if (!userResponse.ok) throw new Error('Failed to fetch user data');
4041

41-
const data = await response.json();
42+
const userData = await userResponse.json();
4243

4344
// Update username
44-
document.getElementById('currentUsername').textContent = data.username || 'Unknown';
45+
document.getElementById('currentUsername').textContent = userData.username || 'Unknown';
4546

4647
// Update 2FA status
47-
this.update2FAStatus(data.two_fa_enabled);
48+
this.update2FAStatus(userData.is_2fa_enabled);
4849

49-
// Update Plex status
50-
this.updatePlexStatus(data.plex_data);
50+
// Load Plex status
51+
try {
52+
const plexResponse = await fetch('./api/auth/plex/status');
53+
if (plexResponse.ok) {
54+
const plexData = await plexResponse.json();
55+
if (plexData.success) {
56+
this.updatePlexStatus(plexData);
57+
} else {
58+
this.updatePlexStatus(null);
59+
}
60+
} else {
61+
this.updatePlexStatus(null);
62+
}
63+
} catch (plexError) {
64+
console.warn('Error loading Plex status:', plexError);
65+
this.updatePlexStatus(null);
66+
}
5167

5268
} catch (error) {
5369
console.error('Error loading user data:', error);
@@ -65,12 +81,12 @@ class UserModule {
6581
}
6682

6783
try {
68-
const response = await fetch('./api/user/username', {
84+
const response = await fetch('./api/user/change-username', {
6985
method: 'POST',
7086
headers: { 'Content-Type': 'application/json' },
7187
body: JSON.stringify({
72-
new_username: newUsername,
73-
current_password: currentPassword
88+
username: newUsername,
89+
password: currentPassword
7490
})
7591
});
7692

@@ -111,7 +127,7 @@ class UserModule {
111127
}
112128

113129
try {
114-
const response = await fetch('./api/user/password', {
130+
const response = await fetch('./api/user/change-password', {
115131
method: 'POST',
116132
headers: { 'Content-Type': 'application/json' },
117133
body: JSON.stringify({
@@ -137,11 +153,11 @@ class UserModule {
137153

138154
async enableTwoFactor() {
139155
try {
140-
const response = await fetch('./api/auth/2fa/setup', { method: 'POST' });
156+
const response = await fetch('./api/user/2fa/setup', { method: 'POST' });
141157
const result = await response.json();
142158

143159
if (response.ok) {
144-
document.getElementById('qrCode').src = result.qr_code;
160+
document.getElementById('qrCode').src = result.qr_code_url;
145161
document.getElementById('secretKey').textContent = result.secret;
146162

147163
document.getElementById('enableTwoFactorSection').style.display = 'none';
@@ -164,7 +180,7 @@ class UserModule {
164180
}
165181

166182
try {
167-
const response = await fetch('./api/auth/2fa/verify', {
183+
const response = await fetch('./api/user/2fa/verify', {
168184
method: 'POST',
169185
headers: { 'Content-Type': 'application/json' },
170186
body: JSON.stringify({ code })
@@ -197,12 +213,12 @@ class UserModule {
197213
}
198214

199215
try {
200-
const response = await fetch('./api/auth/2fa/disable', {
216+
const response = await fetch('./api/user/2fa/disable', {
201217
method: 'POST',
202218
headers: { 'Content-Type': 'application/json' },
203219
body: JSON.stringify({
204220
password: password,
205-
otp_code: otpCode
221+
code: otpCode
206222
})
207223
});
208224

@@ -313,7 +329,7 @@ class UserModule {
313329

314330
async linkPlexAccount() {
315331
try {
316-
const response = await fetch('./api/plex/pin', { method: 'POST' });
332+
const response = await fetch('./api/auth/plex/pin', { method: 'POST' });
317333
const result = await response.json();
318334

319335
if (response.ok) {
@@ -336,7 +352,7 @@ class UserModule {
336352
startPlexPolling() {
337353
this.plexPollingInterval = setInterval(async () => {
338354
try {
339-
const response = await fetch(`./api/plex/token/${this.plexPinId}`, { method: 'POST' });
355+
const response = await fetch(`./api/auth/plex/check/${this.plexPinId}`, { method: 'GET' });
340356
const result = await response.json();
341357

342358
if (response.ok && result.success) {
@@ -377,7 +393,7 @@ class UserModule {
377393
}
378394

379395
try {
380-
const response = await fetch('./api/plex/unlink', { method: 'POST' });
396+
const response = await fetch('./api/auth/plex/unlink', { method: 'POST' });
381397
const result = await response.json();
382398

383399
if (response.ok) {
@@ -428,13 +444,13 @@ class UserModule {
428444

429445
statusBadge.style.display = 'inline-block';
430446

431-
if (plexData && plexData.username) {
447+
if (plexData && plexData.plex_linked) {
432448
statusBadge.textContent = 'Linked';
433449
statusBadge.className = 'status-badge enabled';
434450

435-
document.getElementById('plexUsername').textContent = plexData.username;
436-
document.getElementById('plexEmail').textContent = plexData.email || 'N/A';
437-
document.getElementById('plexLinkedAt').textContent = plexData.linked_at || 'Unknown';
451+
document.getElementById('plexUsername').textContent = plexData.plex_username || 'Unknown';
452+
document.getElementById('plexEmail').textContent = plexData.plex_email || 'N/A';
453+
document.getElementById('plexLinkedAt').textContent = plexData.plex_linked_at || 'Unknown';
438454

439455
notLinkedSection.style.display = 'none';
440456
linkedSection.style.display = 'block';

0 commit comments

Comments
 (0)