Skip to content

Commit 65a14f9

Browse files
committed
refactor: use webview and embedded login prompt for authentication
1 parent 2b3a32d commit 65a14f9

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

sample/Assets/Scenes/Passport/InitialisationWithUI.unity

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,16 @@ MonoBehaviour:
337337
m_Name:
338338
m_EditorClassIdentifier:
339339
loginButton: {fileID: 1274086209}
340+
OnLoginSuccess:
341+
m_PersistentCalls:
342+
m_Calls: []
343+
OnLoginFailure:
344+
m_PersistentCalls:
345+
m_Calls: []
340346
enableRemoteDebugging: 1
341347
remoteDebuggingPort: 9222
342348
clearCacheOnLogin: 1
349+
webViewUrl: https://auth.immutable.com/im-embedded-login-prompt?client_id=IllW5pJ54DShXtaSXzaAlghm40uQjptd
343350
--- !u!1 &96192975
344351
GameObject:
345352
m_ObjectHideFlags: 0

sample/Assets/Scripts/Passport/AuthenticatedSceneManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public static void NavigateToAuthenticatedScene()
6060

6161
public static void NavigateToUnauthenticatedScene()
6262
{
63-
UnityEngine.SceneManagement.SceneManager.LoadScene("UnauthenticatedScene");
63+
// Navigate to the main initialization scene with PassportUI instead
64+
UnityEngine.SceneManagement.SceneManager.LoadScene("InitialisationWithUI");
6465
}
6566
}

sample/Assets/Scripts/Passport/Logout/LogoutScript.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ private async UniTaskVoid LogoutAsync()
2525
await Passport.Instance.Logout();
2626
SampleAppManager.IsConnectedToImx = false;
2727
SampleAppManager.IsConnectedToZkEvm = false;
28-
AuthenticatedSceneManager.NavigateToUnauthenticatedScene();
28+
// Navigate back to the main initialization scene with PassportUI
29+
SceneManager.LoadScene("InitialisationWithUI");
2930
}
3031
catch (System.Exception ex)
3132
{

sample/Assets/Scripts/Passport/PassportInitialisation/PassportInitialisationScript.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ private async void InitialisePassport()
5454
var passport = await Passport.Init(clientId, environment, redirectUri, logoutRedirectUri);
5555
SampleAppManager.PassportInstance = passport;
5656

57-
// Navigate to the unauthenticated scene after initialising Passport
58-
SceneManager.LoadScene("UnauthenticatedScene");
57+
// Navigate to the main initialization scene with PassportUI after initialising Passport
58+
SceneManager.LoadScene("InitialisationWithUI");
5959
}
6060
catch (Exception ex)
6161
{

sample/Assets/Scripts/Passport/PassportInitialisation/PassportInitialisationWithUIScript.cs

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
using UnityEngine.SceneManagement;
55
using Immutable.Passport;
66
using 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>
816
public 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

Comments
 (0)