question

Kim Strasser avatar image
Kim Strasser asked

How can I get the timestamp of a session ticket?

I want to save the player's current session ticket and its timestamp in Player Internal Data but I don't know how to get the timestamp of the session ticket.

I know that I can get the session ticket in the client like this after the player logged in:

string playersessionTicket = task.Result.Result.SessionTicket;

But how can I get its timestamp? I need to find out if the session ticket is still valid.

In addition, how can I compare in Cloud Script if the session ticket is still valid?

For example, the player calls PlayFabClientAPI.LoginWithPlayFab or PlayFabClientAPI.LoginWithIOSDeviceID on a second device. Then, I want to call my cloud script on this second device to find out if the currently saved session ticket(in Player Internal Data) is still valid before the player can make any other API calls on this second device.

I don't know how to find out in Cloud Script if the saved session ticket is still valid. How can I do that?

args.NewSessionTicket gets its value from playersessionTicket = task.Result.Result.SessionTicket.

handlers.GetSessionTicketInternalData = function (args, context)
{
   var resultdata = server.GetUserInternalData({PlayFabId: currentPlayerId, Keys: "PlayerSessionTicket"});
   var newsessionticket = args.NewSessionTicket;
   var currentsessionticket = "";
   var isplayerloggedin = "";
   
   if ((resultdata.Data.hasOwnProperty("PlayerSessionTicket")) && (resultdata.Data.PlayerSessionTicket.Value != "") && (resultdata.Data.PlayerSessionTicket.Value != null))
   {
        log.info("Player has already saved a session ticket before.");
        currentsessionticket = resultdata.Data.PlayerSessionTicket.Value;

        //  How can I find out if currentsessionticket is still valide?
        if (currentsessionticket <= time ???) 
            isplayerloggedin = true;
        else
        {
            isplayerloggedin = false;
            //  Update Player Internal Data with the value of newsessionticket because currentsessionticket is already expired.
            //  ...
        }
   }
   else
   {
        CreateSessionTicketInternalData(newsessionticket);
        isplayerloggedin = false;
        log.info("Player created a new session ticket.");
    }
   
   //  The player should only be able to login and play the game if "playercanlogin" is false.
   return isplayerloggedin;
}

function CreateSessionTicketInternalData(newticket)
{
    server.UpdateUserInternalData({
           PlayFabId: currentPlayerId,
           Data: {
               "PlayerSessionTicket": newticket
           },
           Permission: UserDataPermission.Private
        });
        
    log.info("Created internal PlayerSessionTicket data.");
}
CloudScript
10 |1200

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

1 Answer

·
Seth Du avatar image
Seth Du answered

You may retrieve the session ticket in the Player Internal Data in the Cloud Script, then call AuthenticateSessionTicket Server API to get the creation date.

Besides, there is a sub-property named TokenExpiration inside EntityToken property of Callback result of login related API, which is a timestamp indicating the expiration time of this session ticket (the entity token should have the same expiration time as session ticket)

2 comments
10 |1200

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

Kim Strasser avatar image Kim Strasser commented ·

What do you mean with: the entity token should have the same expiration time as session ticket

Does the entity token not automatically have the same expiration time as the session ticket?

I can only find TokenExpiration in my code. I can not find an expiration time for the session ticket in my code.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Kim Strasser commented ·

I cannot find any documentation about it but according to my experience, they should have the same expiration time.

I don't think there is expiration time you can get for session ticket from APIs.

0 Likes 0 ·

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.