You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"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",
Copy file name to clipboardExpand all lines: sample/Assets/Scripts/Passport/_tutorials~/Authentication/tutorial.md
+88-71Lines changed: 88 additions & 71 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
</div>
6
6
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.
8
8
9
9
<divclass="button-component">
10
10
@@ -14,42 +14,32 @@ The Authentication feature group provides essential tools for integrating user a
14
14
15
15
## Authentication Overview
16
16
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:
18
18
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
23
23
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.
25
25
26
26
## Unity SDK Authentication Features
27
27
28
28
### Feature: Login
29
29
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.
ShowOutput($"Logging in (timeout: {formattedTimeout})...");
38
35
try
39
36
{
40
-
if (SampleAppManager.UsePKCE)
41
-
{
42
-
awaitPassport.LoginPKCE();
43
-
}
44
-
else
45
-
{
46
-
awaitPassport.Login(timeoutMs: timeoutMs);
47
-
}
48
-
NavigateToAuthenticatedScene();
37
+
awaitPassport.Login();
38
+
SceneManager.LoadScene("AuthenticatedScene");
49
39
}
50
-
catch (OperationCanceledException)
40
+
catch (OperationCanceledExceptionex)
51
41
{
52
-
ShowOutput("Failed to login: cancelled");
42
+
ShowOutput($"Failed to login: cancelled {ex.Message}\\n{ex.StackTrace}");
53
43
}
54
44
catch (Exceptionex)
55
45
{
@@ -58,13 +48,18 @@ public async void Login()
58
48
}
59
49
```
60
50
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.
62
52
63
53
### Feature: Logout
64
54
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.
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.
97
85
98
86
### Feature: Relogin
99
87
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.
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.
131
124
132
125
### Feature: Reconnect
133
126
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.
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.
175
167
176
-
## Running the Authentication Examples
168
+
## Running the Feature Group Examples
177
169
178
170
### Prerequisites
179
171
180
-
Before running the authentication examples, you need to:
172
+
Before running the authentication examples, ensure you have:
181
173
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
186
178
187
179
### Step-by-Step Instructions
188
180
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
198
194
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
200
199
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
206
211
207
212
## Summary
208
213
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
210
220
211
221
### Best Practices
212
222
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
219
232
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
0 commit comments