Skip to content

Commit 08fedd5

Browse files
committed
feat: Add support for discord authentication
1 parent ab8367d commit 08fedd5

File tree

4 files changed

+164
-1
lines changed

4 files changed

+164
-1
lines changed

Runtime/Client/LootLockerEndPoints.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class LootLockerEndPoints
1313
public static EndPointClass endingSession = new EndPointClass("v1/session", LootLockerHTTPMethod.DELETE);
1414
public static EndPointClass nintendoSwitchSessionRequest = new EndPointClass("session/nintendo-switch", LootLockerHTTPMethod.POST);
1515
public static EndPointClass epicSessionRequest = new EndPointClass("session/epic", LootLockerHTTPMethod.POST);
16+
public static EndPointClass discordSessionRequest = new EndPointClass("session/discord", LootLockerHTTPMethod.POST);
17+
public static EndPointClass discordRefreshSessionRequest = new EndPointClass("session/discord", LootLockerHTTPMethod.POST);
1618
public static EndPointClass metaSessionRequest = new EndPointClass("session/meta", LootLockerHTTPMethod.POST);
1719
public static EndPointClass xboxSessionRequest = new EndPointClass("session/xbox-one", LootLockerHTTPMethod.POST);
1820
public static EndPointClass appleSessionRequest = new EndPointClass("session/apple", LootLockerHTTPMethod.POST);

Runtime/Game/LootLockerSDKManager.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,134 @@ public static void RefreshMetaSession(string refresh_token, Action<LootLockerMet
14601460
);
14611461
}
14621462

1463+
/// <summary>
1464+
/// Start a Discord session.
1465+
/// The Discord platform must be enabled and configured in the web console for this to work.
1466+
/// A game can support multiple platforms, but it is recommended that a build only supports one platform.
1467+
/// </summary>
1468+
/// <param name="accessToken">The player's Discord OAuth token</param>
1469+
/// <param name="onComplete">onComplete Action for handling the response</param>
1470+
public static void StartDiscordSession(string accessToken, Action<LootLockerDiscordSessionResponse> onComplete)
1471+
{
1472+
if (!CheckInitialized(true))
1473+
{
1474+
onComplete?.Invoke(null);
1475+
return;
1476+
}
1477+
1478+
LootLockerServerRequest.CallAPI(null,
1479+
LootLockerEndPoints.discordSessionRequest.endPoint,
1480+
LootLockerEndPoints.discordSessionRequest.httpMethod,
1481+
LootLockerJson.SerializeObject(new LootLockerDiscordSessionRequest(accessToken)),
1482+
(serverResponse) =>
1483+
{
1484+
var response = LootLockerResponse.Deserialize<LootLockerDiscordSessionResponse>(serverResponse);
1485+
if (response.success)
1486+
{
1487+
LootLockerStateData.SetPlayerData(new LootLockerPlayerData
1488+
{
1489+
SessionToken = response.session_token,
1490+
RefreshToken = response.refresh_token,
1491+
ULID = response.player_ulid,
1492+
Identifier = "",
1493+
PublicUID = response.public_uid,
1494+
LegacyID = response.player_id,
1495+
Name = response.player_name,
1496+
WhiteLabelEmail = "",
1497+
WhiteLabelToken = "",
1498+
CurrentPlatform = LootLockerAuthPlatform.GetPlatformRepresentation(LL_AuthPlatforms.Discord),
1499+
LastSignIn = DateTime.Now,
1500+
CreatedAt = response.player_created_at,
1501+
WalletID = response.wallet_id,
1502+
});
1503+
}
1504+
1505+
onComplete?.Invoke(response);
1506+
},
1507+
false
1508+
);
1509+
}
1510+
1511+
/// <summary>
1512+
/// Refresh a previous Discord session
1513+
/// A response code of 400 (Bad request) could mean that the refresh token has expired and you'll need to sign in again
1514+
/// The Discord platform must be enabled and configured in the web console for this to work.
1515+
/// </summary>
1516+
/// <param name="onComplete">onComplete Action for handling the response</param>
1517+
/// <param name="forPlayerWithUlid">Optional : Execute the request for the specified player. If not supplied, the default player will be used.</param>
1518+
public static void RefreshDiscordSession(Action<LootLockerDiscordSessionResponse> onComplete, string forPlayerWithUlid = null)
1519+
{
1520+
var playerData = LootLockerStateData.GetStateForPlayerOrDefaultStateOrEmpty(forPlayerWithUlid);
1521+
if (string.IsNullOrEmpty(playerData?.RefreshToken))
1522+
{
1523+
onComplete?.Invoke(LootLockerResponseFactory.TokenExpiredError<LootLockerDiscordSessionResponse>(playerData?.ULID));
1524+
return;
1525+
}
1526+
1527+
RefreshDiscordSession(playerData.RefreshToken, onComplete, forPlayerWithUlid);
1528+
}
1529+
1530+
/// <summary>
1531+
/// Refresh a previous Discord session
1532+
/// If you do not want to manually handle the refresh token we recommend using the RefreshDiscordSession(Action<LootLockerDiscordSessionResponse> onComplete, string forPlayerWithUlid) method.
1533+
/// A response code of 400 (Bad request) could mean that the refresh token has expired and you'll need to sign in again
1534+
/// The Discord platform must be enabled and configured in the web console for this to work.
1535+
/// </summary>
1536+
/// <param name="refresh_token">Token received in response from StartDiscordSession request</param>
1537+
/// <param name="onComplete">onComplete Action for handling the response of type LootLockerDiscordSessionResponse</param>
1538+
/// <param name="forPlayerWithUlid">Optional : Execute the request for the specified player. If not supplied, the default player will be used.</param>
1539+
public static void RefreshDiscordSession(string refresh_token, Action<LootLockerDiscordSessionResponse> onComplete, string forPlayerWithUlid = null)
1540+
{
1541+
if (!CheckInitialized(true))
1542+
{
1543+
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerDiscordSessionResponse>(forPlayerWithUlid));
1544+
return;
1545+
}
1546+
1547+
if (string.IsNullOrEmpty(refresh_token))
1548+
{
1549+
var playerData = LootLockerStateData.GetStateForPlayerOrDefaultStateOrEmpty(forPlayerWithUlid);
1550+
if (string.IsNullOrEmpty(playerData?.RefreshToken))
1551+
{
1552+
onComplete?.Invoke(LootLockerResponseFactory.TokenExpiredError<LootLockerDiscordSessionResponse>(playerData?.ULID));
1553+
return;
1554+
}
1555+
refresh_token = playerData.RefreshToken;
1556+
forPlayerWithUlid = playerData.ULID;
1557+
}
1558+
1559+
LootLockerServerRequest.CallAPI(forPlayerWithUlid,
1560+
LootLockerEndPoints.discordSessionRequest.endPoint, LootLockerEndPoints.discordSessionRequest.httpMethod,
1561+
LootLockerJson.SerializeObject(new LootLockerDiscordRefreshSessionRequest(refresh_token)),
1562+
(serverResponse) =>
1563+
{
1564+
var response = LootLockerResponse.Deserialize<LootLockerDiscordSessionResponse>(serverResponse);
1565+
if (response.success)
1566+
{
1567+
LootLockerStateData.SetPlayerData(new LootLockerPlayerData
1568+
{
1569+
SessionToken = response.session_token,
1570+
RefreshToken = response.refresh_token,
1571+
ULID = response.player_ulid,
1572+
Identifier = "",
1573+
PublicUID = response.public_uid,
1574+
LegacyID = response.player_id,
1575+
Name = response.player_name,
1576+
WhiteLabelEmail = "",
1577+
WhiteLabelToken = "",
1578+
CurrentPlatform = LootLockerAuthPlatform.GetPlatformRepresentation(LL_AuthPlatforms.Discord),
1579+
LastSignIn = DateTime.Now,
1580+
CreatedAt = response.player_created_at,
1581+
WalletID = response.wallet_id,
1582+
});
1583+
}
1584+
1585+
onComplete?.Invoke(response);
1586+
},
1587+
false
1588+
);
1589+
}
1590+
14631591
/// <summary>
14641592
/// End active session (if any exists)
14651593
/// Succeeds if a session was ended or no sessions were active

Runtime/Game/Platforms/PlatformManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public enum LL_AuthPlatforms
2121
,Epic
2222
,Meta
2323
,Remote
24+
,Discord
2425
}
2526
public struct LL_AuthPlatformRepresentation
2627
{
@@ -31,7 +32,7 @@ public struct LL_AuthPlatformRepresentation
3132

3233
public class LootLockerAuthPlatformSettings
3334
{
34-
public static List<LL_AuthPlatforms> PlatformsWithRefreshTokens = new List<LL_AuthPlatforms> { LL_AuthPlatforms.AppleGameCenter, LL_AuthPlatforms.AppleSignIn, LL_AuthPlatforms.Epic, LL_AuthPlatforms.Google, LL_AuthPlatforms.GooglePlayGames, LL_AuthPlatforms.Remote };
35+
public static List<LL_AuthPlatforms> PlatformsWithRefreshTokens = new List<LL_AuthPlatforms> { LL_AuthPlatforms.AppleGameCenter, LL_AuthPlatforms.AppleSignIn, LL_AuthPlatforms.Epic, LL_AuthPlatforms.Google, LL_AuthPlatforms.GooglePlayGames, LL_AuthPlatforms.Remote, LL_AuthPlatforms.Discord };
3536
public static List<LL_AuthPlatforms> PlatformsWithStoredAuthData = new List<LL_AuthPlatforms> { LL_AuthPlatforms.Guest, LL_AuthPlatforms.WhiteLabel, LL_AuthPlatforms.AmazonLuna, LL_AuthPlatforms.PlayStationNetwork, LL_AuthPlatforms.XboxOne };
3637
}
3738

@@ -68,6 +69,7 @@ static LootLockerAuthPlatform()
6869
,"epic_games" // Epic Online Services / Epic Games
6970
,"meta" // Meta
7071
,"remote" // Remote (leased) session
72+
,"discord" // Discord
7173
};
7274

7375
private static readonly string[] PlatformFriendlyStrings = new[]
@@ -88,6 +90,7 @@ static LootLockerAuthPlatform()
8890
,"Epic Online Services" // Epic Online Services / Epic Games
8991
,"Meta" // Meta
9092
,"Remote" // Remote (leased) session
93+
,"Discord" // Discord
9194
};
9295

9396
public static LL_AuthPlatformRepresentation GetPlatformRepresentation(LL_AuthPlatforms platform)

Runtime/Game/Requests/LootLockerSessionRequest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,34 @@ public LootLockerAppleGameCenterRefreshSessionRequest(string refreshToken)
367367
this.refresh_token = refreshToken;
368368
}
369369
}
370+
public class LootLockerDiscordSessionRequest
371+
{
372+
public string game_key => LootLockerConfig.current.apiKey;
373+
public string access_token { get; set; }
374+
public string game_version => LootLockerConfig.current.game_version;
375+
376+
public LootLockerDiscordSessionRequest(string accessToken)
377+
{
378+
this.access_token = accessToken;
379+
}
380+
}
381+
382+
public class LootLockerDiscordRefreshSessionRequest
383+
{
384+
public string game_key => LootLockerConfig.current.apiKey;
385+
public string refresh_token { get; set; }
386+
public string game_version => LootLockerConfig.current.game_version;
387+
388+
public LootLockerDiscordRefreshSessionRequest(string refreshToken)
389+
{
390+
this.refresh_token = refreshToken;
391+
}
392+
}
393+
394+
[Serializable]
395+
public class LootLockerDiscordSessionResponse : LootLockerSessionResponse
396+
{
397+
public string refresh_token { get; set; }
398+
public string player_identifier { get; set; }
399+
}
370400
}

0 commit comments

Comments
 (0)