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:
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:
nowgg-payments-login.unitypackage
that you previously downloaded.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.
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:
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.
The now.gg Sign-in button assets are included within now.gg SDK package under the Assets/Login
directory.
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:
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; } // Call this method when the user clicks on the sign-in button. public void OnLoginButtonClick() { 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 Module can be received using OnLoginSuccess
and OnLoginFailed
callback functions.
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.
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; } // Call this method when the user clicks on the sign-in button. public void OnLoginButtonClick() { 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 Module can be received using OnLoginSuccess
and OnLoginFailed
callback functions.
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.
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.
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(); }
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.
If your app does not have a backend server, you can use the following section to retrieve user 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; }
You can also find this class within the 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:
userDataVerified.userId
.With the Advanced Login flow:
token
and refresh_token
from now.gg servers.
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.
Contact us at dev-support@now.gg, and we will be happy to assist you.
User Account Service
User Account Service
Document Rev. 1.0