private void AuthenticateGooglePlayGames()
    {
#if GOOGLEGAMES
        PlayFabClientAPI.LoginWithGoogleAccount(new LoginWithGoogleAccountRequest()
        {
            TitleId               = PlayFabSettings.TitleId,
            ServerAuthCode        = AuthTicket,
            InfoRequestParameters = InfoRequestParams,
            CreateAccount         = true
        }, (result) =>
        {
            //Store Identity and session
            _playFabId     = result.PlayFabId;
            _sessionTicket = result.SessionTicket;

            //check if we want to get this callback directly or send to event subscribers.
            if (OnLoginSuccess != null)
            {
                //report login result back to the subscriber
                OnLoginSuccess.Invoke(result);
            }
        }, (error) =>
        {
            //report errro back to the subscriber
            if (OnPlayFabError != null)
            {
                OnPlayFabError.Invoke(error);
            }
        });
#endif
    }
Beispiel #2
0
 internal void InvokePlayFabError(PlayFabError playFabError)
 {
     if (OnPlayFabError != null)
     {
         OnPlayFabError.Invoke(playFabError);
     }
 }
    /// <summary>
    /// Register a user with an Email & Password
    /// Note: We are not using the RegisterPlayFab API
    /// </summary>
    private void AddAccountAndPassword()
    {
        //Any time we attempt to register a player, first silently authenticate the player.
        //This will retain the players True Origination (Android, iOS, Desktop)
        SilentlyAuthenticate((result) => {
            if (result == null)
            {
                //something went wrong with Silent Authentication, Check the debug console.
                OnPlayFabError.Invoke(new PlayFabError()
                {
                    Error        = PlayFabErrorCode.UnknownError,
                    ErrorMessage = "Silent Authentication by Device failed"
                });
            }

            //Note: If silent auth is success, which is should always be and the following
            //below code fails because of some error returned by the server ( like invalid email or bad password )
            //this is okay, because the next attempt will still use the same silent account that was already created.

            //Now add our username & password.
            PlayFabClientAPI.AddUsernamePassword(new AddUsernamePasswordRequest()
            {
                Username = !string.IsNullOrEmpty(Username) ? Username : result.PlayFabId, //Because it is required & Unique and not supplied by User.
                Email    = Email,
                Password = Password,
            }, (addResult) => {
                if (OnLoginSuccess != null)
                {
                    //Store identity and session
                    _playFabId     = result.PlayFabId;
                    _sessionTicket = result.SessionTicket;

                    //If they opted to be remembered on next login.
                    if (RememberMe)
                    {
                        //Generate a new Guid
                        RememberMeId = Guid.NewGuid().ToString();
                        //Fire and forget, but link the custom ID to this PlayFab Account.
                        PlayFabClientAPI.LinkCustomID(new LinkCustomIDRequest()
                        {
                            CustomId  = RememberMeId,
                            ForceLink = ForceLink
                        }, null, null);
                    }

                    //Override the auth type to ensure next login is using this auth type.
                    AuthType = Authtypes.EmailAndPassword;

                    //Report login result back to subscriber.
                    OnLoginSuccess.Invoke(result);
                }
            }, (error) => {
                if (OnPlayFabError != null)
                {
                    //Report error result back to subscriber
                    OnPlayFabError.Invoke(error);
                }
            });
        });
    }
Beispiel #4
0
    private void AddAccountAndPassword()
    {
        if (OnLogMessage != null)
        {
            OnLogMessage.Invoke("AddAccountAndPassword Start");
        }
        //Any time we attempt to register a player, first silently authenticate the player.
        //This will retain the players True Origination (Android, iOS, Desktop)
        SilentlyAuthenticate((result) => {
            if (result == null)
            {
                //something went wrong with Silent Authentication, Check the debug console.
                OnPlayFabError.Invoke(new PlayFabError()
                {
                    Error        = PlayFabErrorCode.UnknownError,
                    ErrorMessage = "Silent Authentication by Device failed"
                });
                RegisterAuthenticate(result);
            }

            //Note: If silent auth is success, which is should always be and the following
            //below code fails because of some error returned by the server ( like invalid email or bad password )
            //this is okay, because the next attempt will still use the same silent account that was already created.

            //Now add our username & password.
            PlayFabClientAPI.AddUsernamePassword(new AddUsernamePasswordRequest()
            {
                Username = !string.IsNullOrEmpty(Username) ? Username : result.PlayFabId, //Because it is required & Unique and not supplied by User.
                Email    = Email,
                Password = Password,
            }, (addResult) => {
                if (OnLoginSuccess != null)
                {
                    //Store identity and session
                    _playFabId     = result.PlayFabId;
                    _sessionTicket = result.SessionTicket;

                    //Report login result back to subscriber.
                    OnLoginSuccess.Invoke();
                    if (OnLogMessage != null)
                    {
                        OnLogMessage.Invoke("AddUsernamePassword Success");
                    }
                }
            }, (error) => {
                if (OnPlayFabError != null)
                {
                    //Report error result back to subscriber
                    if (OnLogMessage != null)
                    {
                        OnLogMessage.Invoke("AddUsernamePassword Fail");
                    }
                }
                if (error.Error == PlayFabErrorCode.AccountAlreadyLinked)
                {
                    RegisterAuthenticate(result);
                }
            });
        });
    }
Beispiel #5
0
        protected virtual async Task AuthenticateFacebook()
        {
            //If there is no Facebook client, we can't do this
            if (null == Facebook)
            {
                Logout();
                OnPlayFabError?.Invoke(new PlayFabError()
                {
                    ErrorMessage = "No FacebookClient was detected",
                });
            }

            //Check if the user needs to log into Facebook
            if (!Facebook.LoggedIn || string.IsNullOrEmpty(AuthTicket))
            {
                //sign up for the logged in event
                Facebook.OnLoginSuccess -= OnFacebookLoggedIn;
                Facebook.OnLoginSuccess += OnFacebookLoggedIn;
                Facebook.OnLoginError   -= Facebook_OnLoginError;
                Facebook.OnLoginError   += Facebook_OnLoginError;

                //try to log in
                Facebook.Login();
            }
            else
            {
                //just run that event
                await AuthenticateFacebookUser(Facebook.User).ConfigureAwait(false);
            }
        }
 private void StatusFail(PlayFabError error)
 {
     if (OnPlayFabError != null)
     {
         OnPlayFabError.Invoke(error);
     }
 }
Beispiel #7
0
 private void LoginResult <T>(PlayFabResult <T> result) where T : PlayFabResultCommon
 {
     if (null != result.Error)
     {
         //report error back to subscriber
         Logout();
         OnPlayFabError?.Invoke(result.Error);
     }
     else
     {
         //report login result back to subscriber
         OnLoginSuccess?.Invoke(new LoginResult
         {
         });
     }
 }
Beispiel #8
0
        private void LoginResult(PlayFabResult <LoginResult> result)
        {
            if (null != result.Error)
            {
                //report error back to subscriber
                Logout();
                OnPlayFabError?.Invoke(result.Error);
            }
            else
            {
                //Store identity and session
                PlayFabId     = result.Result.PlayFabId;
                SessionTicket = result.Result.SessionTicket;

                //report login result back to subscriber
                OnLoginSuccess?.Invoke(result.Result);
            }
        }
Beispiel #9
0
    private void AddGoogleAccount(System.Action <LoginResult> callback = null)
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        Debug.Log("Server Auth Code: " + AuthCode);

        PlayFabClientAPI.LoginWithGoogleAccount(new LoginWithGoogleAccountRequest()
        {
            TitleId        = PlayFabSettings.TitleId,
            ServerAuthCode = AuthCode,
            CreateAccount  = true
        }, (result) =>
        {
            //Store Identity and session
            _playFabId     = result.PlayFabId;
            _sessionTicket = result.SessionTicket;

            //check if we want to get this callback directly or send to event subscribers.
            if (callback == null && OnLoginSuccess != null)
            {
                //report login result back to the subscriber
                OnLoginSuccess.Invoke(result);
            }
            else if (callback != null)
            {
                //report login result back to the caller
                callback.Invoke(result);
            }
        }, error =>
        {
            //report errro back to the subscriber
            if (callback == null && OnPlayFabError != null)
            {
                OnPlayFabError.Invoke(error);
            }
            else
            {
                //make sure the loop completes, callback with null
                callback.Invoke(null);
                //Output what went wrong to the console.
                Debug.LogError(error.GenerateErrorReport());
            }
        });
#endif
    }
    private void AuthenticateFacebook()
    {
#if FACEBOOK
        if (FB.IsInitialized && FB.IsLoggedIn && !string.IsNullOrEmpty(AuthTicket))
        {
            PlayFabClientAPI.LoginWithFacebook(new LoginWithFacebookRequest()
            {
                TitleId               = PlayFabSettings.TitleId,
                AccessToken           = AuthTicket,
                CreateAccount         = true,
                InfoRequestParameters = InfoRequestParams
            }, (result) =>
            {
                //Store Identity and session
                _playFabId     = result.PlayFabId;
                _sessionTicket = result.SessionTicket;

                //check if we want to get this callback directly or send to event subscribers.
                if (OnLoginSuccess != null)
                {
                    //report login result back to the subscriber
                    OnLoginSuccess.Invoke(result);
                }
            }, (error) =>
            {
                //report errro back to the subscriber
                if (OnPlayFabError != null)
                {
                    OnPlayFabError.Invoke(error);
                }
            });
        }
        else
        {
            if (OnDisplayAuthentication != null)
            {
                OnDisplayAuthentication.Invoke();
            }
        }
#endif
    }
Beispiel #11
0
        public async Task LinkFacebook()
        {
            //If there is no Facebook client, we can't do this
            if (null == Facebook)
            {
                Logout();
                OnPlayFabError?.Invoke(new PlayFabError()
                {
                    ErrorMessage = "No FacebookClient was detected",
                });
            }

            //Check if the user needs to log into Facebook
            if (!Facebook.LoggedIn || string.IsNullOrEmpty(AuthTicket))
            {
                //sign up for the logged in event
                Facebook.OnLoginSuccess -= OnFacebookLoggedIn;
                Facebook.OnLoginSuccess += OnFacebookLoggedIn;
                Facebook.OnLoginError   -= Facebook_OnLoginError;
                Facebook.OnLoginError   += Facebook_OnLoginError;

                //try to log in
                Facebook.Login();
            }
            else
            {
                OnLoggingIn?.Invoke();

                //grab the auth ticket from that user
                AuthTicket = Facebook.User.Token;

                var result = await PlayFabClient.LinkFacebookAccountAsync(new LinkFacebookAccountRequest()
                {
                    AccessToken = AuthTicket,
                }).ConfigureAwait(false);

                LoginResult(result);
            }
        }
Beispiel #12
0
    private void SilentlyAuthenticate(System.Action <LoginResult> callback = null)
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        //Get the device id from native android
        AndroidJavaClass  up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = up.GetStatic <AndroidJavaObject>("currentActivity");
        AndroidJavaObject contentResolver = currentActivity.Call <AndroidJavaObject>("getContentResolver");
        AndroidJavaClass  secure          = new AndroidJavaClass("android.provider.Settings$Secure");
        string            deviceId        = secure.CallStatic <string>("getString", contentResolver, "android_id");

        //Login with the android device ID
        PlayFabClientAPI.LoginWithAndroidDeviceID(new LoginWithAndroidDeviceIDRequest()
        {
            TitleId               = PlayFabSettings.TitleId,
            AndroidDevice         = SystemInfo.deviceModel,
            OS                    = SystemInfo.operatingSystem,
            AndroidDeviceId       = deviceId,
            CreateAccount         = true,
            InfoRequestParameters = InfoRequestParams
        }, (result) => {
            //Store Identity and session
            _playFabId     = result.PlayFabId;
            _sessionTicket = result.SessionTicket;

            //check if we want to get this callback directly or send to event subscribers.
            if (callback == null && OnLoginSuccess != null)
            {
                //report login result back to the subscriber
                OnLoginSuccess.Invoke(result);
            }
            else if (callback != null)
            {
                //report login result back to the caller
                callback.Invoke(result);
            }
        }, (error) => {
            //report errro back to the subscriber
            if (callback == null && OnPlayFabError != null)
            {
                OnPlayFabError.Invoke(error);
            }
            else
            {
                //make sure the loop completes, callback with null
                callback.Invoke(null);
                //Output what went wrong to the console.
                Debug.LogError(error.GenerateErrorReport());
            }
        });
#elif  UNITY_IPHONE || UNITY_IOS && !UNITY_EDITOR
        PlayFabClientAPI.LoginWithIOSDeviceID(new LoginWithIOSDeviceIDRequest()
        {
            TitleId               = PlayFabSettings.TitleId,
            DeviceModel           = SystemInfo.deviceModel,
            OS                    = SystemInfo.operatingSystem,
            DeviceId              = SystemInfo.deviceUniqueIdentifier,
            CreateAccount         = true,
            InfoRequestParameters = InfoRequestParams
        }, (result) => {
            //Store Identity and session
            _playFabId     = result.PlayFabId;
            _sessionTicket = result.SessionTicket;

            //check if we want to get this callback directly or send to event subscribers.
            if (callback == null && OnLoginSuccess != null)
            {
                //report login result back to the subscriber
                OnLoginSuccess.Invoke(result);
            }
            else if (callback != null)
            {
                //report login result back to the caller
                callback.Invoke(result);
            }
        }, (error) => {
            //report errro back to the subscriber
            if (callback == null && OnPlayFabError != null)
            {
                OnPlayFabError.Invoke(error);
            }
            else
            {
                //make sure the loop completes, callback with null
                callback.Invoke(null);
                //Output what went wrong to the console.
                Debug.LogError(error.GenerateErrorReport());
            }
        });
#else
        PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest()
        {
            TitleId               = PlayFabSettings.TitleId,
            CustomId              = SystemInfo.deviceUniqueIdentifier,
            CreateAccount         = true,
            InfoRequestParameters = InfoRequestParams
        }, (result) => {
            //Store Identity and session
            _playFabId     = result.PlayFabId;
            _sessionTicket = result.SessionTicket;

            //check if we want to get this callback directly or send to event subscribers.
            if (callback == null && OnLoginSuccess != null)
            {
                //report login result back to the subscriber
                OnLoginSuccess.Invoke(result);
            }
            else if (callback != null)
            {
                //report login result back to the caller
                callback.Invoke(result);
            }
        }, (error) => {
            //report errro back to the subscriber
            if (callback == null && OnPlayFabError != null)
            {
                OnPlayFabError.Invoke(error);
            }
            else
            {
                //make sure the loop completes, callback with null
                callback.Invoke(null);
                //Output what went wrong to the console.
                Debug.LogError(error.GenerateErrorReport());
            }
        });
#endif
    }
Beispiel #13
0
    /// <summary>
    /// Authenticate a user in PlayFab using an Email & Password combo
    /// </summary>
    private void AuthenticateEmailPassword()
    {
        //Check if the users has opted to be remembered.
        if (RememberMe && !string.IsNullOrEmpty(RememberMeId))
        {
            // If the user is being remembered, then log them in with a customid that was
            // generated by the RememberMeId property
            PlayFabClientAPI.LoginWithCustomID(
                new LoginWithCustomIDRequest()
            {
                TitleId               = PlayFabSettings.TitleId,
                CustomId              = RememberMeId,
                CreateAccount         = true,
                InfoRequestParameters = InfoRequestParams
            },

                // Success
                (LoginResult result) =>
            {
                //Store identity and session
                _playFabId     = result.PlayFabId;
                _sessionTicket = result.SessionTicket;

                if (OnLoginSuccess != null)
                {
                    //report login result back to subscriber
                    OnLoginSuccess.Invoke(result);
                }
            },

                // Failure
                (PlayFabError error) =>
            {
                if (OnPlayFabError != null)
                {
                    //report error back to subscriber
                    OnPlayFabError.Invoke(error);
                }
            });

            return;
        }

        // If username & password is empty, then do not continue, and Call back to Authentication UI Display
        if (string.IsNullOrEmpty(Email) && string.IsNullOrEmpty(Password))
        {
            OnDisplayAuthentication.Invoke();
            return;
        }

        // We have not opted for remember me in a previous session, so now we have to login the user with email & password.
        PlayFabClientAPI.LoginWithEmailAddress(
            new LoginWithEmailAddressRequest()
        {
            TitleId  = PlayFabSettings.TitleId,
            Email    = Email,
            Password = Password,
            InfoRequestParameters = InfoRequestParams
        },

            // Success
            (LoginResult result) =>
        {
            // Store identity and session
            _playFabId     = result.PlayFabId;
            _sessionTicket = result.SessionTicket;

            // Note: At this point, they already have an account with PlayFab using a Username (email) & Password
            // If RememberMe is checked, then generate a new Guid for Login with CustomId.
            if (RememberMe)
            {
                RememberMeId = Guid.NewGuid().ToString();
                AuthType     = Authtypes.EmailAndPassword;

                // Fire and forget, but link a custom ID to this PlayFab Account.
                PlayFabClientAPI.LinkCustomID(
                    new LinkCustomIDRequest
                {
                    CustomId  = RememberMeId,
                    ForceLink = ForceLink
                },
                    null,       // Success callback
                    null        // Failure callback
                    );
            }

            if (OnLoginSuccess != null)
            {
                //report login result back to subscriber
                OnLoginSuccess.Invoke(result);
            }
        },

            // Failure
            (PlayFabError error) =>
        {
            if (OnPlayFabError != null)
            {
                //Report error back to subscriber
                OnPlayFabError.Invoke(error);
            }
        });
    }
Beispiel #14
0
        protected virtual async Task SilentlyAuthenticate(Platform platform, System.Action <LoginResult> callback = null)
        {
            PlayFabResult <LoginResult> result = null;

            switch (platform)
            {
            case Platform.Android:
            {
                //Login with the android device ID
                result = await PlayFabClient.LoginWithAndroidDeviceIDAsync(new LoginWithAndroidDeviceIDRequest()
                    {
                        TitleId               = PlayFabSettings.staticSettings.TitleId,
                        AndroidDevice         = CrossDeviceInfo.Current.DeviceName,
                        OS                    = CrossDeviceInfo.Current.Platform.ToString(),
                        AndroidDeviceId       = CrossDeviceInfo.Current.Id,
                        CreateAccount         = true,
                        InfoRequestParameters = this.InfoRequestParams
                    }).ConfigureAwait(false);
            }
            break;

            case Platform.iOS:
            {
                result = await PlayFabClient.LoginWithIOSDeviceIDAsync(new LoginWithIOSDeviceIDRequest()
                    {
                        TitleId               = PlayFabSettings.staticSettings.TitleId,
                        DeviceModel           = CrossDeviceInfo.Current.Model,
                        OS                    = CrossDeviceInfo.Current.Platform.ToString(),
                        DeviceId              = CrossDeviceInfo.Current.Id,
                        CreateAccount         = true,
                        InfoRequestParameters = InfoRequestParams
                    }).ConfigureAwait(false);
            }
            break;

            default:
            {
                result = await PlayFabClient.LoginWithCustomIDAsync(new LoginWithCustomIDRequest()
                    {
                        TitleId               = PlayFabSettings.staticSettings.TitleId,
                        CustomId              = CrossDeviceInfo.Current.Id,
                        CreateAccount         = true,
                        InfoRequestParameters = InfoRequestParams
                    }).ConfigureAwait(false);
            }
            break;
            }

            if (null != result.Error)
            {
                //report errro back to the subscriber
                if (callback == null)
                {
                    Logout();
                    OnPlayFabError?.Invoke(result.Error);
                }
                else
                {
                    //make sure the loop completes, callback with null
                    callback.Invoke(null);
                }
            }
            else
            {
                //Store identity and session
                PlayFabId     = result.Result.PlayFabId;
                SessionTicket = result.Result.SessionTicket;

                //check if we want to get this callback directly or send to event subscribers.
                if (callback == null)
                {
                    //report login result back to the subscriber
                    OnLoginSuccess?.Invoke(result.Result);
                }
                else
                {
                    //report login result back to the caller
                    callback.Invoke(result.Result);
                }
            }
        }
Beispiel #15
0
        /// <summary>
        /// Register a user with an Email & Password
        /// Note: We are not using the RegisterPlayFab API
        /// </summary>
        protected virtual async Task AddAccountAndPassword()
        {
            //Any time we attempt to register a player, first silently authenticate the player.
            //This will retain the players True Origination (Android, iOS, Desktop)
            await SilentlyAuthenticate(CrossDeviceInfo.Current.Platform, async (result) =>
            {
                if (result == null)
                {
                    //something went wrong with Silent Authentication, Check the debug console.
                    Logout();
                    OnPlayFabError?.Invoke(new PlayFabError()
                    {
                        Error        = PlayFabErrorCode.UnknownError,
                        ErrorMessage = "Silent Authentication by Device failed"
                    });
                }

                //Note: If silent auth is success, which is should always be and the following
                //below code fails because of some error returned by the server ( like invalid email or bad password )
                //this is okay, because the next attempt will still use the same silent account that was already created.

                //Now add our username & password.
                var addUsernameResult = await PlayFabClient.AddUsernamePasswordAsync(new AddUsernamePasswordRequest()
                {
                    Username = !string.IsNullOrEmpty(this.Username) ? Username : result.PlayFabId,                     //Because it is required & Unique and not supplied by User.
                    Email    = this.Email,
                    Password = this.Password,
                }).ConfigureAwait(false);

                if (null != addUsernameResult.Error)
                {
                    //report error back to subscriber
                    Logout();
                    OnPlayFabError?.Invoke(addUsernameResult.Error);
                }
                else
                {
                    //Store identity and session
                    PlayFabId     = result.PlayFabId;
                    SessionTicket = result.SessionTicket;

                    //If they opted to be remembered on next login.
                    if (RememberMe)
                    {
                        //Generate a new Guid
                        RememberMeId = Guid.NewGuid().ToString();

                        //Fire and forget, but link the custom ID to this PlayFab Account.
                        await PlayFabClient.LinkCustomIDAsync(new LinkCustomIDRequest()
                        {
                            CustomId  = RememberMeId,
                            ForceLink = ForceLink
                        }).ConfigureAwait(false);
                    }

                    //Override the auth type to ensure next login is using this auth type.
                    AuthType = AuthType.EmailAndPassword;

                    //Report login result back to subscriber.
                    OnLoginSuccess?.Invoke(result);
                }
            }).ConfigureAwait(false);
        }