Login Module for Unity

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

To implement login using the now.gg Login module for Unity:

  1. Download and Import the now.gg Login Unity module.
  2. Add required dependencies.
  3. Implement now.gg login to your project.

Download and Import the Module

The Unity module for now.gg Login is included as a Unity package file nowgg-payments-login.unitypackage.

Add the module to your Unity project:

  1. Download the package containing the latest version of now.gg Login module for Unity.
  2. After downloading the module, import it into your Unity project. To do this:
    • Click on Assets > Import Package > Custom Package, as shown below:
    • Select nowgg-payments-login.unitypackage that you previously downloaded.
    • Select all the listed files and click on Import.

Once all the module files have been imported, a folder named ‘NowGGSdk‘ will be added to your project. You can find this folder at the root of the Assets folder.

Note: Please do not modify the NowGGSdk folder, as it contains all the assets related to now.gg Login.

Add Required Dependencies

This operation will download and add all the required dependencies to the Assets/Plugins/Android directory of your project using the Unity External Dependency Manager.

To add the required dependencies, follow the steps below:

  • Click on Assets > External Dependency Manager > Android Resolver > Resolve.

Implement now.gg Login

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

Create a class that contains the now.gg Login implementation and attach it to a game object that you can use to start the login process.

To implement 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 of NowGGLoginSdkManager using your client_id as illustrated below:

private string CLIENT_ID = "your_client_id";

 public void Start()
 {
    NowGGLoginSdkManager.Instance.OnLoginSuccess += OnLoginSuccess;
    NowGGLoginSdkManager.Instance.OnLoginFailed += OnLoginFailed;

    NowGGLoginSdkManager.Instance.Login(CLIENT_ID);
 }

 private void OnLoginSuccess(string email, string token, string hostUrl)
 {
    Debug.Log($"Login success with email: {email} and token is: {token}");
 }

 private void OnLoginFailed(int errorCode, string errorMsg)
 {
    Debug.Log($"Login failed with error code: {errorCode} and message: {errorMsg}");
 }

Note: Responses related to the now.gg Login SDK can be received using OnLoginSuccess and OnLoginFailed callback functions.

Login Success

After the user successfully logs in, you will get the requested token in token parameter along with the user’s email in OnLoginSuccess callback function.

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 to generate token and refresh_token.

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

private string CLIENT_ID = "your_client_id";

 public void Start()
 {
    NowGGLoginSdkManager.Instance.OnLoginSuccess += OnLoginSuccess;
    NowGGLoginSdkManager.Instance.OnLoginFailed += OnLoginFailed;

    NowGGLoginSdkManager.Instance.Login(CLIENT_ID, "code");
 }

 private void OnLoginSuccess(string email, string token, string hostUrl)
 {
    Debug.Log($"Login success with email: {email} and authorization code is: {token}");
 }

 private void OnLoginFailed(int errorCode, string errorMsg)
 {
    Debug.Log($"Login failed with error code: {errorCode} and message: {errorMsg}");
 }

Note: Responses related to the now.gg Login SDK can be received using OnLoginSuccess and OnLoginFailed callback functions.

Login Success

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


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 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 the NowGGLoginSdkManager class to add an account at runtime, as illustrated below.

private void OnLoginFailed(int errorCode, string errorMsg)
 {
     statusText.text = "Login Failed: " + errorMsg;
     if (errorMsg.Equals("No nowgg account present") || errorCode == 1)
         NowGGLoginSdkManager.Instance.AddNowGGAccount();
 }

Using Profile Information | Basic Login Flow

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

We recommend that you use this token with your app’s backend server and fetch the User Profile of a signed-in user by referring to this section.

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.

If your app does not have a backend server, you can use the following section to retrieve user profile information.

Retrieve profile information

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

For example, to retrieve the User Profile Information, including the userId of a signed-in user, you may call the VerifyToken function of LoginTokenVerification class, and get the required information in the userDataVerified object of the TokenVerifyResponse callback.

The following code illustrates this implementation:

private void OnLoginSuccess(string email, string token, string hostUrl)
 {
     Debug.Log($"Login success with email: {email} and token is: {token}");

     // verify the token to get user details. We recommend verifying the token on your backend server for more security.
     StartCoroutine(LoginTokenVefication.VerifyToken(token, CLIENT_ID, TokenVerifyResponse));
 }

 private void TokenVerifyResponse(UserDataVerified userDataVerified)
 {
     Debug.Log($"User id: {userDataVerified.userId}");
     string userId = userDataVerified.userId;
     string name = userDataVerified.name;
     string email = userDataVerified.email;
     string picture = userDataVerified.picture;
 }
LoginTokenVerification Class:

You can also find this class within the SDK Demo project.

using System;
 using System.Collections;
 using UnityEngine;
 using UnityEngine.Networking;

 public class LoginTokenVerification : MonoBehaviour
 {
     public static IEnumerator VerifyToken(string token, string client_id, Action tokenVerifyResponse)
     {
         WWWForm form = new WWWForm();
         form.AddField("token_type", "id_token");
         form.AddField("token", token);
         form.AddField("client_id", client_id);

         UnityWebRequest www = UnityWebRequest.Post("https://now.gg/accounts/oauth2/v1/verify-token", form);
         yield return www.SendWebRequest();

         if (www.result != UnityWebRequest.Result.Success)
         {
             Debug.Log($"Error in token verification: {www.error}");
         }
         else
         {
             Debug.Log($"token verification response: {www.downloadHandler.text}");
             TokenVerification tokenVerification = JsonUtility.FromJson(www.downloadHandler.text);
             if (tokenVerifyResponse != null)
                 tokenVerifyResponse(tokenVerification.userData);
         }
     }
 }

 [System.Serializable]
 public class TokenVerification
 {
     public string success;
     public string code;
     [SerializeField] private UserDataVerified decodedData;
     public UserDataVerified userData { get { return decodedData; } }
     public string msg;
 }

 [System.Serializable]
 public class UserDataVerified
 {
     public string iss;
     public string sub;
     public string aud;
     public string exp;
     public string iat;
     public string auth_time;
     public string tokenId;
     public string sessionId;
     public string scope;
     public string email;
     public string name;
     public string picture;
     public string mobile;
     public string userId;
 }

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 does not have a backend server, you can get the userId on the client using userDataVerified.userId.
  • If your app has a backend server, you can call the verify token API and get the userId as a response with 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 now.gg servers while the user is offline.

Note:

  • This is a more secure login authentication flow and only applies to 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.

Need Help?

Contact us at dev-support@now.gg, and we will be happy to assist you.

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