@@ -1246,15 +1246,29 @@ func startPersistentServer(cfg *config.Config, log logger.Logger, tokenManager *
12461246 fmt .Printf ("🔗 Redirect URI: %s\n " , cfg .Auth .RedirectURI )
12471247 fmt .Printf ("🌐 Server running on: http://0.0.0.0:%d\n " , port )
12481248
1249- // Generate and display auth URL
1250- authURL , state , err := oauthMgr .GenerateAuthURL ()
1251- if err != nil {
1249+ // Global state management for OAuth
1250+ var currentState string
1251+ var currentAuthURL string
1252+
1253+ // Function to generate fresh auth URL and state
1254+ generateFreshAuth := func () error {
1255+ authURL , state , err := oauthMgr .GenerateAuthURL ()
1256+ if err != nil {
1257+ return err
1258+ }
1259+ currentAuthURL = authURL
1260+ currentState = state
1261+ return nil
1262+ }
1263+
1264+ // Generate initial auth URL
1265+ if err := generateFreshAuth (); err != nil {
12521266 return fmt .Errorf ("failed to generate auth URL: %w" , err )
12531267 }
12541268
12551269 fmt .Println ("\n 🔗 OAUTH AUTHENTICATION:" )
12561270 fmt .Println ("Open this URL in your browser to authenticate:" )
1257- fmt .Printf (" %s\n " , authURL )
1271+ fmt .Printf (" %s\n " , currentAuthURL )
12581272 fmt .Println ("\n After authentication, you can:" )
12591273 fmt .Printf (" • Visit http://192.168.1.24:%d/status for token status\n " , port )
12601274 fmt .Printf (" • Visit http://192.168.1.24:%d/export/watched for exports\n " , port )
@@ -1309,7 +1323,7 @@ func startPersistentServer(cfg *config.Config, log logger.Logger, tokenManager *
13091323 }
13101324
13111325 // Exchange code for token
1312- token , err := oauthMgr .ExchangeCodeForToken (code , state , receivedState )
1326+ token , err := oauthMgr .ExchangeCodeForToken (code , currentState , receivedState )
13131327 if err != nil {
13141328 log .Error ("server.token_exchange_failed" , map [string ]interface {}{
13151329 "error" : err .Error (),
@@ -1389,7 +1403,7 @@ setTimeout(function() {
13891403 return
13901404 }
13911405
1392- w .Header ().Set ("Content-Type" , "text/html" )
1406+ w .Header ().Set ("Content-Type" , "text/html; charset=utf-8 " )
13931407 if status .HasToken && status .IsValid {
13941408 fmt .Fprintf (w , `
13951409<!DOCTYPE html>
@@ -1426,13 +1440,13 @@ setTimeout(function() {
14261440
14271441 // Auth URL endpoint
14281442 http .HandleFunc ("/auth-url" , func (w http.ResponseWriter , r * http.Request ) {
1429- authURL , _ , err := oauthMgr . GenerateAuthURL ()
1430- if err != nil {
1443+ // Generate fresh auth URL and update current state
1444+ if err := generateFreshAuth (); err != nil {
14311445 http .Error (w , fmt .Sprintf ("Error generating auth URL: %s" , err .Error ()), http .StatusInternalServerError )
14321446 return
14331447 }
14341448
1435- w .Header ().Set ("Content-Type" , "text/html" )
1449+ w .Header ().Set ("Content-Type" , "text/html; charset=utf-8 " )
14361450 fmt .Fprintf (w , `
14371451<!DOCTYPE html>
14381452<html>
@@ -1443,7 +1457,7 @@ setTimeout(function() {
14431457<p><a href="%s" target="_blank" style="background: #e74c3c; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">Authenticate with Trakt.tv</a></p>
14441458<p><small>After authentication, you'll be redirected back to this server automatically.</small></p>
14451459</body>
1446- </html>` , authURL )
1460+ </html>` , currentAuthURL )
14471461 })
14481462
14491463 // Export endpoints
@@ -1456,7 +1470,7 @@ setTimeout(function() {
14561470 // Check authentication
14571471 status , err := tokenManager .GetTokenStatus ()
14581472 if err != nil || ! status .HasToken || ! status .IsValid {
1459- w .Header ().Set ("Content-Type" , "text/html" )
1473+ w .Header ().Set ("Content-Type" , "text/html; charset=utf-8 " )
14601474 fmt .Fprint (w , `
14611475<!DOCTYPE html>
14621476<html>
@@ -1470,7 +1484,7 @@ setTimeout(function() {
14701484 return
14711485 }
14721486
1473- w .Header ().Set ("Content-Type" , "text/html" )
1487+ w .Header ().Set ("Content-Type" , "text/html; charset=utf-8 " )
14741488 fmt .Fprintf (w , `
14751489<!DOCTYPE html>
14761490<html>
@@ -1508,7 +1522,7 @@ setTimeout(function() {
15081522 return
15091523 }
15101524
1511- w .Header ().Set ("Content-Type" , "text/html" )
1525+ w .Header ().Set ("Content-Type" , "text/html; charset=utf-8 " )
15121526 fmt .Fprintf (w , `
15131527<!DOCTYPE html>
15141528<html>
@@ -1531,7 +1545,7 @@ setTimeout(function() {
15311545<h3>📱 Quick Authentication:</h3>
15321546<p>Open this URL to authenticate: <br><a href="%s">%s</a></p>
15331547</body>
1534- </html>` , authURL , authURL )
1548+ </html>` , currentAuthURL , currentAuthURL )
15351549 })
15361550
15371551 // Start server
@@ -1542,7 +1556,7 @@ setTimeout(function() {
15421556
15431557 log .Info ("server.starting" , map [string ]interface {}{
15441558 "port" : port ,
1545- "auth_url" : authURL ,
1559+ "auth_url" : currentAuthURL ,
15461560 })
15471561
15481562 // Handle graceful shutdown
0 commit comments