Beispiel #1
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);
        }