Skip to content

Commit 4de2af2

Browse files
chore: update direct login support to use direct login options (#3896)
1 parent 017b2d0 commit 4de2af2

File tree

7 files changed

+117
-38
lines changed

7 files changed

+117
-38
lines changed

sample/Assets/Scripts/Passport/Login/LoginScript.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,51 +27,52 @@ void Start()
2727
ShowOutput("Passport Instance is null");
2828
}
2929

30-
// Set up button listeners if buttons are assigned
31-
if (DefaultLoginButton != null) DefaultLoginButton.onClick.AddListener(() => Login(DirectLoginMethod.None));
32-
if (GoogleLoginButton != null) GoogleLoginButton.onClick.AddListener(() => Login(DirectLoginMethod.Google));
33-
if (AppleLoginButton != null) AppleLoginButton.onClick.AddListener(() => Login(DirectLoginMethod.Apple));
34-
if (FacebookLoginButton != null) FacebookLoginButton.onClick.AddListener(() => Login(DirectLoginMethod.Facebook));
30+
// Set up button listeners using DirectLoginOptions
31+
if (DefaultLoginButton != null) DefaultLoginButton.onClick.AddListener(() => Login(new DirectLoginOptions()));
32+
if (GoogleLoginButton != null) GoogleLoginButton.onClick.AddListener(() => Login(new DirectLoginOptions(DirectLoginMethod.Google)));
33+
if (AppleLoginButton != null) AppleLoginButton.onClick.AddListener(() => Login(new DirectLoginOptions(DirectLoginMethod.Apple)));
34+
if (FacebookLoginButton != null) FacebookLoginButton.onClick.AddListener(() => Login(new DirectLoginOptions(DirectLoginMethod.Facebook)));
3535
}
3636

3737
/// <summary>
3838
/// Logs into Passport using the default auth method.
3939
/// </summary>
4040
public async void Login()
4141
{
42-
await LoginAsync(DirectLoginMethod.None);
42+
await LoginAsync(new DirectLoginOptions());
4343
}
4444

4545
/// <summary>
46-
/// Logs into Passport using the specified direct login method.
46+
/// Logs into Passport using the specified direct login options.
4747
/// </summary>
48-
/// <param name="directLoginMethod">The direct login method to use (Google, Apple, Facebook, or None for default)</param>
49-
public async void Login(DirectLoginMethod directLoginMethod)
48+
/// <param name="directLoginOptions">The direct login options</param>
49+
public async void Login(DirectLoginOptions directLoginOptions)
5050
{
51-
await LoginAsync(directLoginMethod);
51+
await LoginAsync(directLoginOptions);
5252
}
5353

5454
/// <summary>
5555
/// Internal async method that performs the actual login logic.
5656
/// </summary>
57-
/// <param name="directLoginMethod">The direct login method to use</param>
58-
private async System.Threading.Tasks.Task LoginAsync(DirectLoginMethod directLoginMethod)
57+
/// <param name="directLoginOptions">The direct login options</param>
58+
private async System.Threading.Tasks.Task LoginAsync(DirectLoginOptions directLoginOptions)
5959
{
6060
try
6161
{
62-
string methodName = directLoginMethod == DirectLoginMethod.None ? "default" : directLoginMethod.ToString();
63-
ShowOutput($"Logging in with {methodName} method...");
62+
string directLoginMethod = directLoginOptions.directLoginMethod.ToString().ToLower();
6463

65-
bool success = await Passport.Login(useCachedSession: false, directLoginMethod: directLoginMethod);
64+
ShowOutput($"Logging in with {directLoginMethod} method...");
65+
66+
bool success = await Passport.Login(useCachedSession: false, directLoginOptions: directLoginOptions);
6667

6768
if (success)
6869
{
69-
ShowOutput($"Successfully logged in with {methodName}");
70+
ShowOutput($"Successfully logged in with {directLoginMethod}");
7071
SceneManager.LoadScene("AuthenticatedScene");
7172
}
7273
else
7374
{
74-
ShowOutput($"Failed to log in with {methodName}");
75+
ShowOutput($"Failed to log in with {directLoginMethod}");
7576
}
7677
}
7778
catch (OperationCanceledException ex)

src/Packages/Passport/Runtime/Scripts/Private/Model/DirectLoginMethod.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace Immutable.Passport.Model
44
{
55
/// <summary>
6-
/// Enum for direct login methods supported by Passport.
6+
/// Enum representing direct login methods for authentication providers.
77
/// </summary>
88
[Serializable]
99
public enum DirectLoginMethod
1010
{
11-
None,
11+
Email,
1212
Google,
1313
Apple,
1414
Facebook
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
3+
namespace Immutable.Passport.Model
4+
{
5+
/// <summary>
6+
/// Structure representing direct login options for authentication.
7+
/// Can be used for social login (google, apple, facebook) or email login.
8+
/// </summary>
9+
[Serializable]
10+
public class DirectLoginOptions
11+
{
12+
/// <summary>
13+
/// Authentication method.
14+
/// </summary>
15+
public DirectLoginMethod directLoginMethod = DirectLoginMethod.Email;
16+
17+
/// <summary>
18+
/// Email address for email-based authentication (only used when directLoginMethod is Email).
19+
/// </summary>
20+
public string email;
21+
22+
/// <summary>
23+
/// Default constructor.
24+
/// </summary>
25+
public DirectLoginOptions()
26+
{
27+
directLoginMethod = DirectLoginMethod.Email;
28+
email = null;
29+
}
30+
31+
/// <summary>
32+
/// Constructor with method and email.
33+
/// </summary>
34+
/// <param name="loginMethod">The direct login method</param>
35+
/// <param name="emailAddress">The email address (optional)</param>
36+
public DirectLoginOptions(DirectLoginMethod loginMethod, string emailAddress = null)
37+
{
38+
directLoginMethod = loginMethod;
39+
email = emailAddress;
40+
}
41+
42+
/// <summary>
43+
/// Checks if the email is valid and should be included in requests.
44+
/// </summary>
45+
/// <returns>True if email is valid for email method</returns>
46+
public bool IsEmailValid()
47+
{
48+
return directLoginMethod == DirectLoginMethod.Email && !string.IsNullOrEmpty(email);
49+
}
50+
}
51+
}

src/Packages/Passport/Runtime/Scripts/Private/Model/DirectLoginOptions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Packages/Passport/Runtime/Scripts/Private/Model/Request/GetPKCEAuthUrlRequest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ internal class GetPKCEAuthUrlRequest
1414
public bool isConnectImx;
1515

1616
/// <summary>
17-
/// The direct login method to use for authentication.
17+
/// The direct login options for authentication.
1818
/// </summary>
19-
public string directLoginMethod;
19+
public DirectLoginOptions directLoginOptions;
2020

2121
/// <summary>
22-
/// Creates a new GetPKCEAuthUrlRequest.
22+
/// Creates a new GetPKCEAuthUrlRequest with DirectLoginOptions.
2323
/// </summary>
2424
/// <param name="isConnectImx">Whether this is a connect to IMX operation</param>
25-
/// <param name="directLoginMethod">The direct login method to use</param>
26-
public GetPKCEAuthUrlRequest(bool isConnectImx, DirectLoginMethod directLoginMethod)
25+
/// <param name="directLoginOptions">The direct login options to use</param>
26+
public GetPKCEAuthUrlRequest(bool isConnectImx, DirectLoginOptions directLoginOptions)
2727
{
2828
this.isConnectImx = isConnectImx;
29-
this.directLoginMethod = directLoginMethod == DirectLoginMethod.None ? null : directLoginMethod.ToString().ToLower();
29+
this.directLoginOptions = directLoginOptions;
3030
}
3131
}
3232
}

src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class PassportImpl
2525
private readonly PassportAnalytics _analytics = new();
2626

2727
private bool _pkceLoginOnly; // Used to differentiate between a login and connect
28-
private DirectLoginMethod _directLoginMethod; // Store the direct login method for current operation
28+
private DirectLoginOptions _directLoginOptions; // Store the direct login options for current operation
2929
private UniTaskCompletionSource<bool>? _pkceCompletionSource;
3030
private string _redirectUri;
3131
private string _logoutRedirectUri;
@@ -98,7 +98,7 @@ public void SetCallTimeout(int ms)
9898
_communicationsManager.SetCallTimeout(ms);
9999
}
100100

101-
public UniTask<bool> Login(bool useCachedSession = false, DirectLoginMethod directLoginMethod = DirectLoginMethod.None)
101+
public UniTask<bool> Login(bool useCachedSession = false, DirectLoginOptions directLoginOptions = null)
102102
{
103103
if (useCachedSession)
104104
{
@@ -113,7 +113,7 @@ public UniTask<bool> Login(bool useCachedSession = false, DirectLoginMethod dire
113113
var task = new UniTaskCompletionSource<bool>();
114114
_pkceCompletionSource = task;
115115
_pkceLoginOnly = true;
116-
_directLoginMethod = directLoginMethod;
116+
_directLoginOptions = directLoginOptions;
117117
#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN)
118118
WindowsDeepLink.Initialise(_redirectUri, OnDeepLinkActivated);
119119
#endif
@@ -163,7 +163,7 @@ private async UniTask<bool> Relogin()
163163
return false;
164164
}
165165

166-
public async UniTask<bool> ConnectImx(bool useCachedSession = false, DirectLoginMethod directLoginMethod = DirectLoginMethod.None)
166+
public async UniTask<bool> ConnectImx(bool useCachedSession = false, DirectLoginOptions directLoginOptions = null)
167167
{
168168
if (useCachedSession)
169169
{
@@ -189,7 +189,7 @@ public async UniTask<bool> ConnectImx(bool useCachedSession = false, DirectLogin
189189
UniTaskCompletionSource<bool> task = new UniTaskCompletionSource<bool>();
190190
_pkceCompletionSource = task;
191191
_pkceLoginOnly = false;
192-
_directLoginMethod = directLoginMethod;
192+
_directLoginOptions = directLoginOptions;
193193

194194
#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN)
195195
WindowsDeepLink.Initialise(_redirectUri, OnDeepLinkActivated);
@@ -275,8 +275,24 @@ private async UniTask LaunchAuthUrl()
275275
{
276276
try
277277
{
278-
var request = new GetPKCEAuthUrlRequest(!_pkceLoginOnly, _directLoginMethod);
279-
var callResponse = await _communicationsManager.Call(PassportFunction.GET_PKCE_AUTH_URL, JsonUtility.ToJson(request));
278+
// Create the request JSON manually to ensure proper serialization
279+
var requestJson = $"{{\"isConnectImx\":{(!_pkceLoginOnly).ToString().ToLower()}";
280+
281+
if (_directLoginOptions != null)
282+
{
283+
requestJson += $",\"directLoginOptions\":{{\"directLoginMethod\":\"{_directLoginOptions.directLoginMethod.ToString().ToLower()}\"";
284+
285+
if (_directLoginOptions.IsEmailValid())
286+
{
287+
requestJson += $",\"email\":\"{_directLoginOptions.email}\"";
288+
}
289+
290+
requestJson += "}";
291+
}
292+
293+
requestJson += "}";
294+
295+
var callResponse = await _communicationsManager.Call(PassportFunction.GET_PKCE_AUTH_URL, requestJson);
280296
var response = callResponse.OptDeserializeObject<StringResponse>();
281297

282298
if (response != null && response.success == true && response.result != null)

src/Packages/Passport/Runtime/Scripts/Public/Passport.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,25 +292,25 @@ public void SetCallTimeout(int ms)
292292
/// Logs into Passport using Authorisation Code Flow with Proof Key for Code Exchange (PKCE).
293293
/// This opens the user's default browser on desktop or an in-app browser on mobile.
294294
/// <param name="useCachedSession">If true, Passport will attempt to re-authenticate the player using stored credentials. If re-authentication fails, it won't automatically prompt the user to log in again.</param>
295-
/// <param name="directLoginMethod">Optional direct login method to use (google, apple, facebook). If None, the user will see the standard login page.
295+
/// <param name="directLoginOptions">Direct login options for authentication (defaults to email method).
296296
/// </summary>
297297
/// <returns>
298298
/// Returns true if login is successful, otherwise false.
299299
/// </returns>
300-
public async UniTask<bool> Login(bool useCachedSession = false, DirectLoginMethod directLoginMethod = DirectLoginMethod.None)
300+
public async UniTask<bool> Login(bool useCachedSession = false, DirectLoginOptions directLoginOptions = null)
301301
{
302-
return await GetPassportImpl().Login(useCachedSession, directLoginMethod);
302+
return await GetPassportImpl().Login(useCachedSession, directLoginOptions);
303303
}
304304

305305
/// <summary>
306306
/// Logs the user into Passport using Authorisation Code Flow with Proof Key for Code Exchange (PKCE) and sets up the Immutable X provider.
307307
/// This opens the user's default browser on desktop or an in-app browser on mobile.
308308
/// <param name="useCachedSession">If true, Passport will attempt to re-authenticate the player using stored credentials. If re-authentication fails, it won't automatically prompt the user to log in again.</param>
309-
/// <param name="directLoginMethod">Optional direct login method to use (google, apple, facebook). If None, the user will see the standard login page.
309+
/// <param name="directLoginOptions">Direct login options for authentication (defaults to email method).
310310
/// </summary>
311-
public async UniTask<bool> ConnectImx(bool useCachedSession = false, DirectLoginMethod directLoginMethod = DirectLoginMethod.None)
311+
public async UniTask<bool> ConnectImx(bool useCachedSession = false, DirectLoginOptions directLoginOptions = null)
312312
{
313-
return await GetPassportImpl().ConnectImx(useCachedSession, directLoginMethod);
313+
return await GetPassportImpl().ConnectImx(useCachedSession, directLoginOptions);
314314
}
315315

316316
/// <summary>

0 commit comments

Comments
 (0)