Lua Authentication

There are three ways to authenticate using GameSparks:

  1. GameSparks Authentication - Uses a username/password that players register to.
  2. Device Authentication - Uses a unique string ID (created by the Developer) and an OS identifier.
  3. Social Authentication - Requires you to use a token from the social provider to authenticate users using the information gathered from their profiles.

In this tutorial we'll only cover GameSparks and Device authentication because Social authentication is different for every provider and that is beyond the scope of this tutorial.

GameSparks Authentication

First you'll need to register your user by providing DisplayName, Username and Password.

Create a RegistrationRequest and set the values necessary for registration. This example has hard set values and for your game you'll need to have your players input these values:

--Build request
local requestBuilder = gs.getRequestBuilder()
local registerRequest = requestBuilder.createRegistrationRequest()

--Set values
registerRequest:setDisplayName("examplePlayer")
registerRequest:setUserName("exampleUsername")
registerRequest:setPassword("password")

--Send and check for errors, if no errors print username and message
registerRequest:send(function(authenticationResponse)
    if not authenticationResponse:hasErrors() then
    print(response:getUserName() " has successfully registered!")
    end
end)

Once registered, the player can now use their credentials to log in. When a player registers, they're automatically signed in and there's no need to authenticate again. However, in this tutorial we'll demonstrate the AuthenticationRequest.

To set this up, create an AuthenticationRequest and input the values you used to register:

--Build request
local requestBuilder = gs.getRequestBuilder()
local loginAuth = requestBuilder.createAuthenticationRequest()

--Set values    
loginAuth:setPassword("pass")
loginAuth:setUserName("gstest")

--Send and check for errors, if there are errors print them
loginAuth:send(function(authenticationResponse)
if authenticationResponse:hasErrors() then
       for key,value in pairs(authenticationResponse:getErrors()) do print(key,value) end
   end
end)

Device Authentication

Device authentication doesn't need registration and relies on the Device ID (which must be unique) and the Device OS. However, the Device OS is not restricted to designating operating systems and for this example we mark the OS as Corona:

--Build request
local requestBuilder = gs.getRequestBuilder()
local deviceAuthenticationRequest = requestBuilder.createDeviceAuthenticationRequest()

--Set values
deviceAuthenticationRequest:setDeviceId("1111")
deviceAuthenticationRequest:setDeviceOS("Corona")

--Send and print authentication token
deviceAuthenticationRequest:send(function(authenticationResponse)
writeText("token: "..authenticationResponse:getAuthToken().."\n")
end)

The tutorials in the next section will demonstrate creating and using custom functions and using persistent data.

Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here