@@ -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 } ` ;
0 commit comments