Skip to content

Commit 7a4ae0e

Browse files
chore: re-run tutorial workflow
1 parent ae72762 commit 7a4ae0e

File tree

14 files changed

+580
-779
lines changed

14 files changed

+580
-779
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"title": "Immutable Passport Authentication",
3-
"description": "Implement user authentication in Unity games with Immutable Passport SDK, supporting both Device Code and PKCE flows",
4-
"keywords": ["Immutable", "SDK", "Authentication", "Login", "Logout", "Relogin", "Reconnect", "Passport", "Unity"],
5-
"tech_stack": ["Unity", "C#", "UniTask", "Immutable Passport SDK"],
2+
"title": "Authentication with Immutable Passport SDK",
3+
"description": "Complete authentication system demonstrating Login, Logout, Relogin, and Reconnect features using PKCE flow for secure user authentication and session management in Unity games",
4+
"keywords": ["Immutable", "SDK", "Authentication", "Login", "Logout", "Relogin", "Reconnect", "PKCE", "OAuth", "Passport", "Session Management"],
5+
"tech_stack": ["Unity", "C#", "UniTask", "PKCE", "OAuth 2.0"],
66
"product": "Passport",
77
"programming_language": "C#"
88
}

sample/Assets/Scripts/Passport/_tutorials~/Authentication/tutorial.md

Lines changed: 88 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
</div>
66

7-
The Authentication feature group provides essential tools for integrating user authentication into your Unity application using the Immutable Passport SDK. These features allow players to log in, log out, and maintain their authentication state across sessions, which is fundamental for any blockchain application.
7+
The Authentication feature group demonstrates the core authentication capabilities of the Immutable Passport SDK. These features enable secure user authentication and session management in Unity games, providing seamless login experiences across different platforms and maintaining user sessions between game sessions.
88

99
<div class="button-component">
1010

@@ -14,42 +14,32 @@ The Authentication feature group provides essential tools for integrating user a
1414

1515
## Authentication Overview
1616

17-
The Authentication feature group contains four key features:
17+
The Authentication feature group includes four essential features that work together to provide a complete authentication system:
1818

19-
- **Login**: Authenticate users using Device Code Auth or PKCE flow
20-
- **Logout**: End a user's authenticated session
21-
- **Relogin**: Restore a previously authenticated session using cached credentials
22-
- **Reconnect**: Restore authentication and blockchain connections in a single operation
19+
- **Login**: Primary authentication using PKCE (Proof Key for Code Exchange) flow
20+
- **Logout**: Secure session termination and credential cleanup
21+
- **Relogin**: Silent re-authentication using cached credentials
22+
- **Reconnect**: Re-authentication with automatic IMX provider setup
2323

24-
These features work together to create a complete authentication flow for your application. The Login and Logout features handle the primary authentication process, while Relogin and Reconnect provide convenience methods for maintaining session state across application restarts or network interruptions.
24+
These features work together to create a seamless authentication experience. Login establishes the initial session, Relogin provides quick re-authentication without user interaction, Reconnect combines re-authentication with IMX connectivity, and Logout ensures secure session cleanup.
2525

2626
## Unity SDK Authentication Features
2727

2828
### Feature: Login
2929

30-
The Login feature allows users to authenticate with Immutable Passport using either Device Code Auth or PKCE (Proof Key for Code Exchange) authentication flows.
30+
The Login feature implements the primary authentication flow using PKCE (Proof Key for Code Exchange), which opens the user's default browser on desktop or an in-app browser on mobile platforms for secure authentication.
3131

32-
```csharp title="Login" manualLink="https://github.com/immutable/unity-immutable-sdk/blob/main/sample/Assets/Scripts/Passport/Login/LoginScript.cs"
32+
```csharp title="Login Implementation" manualLink="https://github.com/immutable/unity-immutable-sdk/blob/main/sample/Assets/Scripts/Passport/Login/LoginScript.cs"
3333
public async void Login()
3434
{
35-
var timeoutMs = GetDeviceCodeTimeoutMs();
36-
string formattedTimeout = timeoutMs != null ? $"{timeoutMs} ms" : "none";
37-
ShowOutput($"Logging in (timeout: {formattedTimeout})...");
3835
try
3936
{
40-
if (SampleAppManager.UsePKCE)
41-
{
42-
await Passport.LoginPKCE();
43-
}
44-
else
45-
{
46-
await Passport.Login(timeoutMs: timeoutMs);
47-
}
48-
NavigateToAuthenticatedScene();
37+
await Passport.Login();
38+
SceneManager.LoadScene("AuthenticatedScene");
4939
}
50-
catch (OperationCanceledException)
40+
catch (OperationCanceledException ex)
5141
{
52-
ShowOutput("Failed to login: cancelled");
42+
ShowOutput($"Failed to login: cancelled {ex.Message}\\n{ex.StackTrace}");
5343
}
5444
catch (Exception ex)
5545
{
@@ -58,13 +48,18 @@ public async void Login()
5848
}
5949
```
6050

61-
This implementation checks which authentication method to use based on the `SampleAppManager.UsePKCE` flag. For Device Code Auth (the default on non-WebGL platforms), it calls `Passport.Login()` with an optional timeout parameter. For PKCE auth (required for WebGL), it calls `Passport.LoginPKCE()`. Upon successful login, it navigates to the authenticated scene.
51+
The Login method uses the Passport SDK's PKCE authentication flow, which provides enhanced security by generating a code verifier and challenge. When successful, the user is automatically navigated to the authenticated scene where they can access protected features.
6252

6353
### Feature: Logout
6454

65-
The Logout feature ends the user's authenticated session with Immutable Passport.
55+
The Logout feature securely terminates the user's session and cleans up stored credentials, ensuring proper session management and security.
56+
57+
```csharp title="Logout Implementation" manualLink="https://github.com/immutable/unity-immutable-sdk/blob/main/sample/Assets/Scripts/Passport/Logout/LogoutScript.cs"
58+
public void Logout()
59+
{
60+
LogoutAsync();
61+
}
6662

67-
```csharp title="Logout" manualLink="https://github.com/immutable/unity-immutable-sdk/blob/main/sample/Assets/Scripts/Passport/Logout/LogoutScript.cs"
6863
private async UniTaskVoid LogoutAsync()
6964
{
7065
if (Passport.Instance == null)
@@ -74,14 +69,7 @@ private async UniTaskVoid LogoutAsync()
7469
}
7570
try
7671
{
77-
if (SampleAppManager.UsePKCE)
78-
{
79-
await Passport.Instance.LogoutPKCE();
80-
}
81-
else
82-
{
83-
await Passport.Instance.Logout();
84-
}
72+
await Passport.Instance.Logout();
8573
SampleAppManager.IsConnectedToImx = false;
8674
SampleAppManager.IsConnectedToZkEvm = false;
8775
AuthenticatedSceneManager.NavigateToUnauthenticatedScene();
@@ -93,13 +81,18 @@ private async UniTaskVoid LogoutAsync()
9381
}
9482
```
9583

96-
Similar to the Login feature, Logout checks the authentication method and calls the appropriate logout function (`LogoutPKCE()` or `Logout()`). It also resets the connection states for IMX and zkEVM before navigating back to the unauthenticated scene.
84+
The Logout implementation not only calls the Passport logout method but also resets the application's connection states for both IMX and zkEVM, ensuring a clean slate for the next authentication session.
9785

9886
### Feature: Relogin
9987

100-
The Relogin feature allows users to authenticate again using cached credentials, providing a smoother user experience for returning users.
88+
The Relogin feature enables silent re-authentication using previously stored credentials, providing a smooth user experience by avoiding repeated login prompts when credentials are still valid.
89+
90+
```csharp title="Relogin Implementation" manualLink="https://github.com/immutable/unity-immutable-sdk/blob/main/sample/Assets/Scripts/Passport/Relogin/ReloginScript.cs"
91+
public void Relogin()
92+
{
93+
ReloginAsync();
94+
}
10195

102-
```csharp title="Relogin" manualLink="https://github.com/immutable/unity-immutable-sdk/blob/main/sample/Assets/Scripts/Passport/Relogin/ReloginScript.cs"
10396
private async UniTaskVoid ReloginAsync()
10497
{
10598
if (Passport.Instance == null)
@@ -127,13 +120,13 @@ private async UniTaskVoid ReloginAsync()
127120
}
128121
```
129122

130-
The Relogin feature calls `Passport.Instance.Login()` with the `useCachedSession` parameter set to `true`, which attempts to restore the user's previous session without requiring them to go through the full authentication flow again. If successful, it navigates to the authenticated scene.
123+
The Relogin feature uses the `useCachedSession: true` parameter to attempt authentication with stored credentials. This provides a seamless experience for returning users while gracefully handling cases where credentials may have expired.
131124

132125
### Feature: Reconnect
133126

134-
The Reconnect feature combines re-authentication with reconnecting to blockchain services (IMX) in a single operation.
127+
The Reconnect feature combines re-authentication with automatic IMX provider setup, streamlining the process of restoring both authentication state and blockchain connectivity.
135128

136-
```csharp title="Reconnect" manualLink="https://github.com/immutable/unity-immutable-sdk/blob/main/sample/Assets/Scripts/Passport/Reconnect/ReconnectScript.cs"
129+
```csharp title="Reconnect Implementation" manualLink="https://github.com/immutable/unity-immutable-sdk/blob/main/sample/Assets/Scripts/Passport/Reconnect/ReconnectScript.cs"
137130
private async UniTaskVoid ReconnectAsync()
138131
{
139132
if (Passport.Instance == null)
@@ -147,7 +140,6 @@ private async UniTaskVoid ReconnectAsync()
147140
bool connected = await Passport.Instance.ConnectImx(useCachedSession: true);
148141
if (connected)
149142
{
150-
// Set IMX and zkEVM state and update UI as if user clicked Connect to IMX/EVM
151143
SampleAppManager.IsConnectedToImx = true;
152144
SampleAppManager.IsConnectedToZkEvm = true;
153145
SampleAppManager.PassportInstance = Passport.Instance;
@@ -171,50 +163,75 @@ private async UniTaskVoid ReconnectAsync()
171163
}
172164
```
173165

174-
The Reconnect feature calls `Passport.Instance.ConnectImx()` with the `useCachedSession` parameter set to `true`, which not only tries to reestablish the authentication session but also reconnects to the IMX blockchain. If successful, it updates the connection states for both IMX and zkEVM, updates the UI, and navigates to the authenticated scene.
166+
The Reconnect feature uses `ConnectImx(useCachedSession: true)` to both authenticate the user and establish the IMX connection in a single operation. It also updates the UI state to reflect the successful connection to both IMX and zkEVM networks.
175167

176-
## Running the Authentication Examples
168+
## Running the Feature Group Examples
177169

178170
### Prerequisites
179171

180-
Before running the authentication examples, you need to:
172+
Before running the authentication examples, ensure you have:
181173

182-
1. Set up an Immutable Hub account at [Immutable Hub](https://hub.immutable.com/)
183-
2. Clone the Unity Immutable SDK repository
184-
3. Open the sample app in Unity Editor (2022.3 LTS or newer recommended)
185-
4. Ensure you have the required packages installed (UniTask, TextMeshPro)
174+
- Unity 2021.3 or later installed
175+
- The Immutable Unity SDK properly configured in your project
176+
- Access to [Immutable Hub](https://hub.immutable.com/) for environment setup and configuration
177+
- A valid Passport client ID configured in your project
186178

187179
### Step-by-Step Instructions
188180

189-
1. Open the sample app scene located at `sample/Assets/Scenes/Passport/InitialisationScene.unity`
190-
2. Enter Play mode in the Unity Editor
191-
3. In the Initialisation Scene:
192-
- For non-WebGL builds, choose between "Use Device Code Auth" or "Use PKCE"
193-
- For WebGL builds, PKCE is automatically selected
194-
4. After initialization, you'll be taken to the Unauthenticated Scene where you can:
195-
- Use "Login" to authenticate with a new session
196-
- Use "Relogin" to try authenticating with cached credentials
197-
- Use "Reconnect" to authenticate and reconnect to blockchain services
181+
1. **Open the Sample Project**
182+
- Navigate to the `sample` directory in the Unity Immutable SDK
183+
- Open the project in Unity Editor
184+
185+
2. **Configure Passport Settings**
186+
- Ensure your Passport client ID is properly set in the `PassportInitialisationScript.cs`
187+
- Verify the redirect URIs match your application configuration
188+
189+
3. **Run the Authentication Flow**
190+
- Start with the PassportInitialisation scene to initialize the SDK
191+
- The application will automatically navigate to the UnauthenticatedScene
192+
- Test the Login feature by clicking the "Login" button
193+
- After successful authentication, you'll be redirected to the AuthenticatedScene
198194

199-
### Authentication Flow Sequence
195+
4. **Test Session Management**
196+
- Use the Logout feature to terminate your session
197+
- Return to the UnauthenticatedScene and test the Relogin feature
198+
- Test the Reconnect feature to verify IMX connectivity restoration
200199

201-
For optimal testing:
202-
1. Start with "Login" to create a new authenticated session
203-
2. Use the "Logout" button on the Authenticated Scene to end your session
204-
3. Try "Relogin" to test session restoration
205-
4. If you previously connected to IMX, try "Reconnect" to test combined authentication and blockchain reconnection
200+
5. **Verify State Management**
201+
- Check that connection states (IMX/zkEVM) are properly updated
202+
- Ensure UI elements reflect the current authentication and connection status
203+
204+
### Sequence Dependencies
205+
206+
The authentication features should be tested in this recommended sequence:
207+
1. **Login** - Establish initial authentication
208+
2. **Logout** - Test session termination
209+
3. **Relogin** - Test cached credential authentication
210+
4. **Reconnect** - Test authentication with IMX connectivity
206211

207212
## Summary
208213

209-
The Authentication feature group provides a comprehensive set of tools for handling user authentication in your Unity application with Immutable Passport. It supports both Device Code Auth and PKCE authentication methods, allowing for cross-platform compatibility including WebGL builds.
214+
The Authentication feature group provides a comprehensive authentication system for Unity games using the Immutable Passport SDK. The four features work together to cover all aspects of user session management:
215+
216+
- **Login** handles initial user authentication using secure PKCE flow
217+
- **Logout** ensures proper session cleanup and security
218+
- **Relogin** provides seamless re-authentication for returning users
219+
- **Reconnect** combines authentication with blockchain connectivity setup
210220

211221
### Best Practices
212222

213-
- Initialize Passport before attempting any authentication operations
214-
- Handle authentication exceptions appropriately in your implementation
215-
- For WebGL applications, always use PKCE authentication
216-
- For returning users, try the Relogin or Reconnect features before falling back to a full Login
217-
- Always check if the Passport instance exists before attempting operations
218-
- Clear connection states when logging out to maintain proper application state
223+
When implementing these authentication features:
224+
225+
- Always check for null Passport instances before making authentication calls
226+
- Implement proper error handling for network issues and authentication failures
227+
- Update application state consistently after authentication state changes
228+
- Use the cached session options appropriately to improve user experience
229+
- Ensure UI state reflects the current authentication and connection status
230+
231+
### Key Takeaways
219232

220-
These authentication features provide the foundation for all other Immutable operations in your Unity application, as users must be authenticated before interacting with blockchain services like IMX and zkEVM.
233+
- The PKCE authentication flow provides enhanced security for OAuth 2.0 authentication
234+
- Cached sessions enable seamless re-authentication without user interaction
235+
- Proper state management is crucial for maintaining consistent application behavior
236+
- The Reconnect feature streamlines the process of restoring both authentication and blockchain connectivity
237+
- All authentication operations are asynchronous and require proper exception handling

sample/Assets/Scripts/Passport/_tutorials~/ClearStorageAndCache/metadata.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"title": "Clear Storage and Cache on Mobile Devices",
3-
"description": "Learn how to clear WebView storage and cache on Android and iOS devices using the Immutable Passport SDK",
4-
"keywords": ["Immutable", "SDK", "ClearStorageAndCache", "Mobile", "Android", "iOS", "WebView"],
2+
"title": "Clear Storage and Cache",
3+
"description": "Learn how to clear WebView cache and storage data in Immutable Passport applications for Android and iOS devices",
4+
"keywords": ["Immutable", "SDK", "Clear Storage", "Clear Cache", "WebView", "Mobile", "Android", "iOS", "Authentication", "Maintenance"],
55
"tech_stack": ["Unity", "C#", "WebView"],
66
"product": "Passport",
77
"programming_language": "C#"

0 commit comments

Comments
 (0)