Skip to content

Commit

Permalink
Added Facebook custom login.
Browse files Browse the repository at this point in the history
  • Loading branch information
oliexe committed Jul 24, 2024
1 parent 7abf735 commit f7fe973
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions Assets/Stash/Scripts/Core/StashConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class StashConstants
public const string CustomLogin = "/sdk/custom_login/approve_with_jwt";
public const string LoginAppleGameCenter = "/sdk/custom_login/approve_apple_game_center";
public const string LoginGooglePlayGames = "/sdk/custom_login/google_play";
public const string LoginFacebook = "/sdk/custom_login/facebook_auth";

}
}
55 changes: 55 additions & 0 deletions Assets/Stash/Scripts/Core/StashCustomLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class StashCustomLogin
/// <param name="playerId">Player identification, that will be used to identify purchases.</param>
/// <param name="idToken">Valid identification token (OICD) of the player.</param>
/// <param name="profileImageUrl">URL to the player's profile image/avatar to be displayed during login and on web shop.</param>
/// <param name="environment">Stash API environment (Defaults to Test).</param>
/// <returns>Returns a confirmation response, or throws StashAPIRequestError if fails.</returns>
public static async Task<LinkResponse> CustomLogin(string code, string playerId, string idToken, string profileImageUrl, StashEnvironment environment = StashEnvironment.Test)
{
Expand Down Expand Up @@ -146,6 +147,7 @@ public static async Task<LinkResponse> LinkAppleGameCenter(string code, string p
/// <param name="code">Stash code challenge from the deeplink.</param>
/// <param name="playerId">Player identification, that will be used to identify purchases.</param>
/// <param name="authCode">The authorization code generated using RequestServerSideAccess</param>
/// <param name="environment">Stash API environment (Defaults to Test).</param>
/// <returns>A LinkResponse object.</returns>
public static async Task<LinkResponse> LinkGooglePlayGames(string code, string playerId, string authCode, StashEnvironment environment = StashEnvironment.Test)
{
Expand Down Expand Up @@ -186,5 +188,58 @@ public static async Task<LinkResponse> LinkGooglePlayGames(string code, string p
throw new StashRequestError(result.StatusCode, result.Data);
}
}



/// <summary>
/// Logs in to stash web shop via Facebook account.
/// Requires valid access token generated from Facebook login.
/// </summary>
/// <param name="code">Stash code challenge from the deeplink.</param>
/// <param name="playerId">Player identification, that will be used to identify purchases.</param>
/// <param name="appId">Facebook app id.</param>
/// <param name="accessToken">Facebook access token.</param>
/// <param name="environment">Stash API environment (Defaults to Test).</param>
/// <returns>A LinkResponse object.</returns>
public static async Task<LinkResponse> LinkFacebook(string code, string playerId, string appId, string accessToken, StashEnvironment environment = StashEnvironment.Test)
{
// Create the request body with the challenge and internal user id
var requestBody = new LoginFacebook()
{
code = code,
appId = appId,
inputToken = accessToken,
user = new LoginFacebook.User()
{
id = playerId
}
};

// Set the URL for the link account endpoint
string requestUrl = environment.GetRootUrl() + StashConstants.LoginGooglePlayGames;
// Make a POST request to link the access token
Response result = await RestClient.Post(requestUrl, JsonUtility.ToJson(requestBody));

// Check the response status code
if (result.StatusCode == 200)
{
try
{
Debug.Log("[RESPONSE RAW] " + result.Data);
LinkResponse resultResponse = JsonUtility.FromJson<LinkResponse>(result.Data);
return resultResponse;
}
catch
{
// Throw an error if there is an issue parsing the response data
throw new StashParseError(result.Data);
}
}
else
{
// Throw an error if the API request was not successful
throw new StashRequestError(result.StatusCode, result.Data);
}
}
}
}
19 changes: 19 additions & 0 deletions Assets/Stash/Scripts/Models/LoginFacebook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace Stash.Models
{
[Serializable]
public class LoginFacebook
{
public string code;
public string appId;
public string inputToken;
public User user;

[Serializable]
public class User
{
public string id;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Stash/Scripts/Models/LoginFacebook.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f7fe973

Please sign in to comment.