@@ -858,12 +858,12 @@ def verify_plex_user(plex_token: str) -> Tuple[bool, Optional[Dict[str, Any]]]:
858858 else :
859859 return False , None
860860
861- def unlink_plex_from_user (username : str ) -> bool :
861+ def unlink_plex_from_user (username : str = None ) -> bool :
862862 """
863- Unlink Plex account from a user by removing Plex-related data
863+ Unlink Plex account from the current authenticated user by removing Plex-related data
864864
865865 Args:
866- username: The username to unlink Plex from
866+ username: Not used anymore - kept for compatibility
867867
868868 Returns:
869869 bool: True if successful, False otherwise
@@ -876,20 +876,20 @@ def unlink_plex_from_user(username: str) -> bool:
876876 with open (USER_FILE , 'r' ) as f :
877877 user_data = json .load (f )
878878
879- username_hash = hash_username (username )
880-
881- # Check if this user exists and has Plex data
882- if user_data .get ("username" ) != username_hash :
883- logger .error ("User not found" )
884- return False
879+ # No need to validate username since user is already authenticated via session
880+ # Just check if Plex data exists to unlink
881+ if not user_data .get ('plex_linked' , False ):
882+ logger .warning ("No Plex account linked to unlink" )
883+ return True # Not an error, just nothing to do
885884
886885 # Remove all Plex-related fields
887886 plex_fields_to_remove = [
888887 'plex_token' ,
889888 'plex_username' ,
890889 'plex_email' ,
891890 'plex_linked_at' ,
892- 'plex_linked'
891+ 'plex_linked' ,
892+ 'plex_user_id'
893893 ]
894894
895895 removed_any = False
@@ -919,19 +919,19 @@ def unlink_plex_from_user(username: str) -> bool:
919919 # Set appropriate file permissions
920920 os .chmod (USER_FILE , 0o600 )
921921
922- logger .info (f"Successfully unlinked Plex account for user { username } " )
922+ logger .info (f"Successfully unlinked Plex account from authenticated user " )
923923 return True
924924
925925 except Exception as e :
926- logger .error (f"Error unlinking Plex account for user { username } : { str (e )} " )
927- return False
926+ logger .error (f"Error unlinking Plex account: { str (e )} " )
927+ raise # Re-raise the exception so the route handler can catch it
928928
929929def link_plex_account_session_auth (username : str , plex_token : str , plex_user_data : Dict [str , Any ]) -> bool :
930930 """
931931 Link a Plex account to an existing local user using session authentication
932932
933933 Args:
934- username: Local username (already verified via session)
934+ username: Not used for validation - kept for compatibility
935935 plex_token: Plex access token
936936 plex_user_data: User data from Plex API
937937
@@ -941,11 +941,8 @@ def link_plex_account_session_auth(username: str, plex_token: str, plex_user_dat
941941 try :
942942 user_data = get_user_data ()
943943
944- # Verify this is the correct user by checking username hash
945- username_hash = hash_username (username )
946- if user_data .get ("username" ) != username_hash :
947- logger .warning (f"Failed to link Plex account: Username mismatch for { username } " )
948- return False
944+ # No need to validate username since user is already authenticated via session
945+ # Just proceed to add Plex information
949946
950947 # Add Plex information to existing user
951948 user_data ["plex_linked" ] = True
@@ -956,7 +953,7 @@ def link_plex_account_session_auth(username: str, plex_token: str, plex_user_dat
956953 user_data ["plex_linked_at" ] = time .time ()
957954
958955 if save_user_data (user_data ):
959- logger .info (f"Plex account linked to local user: { username } " )
956+ logger .info (f"Plex account linked to authenticated user - Plex username : { plex_user_data . get ( ' username' ) } " )
960957 return True
961958 else :
962959 logger .error ("Failed to save linked Plex data" )
0 commit comments