Login for Cocos

With the now.gg Login Module for Cocos, you can implement now.gg login within your app/game on the Cocos platform.

To integrate now.gg Login:

  1. Download and Import the now.gg Login module for cocos.
  2. Add required dependencies.
  3. Implement now.gg Login plugin to your project.

Note: Your project’s minimum Android SDK version must not be lower than version 19.

Download and Import the Module

The now.gg Login module for Cocos is included within the download package (nowgg_sdk_cocos-1.2.6.2001.zip).

To add now.gg Login within your cocos project:

  1. Download the package containing the latest version of Login module for Cocos.
  2. After downloading the package, import the Login module to your project. To do this:
    • Copy the nowggsdk directory from the download package to your proj.android > app > jni directory, as shown below.
      cocos_nowgg
    • Copy the libs directory from the download package to your proj.android > app directory, as shown below.
      cocos_nowgg

Add Dependencies

Once the Login module has been imported, you should now add the required dependencies to your project.

  1. Locate the CMakeLists.txt file, present in the root directory of your project.
    • Add the following code to the if(ANDROID) section of your CMakeLists.txt file
      list(APPEND GAME_HEADER
            proj.android/app/jni/nowggsdk/include/nowgg_sdk.h
            )
      
    • Next, add the following code to the if(ANDROID) section of your CMakeLists.txt file to add nowggsdk to your project.
      add_library(nowggsdk STATIC IMPORTED)
       set_target_properties(nowggsdk PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/jni/nowggsdk/${ANDROID_ABI}/libnowggsdk.a)
       target_link_libraries(${APP_NAME} nowggsdk)
    • Lastly, add the following line to your gradle.propertiesfile located in proj.android directory:
      android.useAndroidX=true
      
  2. Locate HelloWorldScene.h class by navigating to cocosSDKsample > classes directory, within the download package.
    • Include now.gg_sdk.h
      #include "../proj.android/app/jni/nowggsdk/include/nowgg_sdk.h" 
      
    • Inherit public nowgg::NowGGSDKListener
      class HelloWorld : public cocos2d::Scene, public nowgg::NowGGSDKListener
      
  3. Now, add the following virtual functions in the HelloWorldScene.h class:
    private:
         virtual void onLoginSuccess(string email, string token, string hostUrl) override;
         virtual void onLoginFailed(int code, string errorMsg) override;
    

    Note: Add the definitions of the virtual functions mentioned above, in HelloWorldScene.cpp.

  4. Add the following dependencies to your build.gradle file located in proj.android > app directory.
    dependencies {
         implementation (files("libs/billingclient.aar"))
         implementation (files("libs/nowggsdk.aar"))
         implementation 'androidx.appcompat:appcompat:1.4.1'
         api 'com.google.code.gson:gson:2.8.6'
         implementation 'com.squareup.retrofit2:retrofit:2.9.0'
         implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
         implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
         implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
     }
    

Implement now.gg Login

Once the Login module has been imported and dependencies have been added, you should now implement now.gg Login to your app/game.

To initiate now.gg Login, you can use either of the following two flows:

1. Basic Login Flow

With this flow, you can implement now.gg login and fetch the user’s basic profile information.

To start the flow, call the login function along with your client_id as illustrated below:

NowGGSDK::login(<client id from nowgg Studio>);

Note: Responses related to the now.gg Login can be received using OnLoginSuccess and OnLoginFailed callback functions, which you have previously added to HelloWorldScene.h.

Login Success

After the user successfully signs in, you will get the requested token and user’s email in the OnLoginSuccess callback function.

void HelloWorld::onLoginSuccess(string email, string token, string hostUrl) {
     __android_log_print(ANDROID_LOG_DEBUG, "TAG", "HelloWorld::onLoginSuccess called : %s : %s \n", email.c_str(), token.c_str());
      VerifyToken(token, "<client-id from nowgg Studio>");
 }

2. Advanced Login Flow

With this flow, you can implement now.gg login and fetch detailed user and session information.

This is a more secure flow that uses an authorization code (code) to generate the token and refresh_token.

Call the login function of NowGGSDK class using your client_id along with code as an additional parameter, as illustrated below:

NowGGSDK::login(<client id from NowGG Studio>, “code”);
Login Success

After the user successfully signs in, you will get the requested authorization code in the token parameter along with the user’s email in OnLoginSuccess callback function as illustrated below:

void HelloWorld::onLoginSuccess(string email, string token, string hostUrl) {
    __android_log_print(ANDROID_LOG_DEBUG, "TAG", "HelloWorld::onLoginSuccess called : %s : %s \n", email.c_str(), token.c_str());
    }

Login failure

This section lists the scenario wherein the login fails. Applicable for both the basic login flow and the advanced login flow.

If the login fails, you will receive an error code and an error message in the OnLoginFailed callback function. You can investigate this error further based on the error message received.

No nowGG account present

If the login fails with an error “No nowGG account present”, you may call the addNowGGAccount function of NowGGSDK class to add an account at runtime, as illustrated below.

void HelloWorld::onLoginFailed(int code, string errorMsg) {
     __android_log_print(ANDROID_LOG_DEBUG, "TAG", "HelloWorld::onLoginFailed called : %d : %s \n", code, errorMsg.c_str());
     NowGGSDK::addNowGGAccount();
 }

Using Profile Information | Basic Login Flow

After a successful sign-in, you now have the token. You can use this token to fetch User Profile Information.

We recommend using a backend server to get the User Profile of a signed-in user. However, if your app does not have a backend server, you can use the following section to retrieve user profile information.

Important Information

  • Do not use a user’s email address or user ID to communicate the currently signed-in user to your app’s backend server.
  • Instead, send the user’s ID token to your backend server and validate the token on the server.

Retrieve profile information

You can refer to our sample implementation to get the profile information for a logged-in user using an ‘on-device’ API call.

For example, to retrieve the User Profile Information, including the userId of a signed-in user, you may call the VerifyToken function. You will receive the decoded data of the user as a response.

The following code illustrates this implementation:

void HelloWorld::VerifyToken(string token, string client_id) {
     __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken called : %s \n", token.c_str());

     cocos2d::network::HttpRequest * request = new cocos2d::network::HttpRequest();
     request - > setUrl("https://now.gg/accounts/oauth2/v1/verify-token");
     std::vector < std::string > headers;
     headers.push_back("Content-Type:application/x-www-form-urlencoded");
     request - > setHeaders(headers);
     request - > setRequestType(cocos2d::network::HttpRequest::Type::POST);
     request - > setResponseCallback([ & , this](network::HttpClient * sender, network::HttpResponse * response) {
         if (!response - > isSucceed()) {
             __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken failed \n");
             return;
         }
         __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken success \n");
         std::vector < char > * buffer = response - > getResponseData();

         char * json = (char * ) malloc(buffer - > size() + 1);
         std::string s2(buffer - > begin(), buffer - > end());
         strcpy(json, s2.c_str());
         __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken response: %s \n", json);
     });

     string data = "token_type=id_token&token=" + token + "&client_id=<your client-id>";
     __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken data:%s \n", data.c_str());
     request - > setRequestData(data.c_str(), strlen(data.c_str()));
     cocos2d::network::HttpClient::getInstance() - > send(request);
     request - > release();
 }

Once you have the user details, you should save that information in your app’s database and use it to skip the login process the next time user launches your app.

Note:

  • It is recommended that you don’t use the email address to identify a user. Instead, use the userId.
  • If your app has a backend server, you can call the verify token API and get the userId as a response with decoded user data.

Get User and Session Information | Advanced Login Flow

With the advanced login flow:

  • Your app gets authorized on the client side by sending an authorization code (code) to your app backend server.
  • Your app backend server then exchanges the authorization code to acquire its own token and refresh_token from now.gg servers.
    • This enables your app backend server to make API calls to the now.gg servers while the user is offline.

Note:

  • This more secure login authentication flow is only applicable for apps with a backend server.
  • You can continue with the basic login flow if your app does not have a backend server.

1. Generate Token and Refresh Token

Once you have the authorization code (code), send it to your app backend server and use the Generate Tokens API to exchange this code with now.gg servers for a token and refresh_token.

  • Use the token to call now.gg APIs (User Info and Session Info).
  • Store the refresh_token to acquire a new token when the token expires.

2. Get User and Session Information using a token

Now that you have the token, you can use it to fetch the user and the session information.

  • To fetch user information, you can use the UserInfo API.
  • To get the session information, you can use the SessionInfo API.

3. Refresh the token

If your token has expired, you can generate it with your refresh_token using the Generate Tokens API.

Sample Project

We have also included a Sample project in the download package. You can refer to this project to understand the now.gg login flow.

The sample project is included in the cocosSDKSample directory.

×
Text copied to clipboard
Link copied to clipbord
Questions? Please reach out to us at dev-support@now.gg