1
+ using System ;
1
2
using UnityEngine ;
2
3
using UnityEngine . UI ;
3
- using Cysharp . Threading . Tasks ;
4
+ using UnityEngine . SceneManagement ;
4
5
using Immutable . Passport ;
6
+ using Immutable . Passport . Core . Logging ;
5
7
6
- namespace Immutable . Passport . Sample . PassportFeatures
8
+ public class PassportInitializationScript : MonoBehaviour
7
9
{
8
- public class PassportInitializationScript : MonoBehaviour
10
+ #pragma warning disable CS8618
11
+ [ SerializeField ] private GameObject TopPadding ;
12
+ [ SerializeField ] private Text Output ;
13
+ [ SerializeField ] private Button UseDeviceCodeAuthButton ;
14
+ [ SerializeField ] private Button UsePKCEButton ;
15
+ #pragma warning restore CS8618
16
+
17
+ void Start ( )
18
+ {
19
+ // WebGL does not support Device Code Auth, so we'll use PKCE by default instead.
20
+ #if UNITY_WEBGL
21
+ UsePKCE ( ) ;
22
+ #endif
23
+ }
24
+
25
+ /// <summary>
26
+ /// Initialises Passport to use Device Code Auth
27
+ /// </summary>
28
+ public void UseDeviceCodeAuth ( )
29
+ {
30
+ SampleAppManager . UsePKCE = false ;
31
+ InitialisePassport ( logoutRedirectUri : "https://www.immutable.com" ) ;
32
+ }
33
+
34
+ /// <summary>
35
+ /// Initialises Passport to use PKCE with the specified redirect URIs.
36
+ /// </summary>
37
+ public void UsePKCE ( )
9
38
{
10
- [ Header ( "Passport Config" ) ]
11
- [ Tooltip ( "Client ID for Passport SDK" ) ]
12
- public string clientId ;
13
- [ Tooltip ( "Environment (e.g., 'production', 'sandbox')" ) ]
14
- public string environment ;
15
- [ Tooltip ( "Optional: UI Text to display errors" ) ]
16
- public Text errorOutput ;
17
-
18
- [ Header ( "Feature Buttons" ) ]
19
- [ Tooltip ( "All feature buttons to enable after Passport is initialized" ) ]
20
- public Button [ ] featureButtons ;
21
-
22
- private void Awake ( )
39
+ SampleAppManager . UsePKCE = true ;
40
+ #if UNITY_WEBGL
41
+ string url = Application . absoluteURL ;
42
+ Uri uri = new Uri ( url ) ;
43
+ string scheme = uri . Scheme ;
44
+ string hostWithPort = uri . IsDefaultPort ? uri . Host : $ "{ uri . Host } :{ uri . Port } ";
45
+ string fullPath = uri . AbsolutePath . EndsWith ( "/" ) ? uri . AbsolutePath : uri . AbsolutePath . Substring ( 0 , uri . AbsolutePath . LastIndexOf ( '/' ) + 1 ) ;
46
+
47
+ string redirectUri = $ "{ scheme } ://{ hostWithPort } { fullPath } callback.html";
48
+ string logoutRedirectUri = $ "{ scheme } ://{ hostWithPort } { fullPath } logout.html";
49
+
50
+ InitialisePassport ( redirectUri : redirectUri , logoutRedirectUri : logoutRedirectUri ) ;
51
+ #else
52
+ InitialisePassport ( redirectUri : "immutablerunner://callback" , logoutRedirectUri : "immutablerunner://logout" ) ;
53
+ #endif
54
+ }
55
+
56
+ /// <summary>
57
+ /// Initialises Passport.
58
+ /// </summary>
59
+ /// <param name="redirectUri">(Android, iOS and macOS only) The URL to which auth will redirect the browser after
60
+ /// authorisation has been granted by the user</param>
61
+ /// <param name="logoutRedirectUri">The URL to which auth will redirect the browser
62
+ /// after log out is complete</param>
63
+ private async void InitialisePassport ( string ? redirectUri = null , string ? logoutRedirectUri = null )
64
+ {
65
+ ShowOutput ( "Initialising Passport..." ) ;
66
+
67
+ try
23
68
{
24
- // Disable all feature buttons until Passport is initialized
25
- if ( featureButtons != null )
26
- {
27
- foreach ( var btn in featureButtons )
28
- if ( btn != null ) btn . interactable = false ;
29
- }
30
- // Use UniTask.Void to call async method from Awake
31
- InitializePassportAsync ( ) . Forget ( ) ;
69
+ // Set the log level for the SDK
70
+ Passport . LogLevel = LogLevel . Info ;
71
+
72
+ // Don't redact token values from logs
73
+ Passport . RedactTokensInLogs = false ;
74
+
75
+ // Initialise Passport
76
+ string environment = Immutable . Passport . Model . Environment . SANDBOX ;
77
+ string clientId = "mp6rxfMDwwZDogcdgNrAaHnG0qMlXuMK" ;
78
+ Passport passport = await Passport . Init ( clientId , environment , redirectUri , logoutRedirectUri ) ;
79
+
80
+ // Navigate to the unauthenticated scene after initialising Passport
81
+ SceneManager . LoadScene ( "UnauthenticatedScene" ) ;
32
82
}
83
+ catch ( Exception ex )
84
+ {
85
+ Debug . LogException ( ex , this ) ;
86
+ ShowOutput ( $ "Initialise Passport error: { ex . Message } ") ;
87
+ }
88
+ }
33
89
34
- private async UniTaskVoid InitializePassportAsync ( )
90
+ /// <summary>
91
+ /// Prints the specified <code>message</code> to the output box.
92
+ /// </summary>
93
+ /// <param name="message">The message to print</param>
94
+ private void ShowOutput ( string message )
95
+ {
96
+ if ( Output != null )
35
97
{
36
- try
37
- {
38
- await Passport . Init ( clientId , environment ) ;
39
- Debug . Log ( "[PassportInitializationScript] Passport initialized successfully." ) ;
40
- // Enable all feature buttons
41
- if ( featureButtons != null )
42
- {
43
- foreach ( var btn in featureButtons )
44
- if ( btn != null ) btn . interactable = true ;
45
- }
46
- }
47
- catch ( System . Exception ex )
48
- {
49
- Debug . LogError ( $ "[PassportInitializationScript] Passport initialization failed: { ex . Message } ") ;
50
- if ( errorOutput != null )
51
- {
52
- errorOutput . text = $ "Passport initialization failed: { ex . Message } ";
53
- }
54
- }
98
+ Output . text = message ;
55
99
}
56
100
}
57
- }
101
+ }
0 commit comments