Skip to content

Commit bb1bcf1

Browse files
committed
feat: add marketing consent direct login options
1 parent 65a14f9 commit bb1bcf1

File tree

6 files changed

+104
-18
lines changed

6 files changed

+104
-18
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Immutable.Passport.Model
88
[Serializable]
99
public enum DirectLoginMethod
1010
{
11+
None,
1112
Email,
1213
Google,
1314
Apple,

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,32 @@ public class DirectLoginOptions
1919
/// </summary>
2020
public string email;
2121

22+
/// <summary>
23+
/// Marketing consent status (optional).
24+
/// </summary>
25+
public MarketingConsentStatus? marketingConsentStatus;
26+
2227
/// <summary>
2328
/// Default constructor.
2429
/// </summary>
2530
public DirectLoginOptions()
2631
{
2732
directLoginMethod = DirectLoginMethod.Email;
2833
email = null;
34+
marketingConsentStatus = null;
2935
}
3036

3137
/// <summary>
3238
/// Constructor with method and email.
3339
/// </summary>
3440
/// <param name="loginMethod">The direct login method</param>
3541
/// <param name="emailAddress">The email address (optional)</param>
36-
public DirectLoginOptions(DirectLoginMethod loginMethod, string emailAddress = null)
42+
/// <param name="marketingConsentStatus">The marketing consent status (optional)</param>
43+
public DirectLoginOptions(DirectLoginMethod loginMethod, string emailAddress = null, MarketingConsentStatus? marketingConsentStatus = null)
3744
{
3845
directLoginMethod = loginMethod;
3946
email = emailAddress;
47+
this.marketingConsentStatus = marketingConsentStatus;
4048
}
4149

4250
/// <summary>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
3+
namespace Immutable.Passport.Model
4+
{
5+
/// <summary>
6+
/// Enum representing marketing consent status.
7+
/// </summary>
8+
[Serializable]
9+
public enum MarketingConsentStatus
10+
{
11+
OptedIn,
12+
Unsubscribed
13+
}
14+
15+
/// <summary>
16+
/// Extension methods for MarketingConsentStatus enum.
17+
/// </summary>
18+
public static class MarketingConsentStatusExtensions
19+
{
20+
/// <summary>
21+
/// Converts the enum value to the string format expected by the game bridge.
22+
/// </summary>
23+
/// <param name="status">The marketing consent status</param>
24+
/// <returns>The corresponding string value</returns>
25+
public static string ToApiString(this MarketingConsentStatus status)
26+
{
27+
return status switch
28+
{
29+
MarketingConsentStatus.OptedIn => "opted_in",
30+
MarketingConsentStatus.Unsubscribed => "unsubscribed",
31+
_ => throw new ArgumentOutOfRangeException(nameof(status), status, "Unknown MarketingConsentStatus value")
32+
};
33+
}
34+
}
35+
}

src/Packages/Passport/Runtime/Scripts/Private/Model/MarketingConsentStatus.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/PassportImpl.cs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -275,22 +275,19 @@ private async UniTask LaunchAuthUrl()
275275
{
276276
try
277277
{
278-
// Create the request JSON manually to ensure proper serialization
279-
var requestJson = $"{{\"isConnectImx\":{(!_pkceLoginOnly).ToString().ToLower()}";
280-
281-
if (_directLoginOptions != null)
278+
// Create the request using a serializable class for clean JSON generation
279+
var request = new AuthUrlRequest
282280
{
283-
requestJson += $",\"directLoginOptions\":{{\"directLoginMethod\":\"{_directLoginOptions.directLoginMethod.ToString().ToLower()}\"";
284-
285-
if (_directLoginOptions.IsEmailValid())
281+
isConnectImx = !_pkceLoginOnly,
282+
directLoginOptions = _directLoginOptions != null ? new DirectLoginRequestOptions
286283
{
287-
requestJson += $",\"email\":\"{_directLoginOptions.email}\"";
288-
}
284+
directLoginMethod = _directLoginOptions.directLoginMethod.ToString().ToLower(),
285+
email = _directLoginOptions.IsEmailValid() ? _directLoginOptions.email : null,
286+
marketingConsentStatus = _directLoginOptions.marketingConsentStatus?.ToApiString()
287+
} : null
288+
};
289289

290-
requestJson += "}";
291-
}
292-
293-
requestJson += "}";
290+
var requestJson = JsonUtility.ToJson(request);
294291

295292
var callResponse = await _communicationsManager.Call(PassportFunction.GET_PKCE_AUTH_URL, requestJson);
296293
var response = callResponse.OptDeserializeObject<StringResponse>();
@@ -299,10 +296,6 @@ private async UniTask LaunchAuthUrl()
299296
{
300297
var url = response.result.Replace(" ", "+");
301298

302-
// force marketing consent to true for now
303-
// TODO: remove this once we have a way to get the marketing consent from the user
304-
url = url + "&marketingConsent=opted_in";
305-
306299
#if UNITY_ANDROID && !UNITY_EDITOR
307300
loginPKCEUrl = url;
308301
SendAuthEvent(_pkceLoginOnly ? PassportAuthEvent.LoginPKCELaunchingCustomTabs : PassportAuthEvent.ConnectImxPKCELaunchingCustomTabs);
@@ -818,4 +811,25 @@ async void onDeeplinkResult(string url)
818811
}
819812
}
820813
#endif
814+
815+
/// <summary>
816+
/// Serializable request class for LaunchAuthUrl to replace manual JSON string concatenation
817+
/// </summary>
818+
[Serializable]
819+
internal class AuthUrlRequest
820+
{
821+
public bool isConnectImx;
822+
public DirectLoginRequestOptions directLoginOptions;
823+
}
824+
825+
/// <summary>
826+
/// Serializable class for directLoginOptions within AuthUrlRequest
827+
/// </summary>
828+
[Serializable]
829+
internal class DirectLoginRequestOptions
830+
{
831+
public string directLoginMethod;
832+
public string email;
833+
public string marketingConsentStatus;
834+
}
821835
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,23 @@ private async void HandleLoginData(string jsonData)
449449
loginOptions.email = loginData.email;
450450
}
451451

452+
// Parse and set marketing consent status if provided
453+
if (!string.IsNullOrEmpty(loginData.marketingConsentStatus))
454+
{
455+
switch (loginData.marketingConsentStatus.ToLower())
456+
{
457+
case "opted_in":
458+
loginOptions.marketingConsentStatus = MarketingConsentStatus.OptedIn;
459+
break;
460+
case "unsubscribed":
461+
loginOptions.marketingConsentStatus = MarketingConsentStatus.Unsubscribed;
462+
break;
463+
default:
464+
PassportLogger.Warn($"{TAG} Unknown marketing consent status: {loginData.marketingConsentStatus}");
465+
break;
466+
}
467+
}
468+
452469
// Perform the login and handle the result
453470
PassportLogger.Info($"{TAG} Starting login with method: {loginData.directLoginMethod}");
454471
bool loginSuccess = await _passportInstance.Login(false, loginOptions);

0 commit comments

Comments
 (0)