Add sign-up and sign-in functionality

Now it is time to begin adding player sign-up and sign-in functionality to the game. In Unity, you will see a user login UI like below. Currently, it doesn’t work – but we will fix that!

  • In the Unity project’s Assets folder, go to Scripts and open the CredentialsManager.cs script.

  • You should see three credential variables:

    • identityPool is your Cognito Identity Pool ID
    • userPoolId is your Cognito User Pool ID
    • appClientId is your Cognito User Pool App Client ID
  • Fill in the string values for these three variables, which were recorded in the previous sections. If you do not have the values for these variables, you can find them in the AWS Management Console under Cognito Identity Pools and User Pools.

Let’s analyze the code in the CredentialsManager.cs script to understand what is happening. First, we initialize a region variable, which is the the AWS Region that our backend resources exist in. This lab uses USEast1 as the default region, however if you are operating in a different region, make sure to change your region code for this variable.

    public static RegionEndpoint region = RegionEndpoint.USEast1;

Next, we initialize our Cognito credential variables. Again, remember to change the string values to the ones of your Cognito identity pool ID, user pool ID, and app client ID.

    public const string identityPool = "identity-pool-id-here";
    public static string userPoolId = "user-pool-id-here";
    public static string appClientId = "app-client-id-here";

After, we need to intialize the Amazon Cognito credentials provider, which is in charge of generating or retrieving a unique identifier for your end users and delivering temporary credentials to access your AWS resources.

    public static CognitoAWSCredentials credentials = new CognitoAWSCredentials(
        identityPool, region
    );

Finally, we are storing the userid in a variable upon sucecssful user login. This is mainly for data analytics so we can centralize data analytics around the player.

    public static string userid = "";
  • Save the script once you are done editing and try testing the functionality.

  • Play the game and sign up for an account by selecting New User? Sign Up Instead.

  • Put a sample username, password, and your real email, and press the Sign up button. Make sure to use your real email so that you can confirm the registration process. If you do not want to use your email address, you can enter a fake one and manually confirm the user in the Cognito console.

  • You should get a success response in the Unity console.

  • To verify that you now have a user in your user pool, go back to the AWS Management Console, go to Cognito, choose Manage User Pools and select your user pool.

  • On the left hand navigation pane, click Users and groups and you should see your new user, similar to:

  • You can also view additional information about the user. If see that your user is UNCONFIRMED, check your email and verify the registration through the email you get from Cognito. If you do not want to provide your real email, click into the user to view details and select CONFIRM.

  • Now that you have signed up for an account, try to enter your details and Login.

  • Upon a successful sign in, you should see the following success messages in the console:

You should see the access token has been returned in the console. It is a JWT token that should look similar to this:

User Access Token: eyJraWQiOiIydU5JSHNvTkttYlhwdzNTTkFvbnV5empqZjJnVytxTm9KYndFZ2hRUjFZPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJiOGE2YjNhYS1kMjgyLTQzY2EtYmY1Ni1jYmY5ZjdhZDliOTUiLCJldmVudF9pZCI6IjVkMzExN2Q1LTJiOWItNGMwMi1iZDAxLWQ2ZmU1OTdmNWE1YyIsInRva2VuX3VzZSI6ImFjY2VzcyIsInNjb3BlIjoiYXdzLmNvZ25pdG8uc2lnbmluLnVzZXIuYWRtaW4iLCJhdXRoX3RpbWUiOjE1NjQ2ODcxNzgsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC51cy1lYXN0LTEuYW1hem9uYXdzLmNvbVwvdXMtZWFzdC0xX2J6RVp5RHZwQSIsImV4cCI6MTU2NDY5MDc3OCwiaWF0IjoxNTY0Njg3MTc4LCJqdGkiOiI4MjgyOTk0MC1mMjNjLTQxZGQtYjVlYi1mZjgxZjYwZWI4YzUiLCJjbGllbnRfaWQiOiIyNGtmMmNnZGRtYm45a2g3OG9wbG9mMTAwOSIsInVzZXJuYW1lIjoidGVzdFVzZXIifQ.PgREPvj4l6T6KeFYNlqKdd41CVhELcURXE_NhZ-WOfrtiMKqt9q_SWQ5hR_CK-MAtxz-nBJ_CoghmLcuImsIRMhyGI8NSbBUJhzakg-S5eyzV3PTxeO2Z9g76KFwZ-0ufwuhbJZKl02-CN23sVnJaFJApSR5ZlOGClSIKlqPHFTzHUA6IVCZWpasLzpIfzCMioO1Oe6l-tXR3p0OSRMH0FndIxUXP1zu1OkhM9VAboGbw7vD2dfydNkVdaDLfMmgnCG5x02A0TcZYtSFdYSmjtTG__UymWWuKqYNZd1hZPjNtIc-D24ec59hIFSTR-y6aOBO96nWSObB_Pema2AEpA
  • You can decode this JWT token using https://jwt.io/. Copy the JWT User Access Token that is returned in the console and open this link in a web browser

  • Paste the JWT token you copied into the encoded section of this website. It should return decoded information like the header, payload, and signature. It will look similar to this:

Congradulations! You have added user sign-up and sign-in functionality to your Unity game. Continue on to the next section to understand the sign-up and sign-in code that provides this functionality.