Login Plugin for Unity

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

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

  1. Download and Import the now.gg login unity plugin.
  2. Add required dependencies.
  3. Implement now.gg login to your project.

Download and Import the Plugin

The Unity plugin for now.gg Login is included as a Unity package file NowGGSdk-1.2.3.1003.unitypackage.

To add the plugin within your Unity project:

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

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

Note: Please do not modify the NowGGSdk folder, as that 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 plugin 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.

Start the Login flow

To start the flow, call the login function of nowGGSdkManager using your client_id. Responses related to the Login SDK can be received using OnLoginSuccess and OnLoginFailed callback functions, as illustrated below:

private string CLIENT_ID = "your_client_id";

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

    NowGGSdkManager.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}");
 }
Login Success

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

Login failure

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 NowGGSdkManager 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)
         NowGGSdkManager.Instance.AddNowGGAccount();
 }

Using Profile Information

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 SDK 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 LoginTokenVefication : 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 next time the 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.

Need Help?

Feel free to reach out to us at dev-support@now.gg, and we will be happy to assist you.

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