Skip to content

Commit 239d350

Browse files
chore(logout): add extensive logging to diagnose deep-link issue
Add comprehensive logging throughout logout flow to identify why deep-link callback is not triggered after browser logout. This will help identify if the issue is: - Deep-link not being written to registry by browser - Deep-link not being read from registry by Unity - Deep-link not matching expected logout URI - Callback not being invoked after deeplink receipt
1 parent 702b6fb commit 239d350

4 files changed

Lines changed: 120 additions & 11 deletions

File tree

src/Packages/Passport/Runtime/Scripts/Private/Helpers/WindowsDeepLink.cs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,37 @@ private static extern int RegQueryValueEx(
8282
/// <param name="callback">Callback to invoke when a deep link is received</param>
8383
public static void Initialise(string redirectUri, Action<string> callback)
8484
{
85+
PassportLogger.Info($"[WindowsDeepLink] ===== INITIALISE CALLED =====");
86+
PassportLogger.Info($"[WindowsDeepLink] redirectUri: {redirectUri}");
87+
PassportLogger.Info($"[WindowsDeepLink] callback: {(callback == null ? "NULL" : "PROVIDED")}");
88+
PassportLogger.Info($"[WindowsDeepLink] _instance: {(_instance == null ? "NULL - will create new" : "EXISTS - will reuse")}");
89+
8590
if (_instance == null)
8691
{
8792
_instance = new GameObject(nameof(WindowsDeepLink)).AddComponent<WindowsDeepLink>();
8893
DontDestroyOnLoad(_instance.gameObject);
94+
PassportLogger.Info($"[WindowsDeepLink] Created new WindowsDeepLink GameObject");
8995
}
9096

91-
if (string.IsNullOrEmpty(redirectUri)) return;
97+
if (string.IsNullOrEmpty(redirectUri))
98+
{
99+
PassportLogger.Warn($"[WindowsDeepLink] redirectUri is null or empty! Returning early.");
100+
return;
101+
}
92102

93103
// Extract protocol name from URI (e.g. "immutable" from "immutable://")
94104
var protocolName = redirectUri.Split(new[] { "://" }, StringSplitOptions.None)[0];
105+
PassportLogger.Info($"[WindowsDeepLink] Extracted protocol name: '{protocolName}'");
106+
95107
_instance._protocolName = protocolName;
96108
_instance._callback = callback;
97109

98110
// Register protocol and create handler script
111+
PassportLogger.Info($"[WindowsDeepLink] Registering protocol '{protocolName}'...");
99112
RegisterProtocol(protocolName);
113+
PassportLogger.Info($"[WindowsDeepLink] Creating command script for protocol '{protocolName}'...");
100114
CreateCommandScript(protocolName);
115+
PassportLogger.Info($"[WindowsDeepLink] ===== INITIALISE COMPLETED =====");
101116
}
102117

103118
private static void CreateCommandScript(string protocolName)
@@ -341,9 +356,12 @@ private static string GetGameExecutablePath(string suffix)
341356

342357
private void OnApplicationFocus(bool hasFocus)
343358
{
359+
PassportLogger.Debug($"[WindowsDeepLink] OnApplicationFocus: hasFocus={hasFocus}");
360+
344361
// Only handle deeplink when application regains focus
345362
if (!hasFocus) return;
346363

364+
PassportLogger.Info($"[WindowsDeepLink] Application gained focus - checking for deeplink...");
347365
HandleDeeplink();
348366
}
349367

@@ -356,11 +374,14 @@ private void Update()
356374
if (Time.unscaledTime < _nextPollAt) return;
357375
_nextPollAt = Time.unscaledTime + POLL_INTERVAL_SECONDS;
358376

377+
PassportLogger.Debug($"[WindowsDeepLink] Polling for deeplink (every {POLL_INTERVAL_SECONDS}s)...");
359378
HandleDeeplink();
360379
}
361380

362381
private void HandleDeeplink()
363382
{
383+
PassportLogger.Debug($"[WindowsDeepLink] HandleDeeplink called - checking registry for protocol '{_protocolName}'");
384+
364385
// Open registry key for the protocol
365386
var registryPath = $@"Software\Classes\{_protocolName}";
366387
UIntPtr hKey;
@@ -373,10 +394,12 @@ private void HandleDeeplink()
373394

374395
if (result != 0)
375396
{
376-
PassportLogger.Error($"Failed to open registry key. Error code: {result}");
397+
PassportLogger.Error($"[WindowsDeepLink] Failed to open registry key '{registryPath}'. Error code: {result}");
377398
return;
378399
}
379400

401+
PassportLogger.Debug($"[WindowsDeepLink] Successfully opened registry key '{registryPath}'");
402+
380403
// Get size of deeplink data
381404
uint type = 0;
382405
uint dataSize = 0;
@@ -388,11 +411,17 @@ private void HandleDeeplink()
388411
// No deeplink written yet; don't spam logs on each poll.
389412
if (result != ERROR_FILE_NOT_FOUND)
390413
{
391-
PassportLogger.Warn($"Failed to get deeplink data size. Error code: {result}");
414+
PassportLogger.Warn($"[WindowsDeepLink] Failed to get deeplink data size from registry. Error code: {result}");
415+
}
416+
else
417+
{
418+
PassportLogger.Debug($"[WindowsDeepLink] No deeplink found in registry yet (ERROR_FILE_NOT_FOUND)");
392419
}
393420
return;
394421
}
395422

423+
PassportLogger.Info($"[WindowsDeepLink] ✓ Found deeplink in registry! dataSize={dataSize}");
424+
396425
// Read deeplink data
397426
var data = new byte[dataSize];
398427
result = RegQueryValueEx(hKey, REGISTRY_DEEP_LINK_NAME, IntPtr.Zero, ref type, data, ref dataSize);
@@ -402,20 +431,24 @@ private void HandleDeeplink()
402431
{
403432
// Convert and validate URI
404433
var uri = System.Text.Encoding.Unicode.GetString(data, 0, (int)dataSize - 2); // Remove null terminator
434+
PassportLogger.Info($"[WindowsDeepLink] Read deeplink URI from registry: '{uri}'");
435+
405436
if (_protocolName != null && !uri.StartsWith(_protocolName))
406437
{
407-
PassportLogger.Error($"Incorrect prefix uri {uri}");
438+
PassportLogger.Error($"[WindowsDeepLink] ✗ Incorrect protocol prefix! Expected '{_protocolName}://' but got '{uri}'");
408439
}
409440
else
410441
{
411442
// Invoke callback with valid URI
443+
PassportLogger.Info($"[WindowsDeepLink] ✓ Valid URI - invoking callback...");
412444
_callback?.Invoke(uri);
413445
callbackInvoked = true;
446+
PassportLogger.Info($"[WindowsDeepLink] Callback invoked successfully");
414447
}
415448
}
416449
else
417450
{
418-
PassportLogger.Warn($"Failed to get registry key. Error code: {result}");
451+
PassportLogger.Warn($"[WindowsDeepLink] Failed to read deeplink from registry. Error code: {result}, type: {type}");
419452
}
420453

421454
// Clean up registry handle
@@ -424,37 +457,41 @@ private void HandleDeeplink()
424457
// Delete registry key if callback was invoked
425458
if (callbackInvoked)
426459
{
460+
PassportLogger.Info($"[WindowsDeepLink] Deleting registry key '{registryPath}'...");
427461
result = RegDeleteTree((UIntPtr)HKEY_CURRENT_USER, registryPath);
428462

429463
if (result != 0)
430464
{
431-
PassportLogger.Warn($"Failed to delete registry key. Error code: {result}");
465+
PassportLogger.Warn($"[WindowsDeepLink] Failed to delete registry key. Error code: {result}");
432466
}
433467
else
434468
{
435-
PassportLogger.Debug("Successfully deleted registry key.");
469+
PassportLogger.Info($"[WindowsDeepLink] Successfully deleted registry key.");
436470
}
437471
}
438472
else
439473
{
440-
PassportLogger.Debug("Did not invoke callback so not deleting registry key.");
474+
PassportLogger.Debug($"[WindowsDeepLink] Did not invoke callback so not deleting registry key.");
441475
}
442476

443477
// Clean up command script
444478
var cmdPath = GetGameExecutablePath(".cmd");
445479
if (File.Exists(cmdPath))
446480
{
481+
PassportLogger.Info($"[WindowsDeepLink] Deleting command script: {cmdPath}");
447482
try
448483
{
449484
File.Delete(cmdPath);
485+
PassportLogger.Info($"[WindowsDeepLink] Command script deleted successfully");
450486
}
451487
catch (Exception ex)
452488
{
453-
PassportLogger.Warn($"Failed to delete script: {ex.Message}");
489+
PassportLogger.Warn($"[WindowsDeepLink] Failed to delete script: {ex.Message}");
454490
}
455491
}
456492

457493
// Clean up instance
494+
PassportLogger.Info($"[WindowsDeepLink] Destroying WindowsDeepLink GameObject and clearing instance");
458495
Destroy(gameObject);
459496
_instance = null;
460497
}

src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,30 +251,51 @@ public async void OnDeepLinkActivated(string url)
251251
{
252252
try
253253
{
254+
PassportLogger.Info($"{TAG} ===== DEEPLINK RECEIVED =====");
254255
PassportLogger.Info($"{TAG} Received deeplink URL: {url}");
256+
PassportLogger.Info($"{TAG} _redirectUri={_redirectUri}, _logoutRedirectUri={_logoutRedirectUri}");
255257

256258
Uri uri = new Uri(url);
257259
string hostWithPort = uri.IsDefaultPort ? uri.Host : $"{uri.Host}:{uri.Port}";
258260

259261
string domain = $"{uri.Scheme}://{hostWithPort}{uri.AbsolutePath}";
262+
263+
PassportLogger.Info($"{TAG} Parsed domain from deeplink: {domain}");
260264

261265
if (domain.EndsWith("/"))
262266
{
263267
domain = domain.Remove(domain.Length - 1);
268+
PassportLogger.Info($"{TAG} Removed trailing slash from domain: {domain}");
264269
}
265270

271+
PassportLogger.Info($"{TAG} Comparing domain '{domain}' with:");
272+
PassportLogger.Info($"{TAG} - _logoutRedirectUri: '{_logoutRedirectUri}'");
273+
PassportLogger.Info($"{TAG} - _redirectUri: '{_redirectUri}'");
274+
266275
if (domain.Equals(_logoutRedirectUri))
267276
{
277+
PassportLogger.Info($"{TAG} ✓ LOGOUT deeplink match! Calling HandleLogoutPkceSuccess...");
268278
HandleLogoutPkceSuccess();
269279
}
270280
else if (domain.Equals(_redirectUri))
271281
{
282+
PassportLogger.Info($"{TAG} ✓ LOGIN deeplink match! Calling CompleteLoginPKCEFlow...");
272283
await CompleteLoginPKCEFlow(url);
273284
}
285+
else
286+
{
287+
PassportLogger.Warn($"{TAG} ✗ Deeplink does NOT match logout or login URI!");
288+
PassportLogger.Warn($"{TAG} Received: '{domain}'");
289+
PassportLogger.Warn($"{TAG} Expected logout: '{_logoutRedirectUri}' OR login: '{_redirectUri}'");
290+
}
291+
292+
PassportLogger.Info($"{TAG} ===== DEEPLINK PROCESSING COMPLETED =====");
274293
}
275294
catch (Exception ex)
276295
{
277-
PassportLogger.Error($"{TAG} Deeplink error {url}: {ex.Message}");
296+
PassportLogger.Error($"{TAG} ===== DEEPLINK ERROR =====");
297+
PassportLogger.Error($"{TAG} Deeplink error for URL {url}: {ex.Message}");
298+
PassportLogger.Error($"{TAG} Stack trace: {ex.StackTrace}");
278299
}
279300
}
280301

@@ -454,20 +475,30 @@ public async UniTask<bool> Logout(bool hardLogout = true)
454475
{
455476
try
456477
{
478+
PassportLogger.Info($"{TAG} ===== LOGOUT FLOW START =====");
479+
PassportLogger.Info($"{TAG} hardLogout={hardLogout}, _logoutRedirectUri={_logoutRedirectUri}");
480+
457481
SendAuthEvent(PassportAuthEvent.LoggingOutPKCE);
458482

459483
var task = new UniTaskCompletionSource<bool>();
460484
_pkceCompletionSource = task;
485+
486+
PassportLogger.Info($"{TAG} Created _pkceCompletionSource for logout");
487+
461488
#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN)
489+
PassportLogger.Info($"{TAG} Initialising WindowsDeepLink for logout with URI: {_logoutRedirectUri}");
462490
WindowsDeepLink.Initialise(_logoutRedirectUri, OnDeepLinkActivated);
491+
PassportLogger.Info($"{TAG} WindowsDeepLink.Initialise completed");
463492
#endif
464493
LaunchLogoutPkceUrl(hardLogout);
494+
PassportLogger.Info($"{TAG} Waiting for logout callback (deep-link or completion)...");
465495
return await task.Task;
466496
}
467497
catch (Exception ex)
468498
{
469499
var errorMessage = $"Failed to log out: {ex.Message}";
470500
PassportLogger.Error($"{TAG} {errorMessage}");
501+
PassportLogger.Error($"{TAG} Stack trace: {ex.StackTrace}");
471502

472503
Track(PassportAnalytics.EventName.COMPLETE_LOGOUT_PKCE, success: false);
473504
SendAuthEvent(PassportAuthEvent.LogoutPKCEFailed);
@@ -477,30 +508,53 @@ public async UniTask<bool> Logout(bool hardLogout = true)
477508

478509
private async void HandleLogoutPkceSuccess()
479510
{
511+
PassportLogger.Info($"{TAG} ===== HANDLE LOGOUT SUCCESS CALLED =====");
512+
PassportLogger.Info($"{TAG} _isLoggedIn={_isLoggedIn}, _pkceCompletionSource={(_pkceCompletionSource == null ? "NULL" : "EXISTS")}");
513+
480514
await UniTask.SwitchToMainThread();
515+
516+
PassportLogger.Info($"{TAG} Switched to main thread");
517+
481518
if (_isLoggedIn)
482519
{
520+
PassportLogger.Info($"{TAG} User was logged in, setting PKCE result to true");
483521
TrySetPkceResult(true);
484522
}
523+
else
524+
{
525+
PassportLogger.Warn($"{TAG} User was NOT logged in when HandleLogoutPkceSuccess was called!");
526+
}
527+
485528
Track(PassportAnalytics.EventName.COMPLETE_LOGOUT_PKCE, success: true);
486529
SendAuthEvent(PassportAuthEvent.LogoutPKCESuccess);
530+
487531
_isLoggedIn = false;
488532
_pkceCompletionSource = null;
533+
534+
PassportLogger.Info($"{TAG} ===== LOGOUT SUCCESS COMPLETED =====");
489535
}
490536

491537
private async void LaunchLogoutPkceUrl(bool hardLogout)
492538
{
539+
PassportLogger.Info($"{TAG} LaunchLogoutPkceUrl called with hardLogout={hardLogout}");
540+
493541
var logoutUrl = await GetLogoutUrl();
542+
543+
PassportLogger.Info($"{TAG} Got logout URL: {logoutUrl}");
544+
494545
if (hardLogout)
495546
{
547+
PassportLogger.Info($"{TAG} Performing hard logout - launching browser...");
496548
#if UNITY_ANDROID && !UNITY_EDITOR
497549
LaunchAndroidUrl(logoutUrl);
498550
#else
499551
_communicationsManager.LaunchAuthURL(logoutUrl, _logoutRedirectUri);
500552
#endif
553+
PassportLogger.Info($"{TAG} Browser launched with logout URL. Waiting for deep-link callback...");
501554
}
502555
else
503556
{
557+
PassportLogger.Info($"{TAG} Performing soft logout (no browser) - calling HandleLogoutPkceSuccess directly");
504558
HandleLogoutPkceSuccess();
505559
}
506560
}

src/Packages/Passport/Runtime/Scripts/Public/Passport.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,18 @@ private PassportImpl GetPassportImpl()
628628

629629
private void OnDeepLinkActivated(string url)
630630
{
631+
PassportLogger.Info($"{TAG} [Passport.OnDeepLinkActivated] Deeplink received: {url}");
631632
_deeplink = url;
632633

633634
if (_passportImpl != null)
634635
{
636+
PassportLogger.Info($"{TAG} [Passport.OnDeepLinkActivated] Forwarding to PassportImpl...");
635637
GetPassportImpl().OnDeepLinkActivated(url);
636638
}
639+
else
640+
{
641+
PassportLogger.Warn($"{TAG} [Passport.OnDeepLinkActivated] _passportImpl is NULL - cannot forward deeplink!");
642+
}
637643
}
638644

639645
private void OnPassportAuthEvent(PassportAuthEvent authEvent)

src/Packages/Passport/Samples~/SamplesScenesScripts/Scripts/Passport/Logout/LogoutScript.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,26 @@ private async UniTaskVoid LogoutAsync()
2222
}
2323
try
2424
{
25+
Debug.Log($"[LogoutScript] ===== LOGOUT BUTTON CLICKED =====");
26+
Debug.Log($"[LogoutScript] Starting logout process...");
27+
2528
await Passport.Instance.Logout();
29+
30+
Debug.Log($"[LogoutScript] Logout completed successfully");
31+
Debug.Log($"[LogoutScript] Setting connection flags to false...");
32+
2633
SampleAppManager.IsConnectedToImx = false;
2734
SampleAppManager.IsConnectedToZkEvm = false;
35+
36+
Debug.Log($"[LogoutScript] Navigating to UnauthenticatedScene...");
2837
AuthenticatedSceneManager.NavigateToUnauthenticatedScene();
38+
Debug.Log($"[LogoutScript] ===== LOGOUT FLOW COMPLETED =====");
2939
}
3040
catch (System.Exception ex)
3141
{
32-
Debug.LogError($"Failed to logout: {ex.Message}");
42+
Debug.LogError($"[LogoutScript] ===== LOGOUT FAILED =====");
43+
Debug.LogError($"[LogoutScript] Failed to logout: {ex.Message}");
44+
Debug.LogError($"[LogoutScript] Stack trace: {ex.StackTrace}");
3345
}
3446
}
3547
}

0 commit comments

Comments
 (0)