44using UnityEngine . SceneManagement ;
55using Immutable . Passport ;
66using Immutable . Passport . Core . Logging ;
7-
7+ using Cysharp . Threading . Tasks ;
8+
9+ /// <summary>
10+ /// Sample script demonstrating how to initialize Passport using the PassportUI prefab.
11+ /// This is the recommended approach for quick setup - configure Passport settings
12+ /// in the PassportUI Inspector, then call InitializeWithPassport().
13+ ///
14+ /// For advanced scenarios without UI, see PassportInitialisationScript.cs instead.
15+ /// </summary>
816public class PassportInitialisationWithUIScript : MonoBehaviour
917{
1018#pragma warning disable CS8618
@@ -21,25 +29,6 @@ void Start()
2129
2230 private async void InitialisePassport ( )
2331 {
24- string redirectUri ;
25- string logoutRedirectUri ;
26-
27- #if UNITY_WEBGL
28- var url = Application . absoluteURL ;
29- var uri = new Uri ( url ) ;
30- var scheme = uri . Scheme ;
31- var hostWithPort = uri . IsDefaultPort ? uri . Host : $ "{ uri . Host } :{ uri . Port } ";
32- var fullPath = uri . AbsolutePath . EndsWith ( "/" )
33- ? uri . AbsolutePath
34- : uri . AbsolutePath . Substring ( 0 , uri . AbsolutePath . LastIndexOf ( '/' ) + 1 ) ;
35-
36- redirectUri = $ "{ scheme } ://{ hostWithPort } { fullPath } callback.html";
37- logoutRedirectUri = $ "{ scheme } ://{ hostWithPort } { fullPath } logout.html";
38- #else
39- redirectUri = "immutablerunner://callback" ;
40- logoutRedirectUri = "immutablerunner://logout" ;
41- #endif
42-
4332 try
4433 {
4534 // Set the log level for the SDK
@@ -48,27 +37,43 @@ private async void InitialisePassport()
4837 // Don't redact token values from logs
4938 Passport . RedactTokensInLogs = false ;
5039
51- // Initialise Passport with UI support enabled
52- const string environment = Immutable . Passport . Model . Environment . SANDBOX ;
53- // const string clientId = "mp6rxfMDwwZDogcdgNrAaHnG0qMlXuMK";
54- const string clientId = "IllW5pJ54DShXtaSXzaAlghm40uQjptd" ;
55- var passport = await Passport . Init ( clientId , environment , redirectUri , logoutRedirectUri ) ;
56- SampleAppManager . PassportInstance = passport ;
57-
58- // Find and initialize PassportUI at runtime
40+ // Find PassportUI component
5941 passportUI = FindObjectOfType < PassportUI > ( ) ;
60- if ( passportUI != null )
42+ if ( passportUI == null )
43+ {
44+ Debug . LogError ( "PassportUI component not found in scene - UI login will not be available" ) ;
45+ return ;
46+ }
47+
48+ // Subscribe to login events for automatic scene transition
49+ PassportUI . OnLoginSuccessStatic += OnPassportLoginSuccess ;
50+ PassportUI . OnLoginFailureStatic += OnPassportLoginFailure ;
51+
52+ // Check if Passport is already initialized (e.g., from logout flow)
53+ if ( Passport . Instance != null )
6154 {
62- passportUI . Init ( passport ) ;
63- Debug . Log ( "PassportUI found and initialized successfully" ) ;
55+ Debug . Log ( "Passport already initialized, setting up UI only..." ) ;
56+
57+ // Just initialize the UI with the existing Passport instance
58+ await passportUI . InitializeWithPassport ( Passport . Instance ) ;
59+
60+ // Store reference for other scripts that need it
61+ SampleAppManager . PassportInstance = Passport . Instance ;
6462
65- // Subscribe to login success event for automatic scene transition
66- PassportUI . OnLoginSuccessStatic += OnPassportLoginSuccess ;
67- PassportUI . OnLoginFailureStatic += OnPassportLoginFailure ;
63+ Debug . Log ( "PassportUI initialized with existing Passport instance" ) ;
6864 }
6965 else
7066 {
71- Debug . LogWarning ( "PassportUI component not found in scene - UI login will not be available" ) ;
67+ Debug . Log ( "Initializing Passport using PassportUI prefab configuration..." ) ;
68+
69+ // PassportUI handles both Passport.Init() and UI setup
70+ // Configuration is done in the PassportUI Inspector (clientId, environment, etc.)
71+ await passportUI . InitializeWithPassport ( ) ;
72+
73+ // Store reference for other scripts that need it
74+ SampleAppManager . PassportInstance = Passport . Instance ;
75+
76+ Debug . Log ( "Passport and PassportUI initialized successfully" ) ;
7277 }
7378 }
7479 catch ( Exception ex )
@@ -79,15 +84,15 @@ private async void InitialisePassport()
7984
8085 private void OnPassportLoginSuccess ( )
8186 {
82- Debug . Log ( "🎉 Passport login successful! Navigating to authenticated scene..." ) ;
87+ Debug . Log ( "Passport login successful! Navigating to authenticated scene..." ) ;
8388
8489 // Navigate to authenticated scene
8590 SceneManager . LoadScene ( "AuthenticatedScene" ) ;
8691 }
8792
8893 private void OnPassportLoginFailure ( string errorMessage )
8994 {
90- Debug . LogError ( $ "❌ Passport login failed: { errorMessage } ") ;
95+ Debug . LogError ( $ "Passport login failed: { errorMessage } ") ;
9196
9297 // Could show error UI, retry options, etc.
9398 // For now, just log the error
0 commit comments