Issue new token
Unreal Engine

Issue External Token from Unreal Engine

To issue an external token from Unreal Engine using ChafKit, follow the steps below:

  1. Import the ChafKitAuth module by adding the following line at the top of your code file:

    #include "ChafKit/Public/ChafKitAuth.h"
  2. After initializing ChafKit, run the following code to issue the external token:

    auto ExternalTokenHandle = ChafKitAuth::IssueExternalToken();
    ExternalTokenHandle->BindLambda(
        [](ChafKitCore::ExternalTokenIssued result)
        {
            if (result.error == nullptr)
            {
                UE_LOG(LogTemp, Log, TEXT("%s"), ANSI_TO_TCHAR(result.token));
                GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, TEXT("Token is issued. Check Output Log."));
            }
            else
            {
                GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, result.error);
            }
        });

    In this code, the ChafKitAuth::IssueExternalToken() function is called to issue the external token.

Here is the full code within the Init function of your custom GameInstance class:

#include "MyGameInstance.h"
#include "Engine/Engine.h"
#include "ChafKit/Public/ChafKitSDK.h"
#include "ChafKit/Public/ChafKitAuth.h"
 
void UMyGameInstance::Init()
{
    Super::Init();
 
    auto CallbackHandle = ChafKitSDK::Initialize(TEXT("YOUR-GAME-ID"));
 
    CallbackHandle->BindLambda(
        [](ChafKitCore::ChafKitInitialized result)
        {
            if (result.error == nullptr)
            {
                auto ExternalTokenHandle = ChafKitAuth::IssueExternalToken();
                ExternalTokenHandle->BindLambda(
                    [](ChafKitCore::ExternalTokenIssued result)
                    {
                        if (result.error == nullptr)
                        {
                            UE_LOG(LogTemp, Log, TEXT("%s"), ANSI_TO_TCHAR(result.token));
                            GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, TEXT("Token is issued. Check Output Log."));
                        }
                        else
                        {
                            GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, result.error);
                        }
                    });
            }
            else
            {
                GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, result.error);
            }
        }
    );
}

Recompile and run your game. You will see the text "Token is issued. Check Output Log." displayed on the top left corner of the screen. Open the Output Log to view the issued token.

Now, let's proceed to the next section, Decode and Validate Token to make use of the issued token.