Skip to content

Commit c4536ec

Browse files
chore: WIP updated auth method select functionality
1 parent 8407522 commit c4536ec

File tree

3 files changed

+114
-73
lines changed

3 files changed

+114
-73
lines changed

sample/Assets/Scenes/Passport/SelectAuthMethod.unity

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -132,31 +132,15 @@ GameObject:
132132
serializedVersion: 6
133133
m_Component:
134134
- component: {fileID: 2269946}
135-
- component: {fileID: 2269945}
136135
- component: {fileID: 2269947}
136+
- component: {fileID: 2269948}
137137
m_Layer: 0
138138
m_Name: PassportInit
139139
m_TagString: Untagged
140140
m_Icon: {fileID: 0}
141141
m_NavMeshLayer: 0
142142
m_StaticEditorFlags: 0
143143
m_IsActive: 1
144-
--- !u!114 &2269945
145-
MonoBehaviour:
146-
m_ObjectHideFlags: 0
147-
m_CorrespondingSourceObject: {fileID: 0}
148-
m_PrefabInstance: {fileID: 0}
149-
m_PrefabAsset: {fileID: 0}
150-
m_GameObject: {fileID: 2269944}
151-
m_Enabled: 1
152-
m_EditorHideFlags: 0
153-
m_Script: {fileID: 11500000, guid: 91b840080cd8945349b79d86957b9d0b, type: 3}
154-
m_Name:
155-
m_EditorClassIdentifier:
156-
clientId: mp6rxfMDwwZDogcdgNrAaHnG0qMlXuMK
157-
environment: sandbox
158-
errorOutput: {fileID: 1690937182}
159-
featureButtons: []
160144
--- !u!4 &2269946
161145
Transform:
162146
m_ObjectHideFlags: 0
@@ -185,6 +169,22 @@ MonoBehaviour:
185169
m_Name:
186170
m_EditorClassIdentifier:
187171
altID: ef311053-db3c-46b5-95ce-91de9d804768
172+
--- !u!114 &2269948
173+
MonoBehaviour:
174+
m_ObjectHideFlags: 0
175+
m_CorrespondingSourceObject: {fileID: 0}
176+
m_PrefabInstance: {fileID: 0}
177+
m_PrefabAsset: {fileID: 0}
178+
m_GameObject: {fileID: 2269944}
179+
m_Enabled: 1
180+
m_EditorHideFlags: 0
181+
m_Script: {fileID: 11500000, guid: 91b840080cd8945349b79d86957b9d0b, type: 3}
182+
m_Name:
183+
m_EditorClassIdentifier:
184+
TopPadding: {fileID: 0}
185+
Output: {fileID: 1690937182}
186+
UseDeviceCodeAuthButton: {fileID: 717484784}
187+
UsePKCEButton: {fileID: 1744191472}
188188
--- !u!1 &27701369
189189
GameObject:
190190
m_ObjectHideFlags: 0
@@ -600,7 +600,7 @@ PrefabInstance:
600600
m_Modifications:
601601
- target: {fileID: 1289509568734035852, guid: b513e9e94967f45dbbf2c4e1f36dd77e, type: 3}
602602
propertyPath: m_RootOrder
603-
value: 2
603+
value: 1
604604
objectReference: {fileID: 0}
605605
- target: {fileID: 1289509568734035852, guid: b513e9e94967f45dbbf2c4e1f36dd77e, type: 3}
606606
propertyPath: m_LocalPosition.x
@@ -1067,10 +1067,9 @@ MonoBehaviour:
10671067
m_OnClick:
10681068
m_PersistentCalls:
10691069
m_Calls:
1070-
- m_Target: {fileID: 230457912}
1071-
m_TargetAssemblyTypeName: Immutable.Passport.Sample.PassportFeatures.DeviceCodeLoginScript,
1072-
Assembly-CSharp
1073-
m_MethodName: StartDeviceCodeLogin
1070+
- m_Target: {fileID: 2269948}
1071+
m_TargetAssemblyTypeName: PassportInitializationScript, Assembly-CSharp
1072+
m_MethodName: UseDeviceCodeAuth
10741073
m_Mode: 1
10751074
m_Arguments:
10761075
m_ObjectArgument: {fileID: 0}
@@ -2533,10 +2532,9 @@ MonoBehaviour:
25332532
m_OnClick:
25342533
m_PersistentCalls:
25352534
m_Calls:
2536-
- m_Target: {fileID: 1653741709}
2537-
m_TargetAssemblyTypeName: Immutable.Passport.Sample.PassportFeatures.PkceAuthScript,
2538-
Assembly-CSharp
2539-
m_MethodName: StartPkceAuth
2535+
- m_Target: {fileID: 2269948}
2536+
m_TargetAssemblyTypeName: PassportInitializationScript, Assembly-CSharp
2537+
m_MethodName: UsePKCE
25402538
m_Mode: 1
25412539
m_Arguments:
25422540
m_ObjectArgument: {fileID: 0}
@@ -2654,7 +2652,7 @@ RectTransform:
26542652
m_Children:
26552653
- {fileID: 1238552242}
26562654
m_Father: {fileID: 0}
2657-
m_RootOrder: 1
2655+
m_RootOrder: 2
26582656
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
26592657
m_AnchorMin: {x: 0, y: 0}
26602658
m_AnchorMax: {x: 0, y: 0}

sample/Assets/Scripts/Passport/DeviceCodeLogin/DeviceCodeLoginScript.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ private async UniTaskVoid StartDeviceCodeLoginAsync()
3131
var timeoutMs = GetDeviceCodeTimeoutMs();
3232
string formattedTimeout = timeoutMs != null ? $"{timeoutMs} ms" : "none";
3333
ShowOutput($"Logging in (timeout: {formattedTimeout})...");
34-
AuthenticatedSceneManager.NavigateToUnauthenticatedScene();
3534
try
3635
{
3736
await Passport.Instance.Login(timeoutMs: timeoutMs);
Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,101 @@
1+
using System;
12
using UnityEngine;
23
using UnityEngine.UI;
3-
using Cysharp.Threading.Tasks;
4+
using UnityEngine.SceneManagement;
45
using Immutable.Passport;
6+
using Immutable.Passport.Core.Logging;
57

6-
namespace Immutable.Passport.Sample.PassportFeatures
8+
public class PassportInitializationScript : MonoBehaviour
79
{
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()
938
{
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
2368
{
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");
3282
}
83+
catch (Exception ex)
84+
{
85+
Debug.LogException(ex, this);
86+
ShowOutput($"Initialise Passport error: {ex.Message}");
87+
}
88+
}
3389

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)
3597
{
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;
5599
}
56100
}
57-
}
101+
}

0 commit comments

Comments
 (0)