Skip to content

Commit 1322acf

Browse files
committed
Enhance user data handling and localStorage management
- Added cleanup logic for stale localStorage flags in user.js to prevent interference with user data loading. - Improved timestamp formatting for Plex linking status in user.js, ensuring better readability. - Updated index.html to refine redirection logic based on Plex linking state, allowing for smoother user experience. - Modified plex_auth_routes.py to ensure proper navigation without triggering localStorage redirects during Plex authentication callback.
1 parent f61db7f commit 1322acf

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

frontend/static/js/user.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class UserModule {
3535

3636
async loadUserData() {
3737
try {
38+
// Clean up any stale localStorage flags that might interfere
39+
this.cleanupStaleFlags();
40+
3841
// Load user info
3942
const userResponse = await fetch('./api/user/info', { credentials: 'include' });
4043
if (!userResponse.ok) throw new Error('Failed to fetch user data');
@@ -375,6 +378,7 @@ class UserModule {
375378
localStorage.setItem('huntarr-plex-pin-id', this.currentPlexPinId);
376379
localStorage.setItem('huntarr-plex-linking', 'true');
377380
localStorage.setItem('huntarr-plex-user-mode', 'true');
381+
localStorage.setItem('huntarr-plex-linking-timestamp', Date.now().toString());
378382

379383
// Redirect to Plex authentication
380384
setTimeout(() => {
@@ -526,6 +530,7 @@ class UserModule {
526530
localStorage.removeItem('huntarr-plex-linking');
527531
localStorage.removeItem('huntarr-plex-pin-id');
528532
localStorage.removeItem('huntarr-plex-user-mode');
533+
localStorage.removeItem('huntarr-plex-linking-timestamp');
529534

530535
// Show modal and start checking
531536
document.getElementById('plexLinkModal').style.display = 'block';
@@ -617,7 +622,20 @@ class UserModule {
617622

618623
document.getElementById('plexUsername').textContent = plexData.plex_username || 'Unknown';
619624
document.getElementById('plexEmail').textContent = plexData.plex_email || 'N/A';
620-
document.getElementById('plexLinkedAt').textContent = plexData.plex_linked_at || 'Unknown';
625+
626+
// Format the timestamp properly
627+
let linkedAtText = 'Unknown';
628+
if (plexData.plex_linked_at) {
629+
try {
630+
const timestamp = plexData.plex_linked_at;
631+
const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
632+
linkedAtText = date.toLocaleString(); // Format as readable date/time
633+
} catch (error) {
634+
console.error('Error formatting plex_linked_at timestamp:', error);
635+
linkedAtText = 'Invalid Date';
636+
}
637+
}
638+
document.getElementById('plexLinkedAt').textContent = linkedAtText;
621639

622640
notLinkedSection.style.display = 'none';
623641
linkedSection.style.display = 'block';
@@ -630,6 +648,34 @@ class UserModule {
630648
}
631649
}
632650

651+
cleanupStaleFlags() {
652+
// Clean up any localStorage flags that might interfere with normal operation
653+
const flagsToClean = [
654+
'huntarr-plex-login',
655+
'huntarr-plex-setup-mode'
656+
];
657+
658+
flagsToClean.forEach(flag => {
659+
if (localStorage.getItem(flag)) {
660+
console.log(`[UserModule] Cleaning up stale localStorage flag: ${flag}`);
661+
localStorage.removeItem(flag);
662+
}
663+
});
664+
665+
// Only clean up Plex linking flags if they're older than 10 minutes (stale)
666+
const plexLinkingTimestamp = localStorage.getItem('huntarr-plex-linking-timestamp');
667+
if (plexLinkingTimestamp) {
668+
const tenMinutesAgo = Date.now() - (10 * 60 * 1000);
669+
if (parseInt(plexLinkingTimestamp) < tenMinutesAgo) {
670+
console.log('[UserModule] Cleaning up stale Plex linking flags (older than 10 minutes)');
671+
localStorage.removeItem('huntarr-plex-linking');
672+
localStorage.removeItem('huntarr-plex-pin-id');
673+
localStorage.removeItem('huntarr-plex-user-mode');
674+
localStorage.removeItem('huntarr-plex-linking-timestamp');
675+
}
676+
}
677+
}
678+
633679
showStatus(element, message, type) {
634680
element.textContent = message;
635681
element.className = `status-message ${type}`;

frontend/templates/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@
107107
window.location.href = './login';
108108
} else if (plexLinking === 'true' && plexSetupMode === 'true') {
109109
// Only redirect to setup page for setup mode
110-
// User mode now goes directly from callback to user page
111110
window.location.href = './setup';
111+
} else if (plexLinking === 'true' && plexUserMode === 'true') {
112+
// For user mode, stay on current page and let user.js handle it
113+
// Do NOT redirect - let the UserModule.checkPlexReturn() handle the flow
114+
console.log('Detected Plex user mode return - letting UserModule handle it');
112115
}
113116
});
114117
</script>

src/primary/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def authenticate_request():
469469
logger.warning(f"Access from {remote_addr} is not recognized as local network - Authentication required")
470470

471471
# Check for valid session
472-
session_id = session.get(SESSION_COOKIE_NAME)
472+
session_id = request.cookies.get(SESSION_COOKIE_NAME)
473473
if session_id and verify_session(session_id):
474474
if not is_polling_endpoint:
475475
pass # Session valid - debug spam removed

src/primary/routes/plex_auth_routes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ def plex_status():
491491
@plex_auth_bp.route('/auth/plex/callback')
492492
def plex_callback():
493493
"""Handle Plex authentication callback (redirect back to app)"""
494-
# Check localStorage to determine if this is setup mode or user mode
495-
# For now, redirect directly to user page to avoid double flash
496-
# TODO: Could be enhanced to check localStorage and route appropriately
497-
return redirect('/user')
494+
# Redirect to main page with user hash to avoid index.html redirect conflicts
495+
# This ensures proper navigation without triggering localStorage redirects
496+
return redirect('/#user')

0 commit comments

Comments
 (0)