question

Grant McNamara avatar image
Grant McNamara asked

How Would i make a java script register?

Im making a game and i am trying to make a register function but i dont know how i have the html code

<!DOCTYPE html>
<html>
    <head>
      <title>Register - That New Platformer Game</title>
        <script src="https://download.playfab.com/PlayFabClientApi.js"></script> 
        <!-- Uncomment below to include additional API Sets. Server and Admin require a DeveloperSecretKey -->
        <!-- <script src="//download.playfab.com/PlayFabServerApi.js" ></script> -->
        <!-- <script src="//download.playfab.com/PlayFabAdminApi.js" ></script> -->
        <!-- <script src="//download.playfab.com/PlayFabMatchmakerApi.js" ></script> -->
        <script src="playfabreg.js"></script>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
<h1 class="headcenter"><a href="index.html">Register</a></h1>
<input class="textbox" type="text" id="email" placeholder="Enter Your Email">
<input class="textbox" type="text" id="password" placeholder="Enter A Password 6-100 characters long">
<input class="textbox" type="text" id="username" placeholder="Enter Your User Name (Others Will see this)">
<input class="textbox" type="text" id="loginname" placeholder="Enter a name to login others will not see this">
<input type="button" class="myButton" onclick="RegisterUser()" value="Register">
<br> </br>
<div id="registerresult"> </div>

    </body>
</html>

and the following playfab register api script

          function RegisterUser()
          {
            // fetch title id
            var id = "97CAF";
            var displayname = document.getElementById("username");
            var username = document.getElementById("loginname");
            var email = document.getElementById("email");
            var password = document.getElementById("password");
            if(!id || id == "")
            {
              OutputError("TitleId cannot be null");
              return;
            }
            else if(typeof PlayFab == 'undefined') // make sure we have the SDK prior to calling / setting 
            {
              OutputError("The PlayFab SDK could not be found. Double check your script sources");
              return;
            }

            // save these values locally to ease use
            localStorage.userId = GUID;
            localStorage.titleId = id;
            
            // save title id
            PlayFab.settings.titleId = id;

            // build http request object for LoginWithCustomId
            var RegisterPlayFabUserRequest = {};
            RegisterPlayFabUserRequest.TitleId = id;
            RegisterPlayFabUserRequest.DisplayName = displayname;
            RegisterPlayFabUserRequest.RequireBothUsernameAndEmail   = true;
            RegisterPlayFabUserRequest.Email = email;
            RegisterPlayFabUserRequest.Password = password;
            RegisterPlayFabUserRequest.Username = username;

            OutputStatus("Logging into PlayFab...");
            PlayFabClientSDK.RegisterPlayFabUser(RegisterPlayFabUserRequest, (response, error) => {
                if(error)
                {
                  OutputError(error);
                } 
                else
                {
                  // display account details
                  var result = response.data;
                  var status = "Login Successful. <br \\> Welcome Player: " + result.PlayFabId + "<br \\> Your session ticket is: " + result.SessionTicket;
                  OutputStatus(status);
                }
            });
          };
          // some helper js methods 
          window.onload = function() 
          {
              var id = document.getElementById("titleId");

              if(!id)
              {
                OutputError("Please wait for game to load!");
                return;
              }
              else if(typeof PlayFab == 'undefined') // make sure we have the SDK prior to calling / setting 
              {
                OutputError("The PlayFab SDK could not be found. Double check your script sources");
                return;
              }
              id.value = localStorage.titleId ? localStorage.titleId : "";
          };

          function OutputError(error)
          {
            var output = document.getElementById("registerresult");

            if(output)
            {
              output.style.color = "red";
              output.innerHTML = typeof error == "string" ? error : error.status + " -- " + error.errorMessage;  
            }
            else
            {
              console.error("#result could not be located.");
              console.error(error);
            }
          }

          function OutputStatus(request, error)
          {
            var output = document.getElementById("registerresult");

            if(output)
            {
              output.style.color = "black";
              output.innerHTML = request;
            }
            else
            {
                console.log("#result could not be located.");
                console.log(request);
            }
          }

          function CreateGUID()
          {
            //http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);});
          }

But it is not working.

CloudScriptLeaderboards and Statistics
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rick Chen avatar image
Rick Chen answered

It seems that the JavaScript code you shared is not complete, the GUID in your playfabreg.js line 21 is not defined. The rest code looks fine. For the PlayFab JavaScript tutorial, you could refer to this document: JavaScript quickstart for Native and Phaser - PlayFab | Microsoft Docs.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

SZEINER avatar image
SZEINER answered

Hello can you have any update for this script? Cheers

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.