Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/merge anonymous accounts to rpm prompt #219

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
597 changes: 594 additions & 3 deletions Runtime/AvatarCreator/Prefabs/Elements/LoginElement.prefab

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Runtime/AvatarCreator/Scripts/Managers/AuthManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public static async void SendEmailCode(string email)
await AuthAPIRequests.SendCodeToEmail(email, userSession.Id);
}

public static async Task<bool> LoginWithCode(string otp)
public static async Task<bool> LoginWithCode(string otp, string userIdToMerge = null)
{
try
{
userSession = await AuthAPIRequests.LoginWithCode(otp);
userSession = await AuthAPIRequests.LoginWithCode(otp, userIdToMerge);
IsSignedIn = true;
OnSignedIn?.Invoke(userSession);
return true;
Expand Down
9 changes: 8 additions & 1 deletion Runtime/AvatarCreator/Scripts/UI/Elements/LoginElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class LoginElement : MonoBehaviour
[SerializeField] private UnityEvent OnLoginSuccess;
[SerializeField] private UnityEvent<string> OnLoginFail;

private bool mergeCurrentSession;
private void OnEnable()
{
AuthManager.OnSignInError += LoginFailed;
Expand All @@ -39,14 +40,20 @@ public void SendVerificationCode()
AuthManager.SendEmailCode(emailField.text);
}

public void MergeCurrentUserToRpmAccount(bool merge)
{
mergeCurrentSession = merge;
}

/// <summary>
/// Attempts to login with the verification code that was entered into the code InputField.
/// </summary>
public async void LoginWithCode()
{
try
{
if (await AuthManager.LoginWithCode(codeField.text))
var userIdToMerge = mergeCurrentSession && AuthManager.IsSignedInAnonymously ? AuthManager.UserSession.Id : null;
if (await AuthManager.LoginWithCode(codeField.text, userIdToMerge))
{
LoginSuccess();
}
Expand Down
15 changes: 10 additions & 5 deletions Runtime/AvatarCreator/Scripts/WebRequests/AuthAPIRequests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public AuthAPIRequests(string domain)
{
this.domain = domain;
webRequestDispatcher = new WebRequestDispatcher();

rpmAuthBaseUrl = string.Format(Env.RPM_SUBDOMAIN_BASE_URL, domain);
}

Expand Down Expand Up @@ -49,14 +49,19 @@ public async Task SendCodeToEmail(string email, string userId = "")
response.ThrowIfError();
}

public async Task<UserSession> LoginWithCode(string code)
public async Task<UserSession> LoginWithCode(string code, string userIdToMerge = null)
{
var payload = AuthDataConverter.CreatePayload(new Dictionary<string, string>
var body = new Dictionary<string, string>
{
{ AuthConstants.AUTH_TYPE_CODE, code }
});
};
if (userIdToMerge != null)
{
body.Add(AuthConstants.USER_ID, userIdToMerge);
}
var payload = AuthDataConverter.CreatePayload(body);

var response = await webRequestDispatcher.SendRequest<Response>( $"{rpmAuthBaseUrl}/auth/login", HttpMethod.POST, headers, payload);
var response = await webRequestDispatcher.SendRequest<Response>($"{rpmAuthBaseUrl}/auth/login", HttpMethod.POST, headers, payload);
response.ThrowIfError();

var data = AuthDataConverter.ParseResponse(response.Text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ This element is useful for creating Ready Player Me login UI.
- an input field for the user to enter their email address
- an input field for the user to enter in their 1 time login code
- functionality to automatically send a login code to the user after they enter their email address
- functionality to merge user session to their Ready Player Me account
- OnLoginSuccess event that can be subscribed to in the inspector
- OnLoginFailed event that can be subscribed to in the inspector and passes a string containing the error message

Expand Down
Loading