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:
Note: Your project’s minimum Android SDK version must not be lower than version 19.
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:
Once the Login module has been imported, you should now add the required dependencies to your project.
if(ANDROID)
section of your CMakeLists.txt file
list(APPEND GAME_HEADER proj.android/app/jni/nowggsdk/include/nowgg_sdk.h )
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)
gradle.properties
file located in proj.android directory:
android.useAndroidX=true
HelloWorldScene.h
class by navigating to cocosSDKsample > classes directory, within the download package.
#include "../proj.android/app/jni/nowggsdk/include/nowgg_sdk.h"
class HelloWorld : public cocos2d::Scene, public nowgg::NowGGSDKListener
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
.
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' }
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:
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
.
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>"); }
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”);
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()); }
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(); }
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.
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:
userId
.With the advanced login flow:
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
.
token
to call now.gg APIs (User Info and Session Info).refresh_token
to acquire a new token
when the token expires.Now that you have the token, you can use it to fetch the user and the session information.
If your token
has expired, you can generate it with your refresh_token
using the Generate Tokens API.
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.
User Account Service
User Account Service
Document Rev. 1.0