using Epic.OnlineServices; using Epic.OnlineServices.Auth; using Epic.OnlineServices.Stats; using PlayEveryWare.EpicOnlineServices; using System.Collections; using UnityEngine; public class EOSLogin : MonoBehaviour, IEOSCoroutineOwner { protected EOSAchievementManager achievementManager; IEnumerator Start() { EOSManager.Instance.Init(this); yield return new WaitForSecondsRealtime(2f); //you need to wait a while from app's startup, or will start to behave weirdly Login_ExchangeCode(); } void Login_ExchangeCode() { EOSManager.EOSSingleton.EpicLauncherArgs argsEpic = EOSManager.Instance.GetCommandLineArgsFromEpicLauncher(); EOSManager.Instance.StartLoginWithLoginTypeAndToken(LoginCredentialType.ExchangeCode, argsEpic.epicUserID, argsEpic.authPassword, (Epic.OnlineServices.Auth.LoginCallbackInfo callbackInfo) => { if (callbackInfo.ResultCode != Epic.OnlineServices.Result.Success) CreateAndConnectWithContinuanceToken(callbackInfo); else { HandleOnAuthSucceded(callbackInfo); } } ); } void CreateAndConnectWithContinuanceToken(Epic.OnlineServices.Auth.LoginCallbackInfo callbackInfo) { if (callbackInfo.ResultCode != Epic.OnlineServices.Result.Success) { // ask user if they want to connect; sample assumes they do EOSManager.Instance.CreateConnectUserWithContinuanceToken(callbackInfo.ContinuanceToken, (Epic.OnlineServices.Connect.CreateUserCallbackInfo createUserCallbackInfo) => { print("Creating new connect user"); EOSManager.Instance.StartConnectLoginWithEpicAccount(callbackInfo.LocalUserId, (Epic.OnlineServices.Connect.LoginCallbackInfo retryConnectLoginCallbackInfo) => { }); }); } else { HandleOnAuthSucceded(callbackInfo); } } bool invalidUserId = false; void HandleOnAuthSucceded(Epic.OnlineServices.Auth.LoginCallbackInfo callbackInfo) { Debug.Log("EOS LOGGED with token [" + callbackInfo.ResultCode + "]"); StartLoginWithLoginTypeAndTokenCallback(callbackInfo); //init achievements achievementManager = EOSManager.Instance.GetOrCreateManager(); achievementManager.GetAchievementDefinitionCount(); achievementManager.EnumerateCachedAchievementDefinitions(); ProductUserId userId = EOSManager.Instance.GetProductUserId(); if (userId == null) { Debug.LogError("User ID is NULL. invalidUserId"); invalidUserId = true; } else { if (userId.IsValid()) achievementManager.EnumerateCachedPlayerAchievement(userId); else invalidUserId = true; } achievementManager.AddNotifyAchievementDataUpdated(OnAchievementDataUpdated); IncrementLoginStat(); } //------------------------------------------------------------------------- public void StartLoginWithLoginTypeAndTokenCallback(LoginCallbackInfo loginCallbackInfo) { if (loginCallbackInfo.ResultCode == Epic.OnlineServices.Result.AuthMFARequired) { // collect MFA // do something to give the MFA to the SDK print("MFA Authentication not supported in sample. [" + loginCallbackInfo.ResultCode + "]"); } else if (loginCallbackInfo.ResultCode == Result.AuthPinGrantCode) { ///TODO(mendsley): Handle pin-grant in a more reasonable way Debug.LogError("------------PIN GRANT------------"); Debug.LogError("External account is not connected to an Epic Account. Use link below"); //Debug.LogError($"URL: {loginCallbackInfo.PinGrantInfo?.VerificationURI}"); //Debug.LogError($"CODE: {loginCallbackInfo.PinGrantInfo?.UserCode}"); Debug.LogError("---------------------------------"); } else if (loginCallbackInfo.ResultCode == Epic.OnlineServices.Result.Success) { StartConnectLoginWithLoginCallbackInfo(loginCallbackInfo); } else if (loginCallbackInfo.ResultCode == Epic.OnlineServices.Result.InvalidUser) { print("Trying Auth link with external account: " + loginCallbackInfo.ContinuanceToken); EOSManager.Instance.AuthLinkExternalAccountWithContinuanceToken(loginCallbackInfo.ContinuanceToken, LinkAccountFlags.NoFlags, (Epic.OnlineServices.Auth.LinkAccountCallbackInfo linkAccountCallbackInfo) => { StartConnectLoginWithLoginCallbackInfo(loginCallbackInfo); }); } else { print("Error logging in. [" + loginCallbackInfo.ResultCode + "]"); } } //------------------------------------------------------------------------- private void StartConnectLoginWithLoginCallbackInfo(LoginCallbackInfo loginCallbackInfo) { EOSManager.Instance.StartConnectLoginWithEpicAccount(loginCallbackInfo.LocalUserId, (Epic.OnlineServices.Connect.LoginCallbackInfo connectLoginCallbackInfo) => { if (connectLoginCallbackInfo.ResultCode == Result.Success) { print("Connect Login Successful. [" + connectLoginCallbackInfo.ResultCode + "]"); //ConfigureUIForLogout(); } else if (connectLoginCallbackInfo.ResultCode == Result.InvalidUser) { // ask user if they want to connect; sample assumes they do EOSManager.Instance.CreateConnectUserWithContinuanceToken(connectLoginCallbackInfo.ContinuanceToken, (Epic.OnlineServices.Connect.CreateUserCallbackInfo createUserCallbackInfo) => { print("Creating new connect user"); EOSManager.Instance.StartConnectLoginWithEpicAccount(loginCallbackInfo.LocalUserId, (Epic.OnlineServices.Connect.LoginCallbackInfo retryConnectLoginCallbackInfo) => { }); }); } }); } public void IncrementLoginStat() { if (invalidUserId) return; var statsInterface = EOSManager.Instance.GetEOSPlatformInterface().GetStatsInterface(); var userId = EOSManager.Instance.GetProductUserId(); IngestStatOptions ingestOptions = new IngestStatOptions() { LocalUserId = userId, TargetUserId = userId, Stats = new IngestData[] { new IngestData() { StatName = "login_count", IngestAmount = 1 } } }; statsInterface.IngestStat(ref ingestOptions, null, (ref IngestStatCompleteCallbackInfo info) => { //Debug.LogFormat("Stat ingest result: {0}", info.ResultCode.ToString()); achievementManager.RefreshData(); }); } private async void OnAchievementDataUpdated() { } // ----------------------------------------------------------------------- void IEOSCoroutineOwner.StartCoroutine(IEnumerator routine) { base.StartCoroutine(routine); } }