Skip to content

Commit b6b9e01

Browse files
committed
Refactor Plex account linking process and update user interface
- Removed the display of the PIN code in the user interface and replaced it with a direct link to the Plex authentication URL, enhancing user experience. - Updated instructions for linking a Plex account to reflect the new authentication flow. - Modified the backend logic to utilize a database for storing Plex account information, improving data management and error handling.
1 parent bbfee7a commit b6b9e01

File tree

4 files changed

+14
-24
lines changed

4 files changed

+14
-24
lines changed

frontend/static/js/user.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ class UserModule {
342342
const result = await response.json();
343343

344344
if (response.ok) {
345-
document.getElementById('plexLinkPinCode').textContent = result.pin;
345+
// Open Plex auth URL in new window
346+
window.open(result.auth_url, '_blank');
347+
346348
document.getElementById('plexLinkStatus').textContent = 'Waiting for authentication...';
347349
document.getElementById('plexLinkModal').style.display = 'flex';
348350

frontend/templates/components/user_section.html

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,10 @@ <h3><i class="fas fa-tv"></i> Plex Account</h3>
184184
</div>
185185
<h2>Link Plex Account</h2>
186186
</div>
187-
<div class="plex-pin-display">
188-
<p>PIN Code:</p>
189-
<p id="plexLinkPinCode" class="plex-pin-code"></p>
190-
</div>
191187
<div class="plex-instructions">
192-
<p>1. Go to <a href="https://plex.tv/link" target="_blank" rel="noopener noreferrer">plex.tv/link</a></p>
193-
<p>2. Enter the PIN code above</p>
194-
<p>3. Wait for the link to complete</p>
188+
<p>1. A new tab/window has opened to Plex authentication</p>
189+
<p>2. Sign in to your Plex account and authorize Huntarr</p>
190+
<p>3. Return to this window and wait for the link to complete</p>
195191
</div>
196192
<div id="plexLinkStatus" class="plex-status waiting"></div>
197193
<div class="modal-actions">

src/primary/auth.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,32 +1085,25 @@ def link_plex_account_session_auth(username: str, plex_token: str, plex_user_dat
10851085
Link a Plex account to an existing local user using session authentication
10861086
10871087
Args:
1088-
username: Not used for validation - kept for compatibility
1088+
username: Username from session authentication
10891089
plex_token: Plex access token
10901090
plex_user_data: User data from Plex API
10911091
10921092
Returns:
10931093
bool: True if account linked successfully
10941094
"""
10951095
try:
1096-
user_data = get_user_data()
1097-
1098-
# No need to validate username since user is already authenticated via session
1099-
# Just proceed to add Plex information
1096+
from src.primary.utils.database import get_database
1097+
db = get_database()
11001098

1101-
# Prepare Plex user data for storage - add linked timestamp
1102-
plex_data_to_store = plex_user_data.copy()
1103-
plex_data_to_store['linked_at'] = time.time()
1099+
# Use database approach instead of JSON files
1100+
success = db.update_user_plex(username, plex_token, plex_user_data)
11041101

1105-
# Store Plex information using database schema fields
1106-
user_data["plex_token"] = plex_token
1107-
user_data["plex_user_data"] = plex_data_to_store
1108-
1109-
if save_user_data(user_data):
1110-
logger.info(f"Plex account linked to authenticated user - Plex username: {plex_user_data.get('username')}")
1102+
if success:
1103+
logger.info(f"Plex account linked to user {username} - Plex username: {plex_user_data.get('username')}")
11111104
return True
11121105
else:
1113-
logger.error("Failed to save linked Plex data")
1106+
logger.error("Failed to update user Plex data in database")
11141107
return False
11151108

11161109
except Exception as e:

src/primary/routes/plex_auth_routes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def create_pin():
3838
return jsonify({
3939
'success': True,
4040
'pin_id': pin_data['id'],
41-
'pin': pin_data['code'],
4241
'auth_url': pin_data['auth_url']
4342
})
4443
else:

0 commit comments

Comments
 (0)