Skip to main content

Auth and Your Client Code

Introduction

Niantic Spatial Access tokens expire after a set length of time. On login, the client code should check to see if the user has a token and it has not expired. If neither of these conditions are met, the client should request a new token.

Working with Multiple Users

If you have a backend server for handling multiple users, you will need to set things up so that each user is authorized for NSDK while they are logged in. See Auth Backend for information on the server side.

On the client side, you will need to do the following:

  1. Receive an access token from the server on login
  2. Plug the access token into the Niantic SDK
  3. Periodically check to see if the token is about to expire (or has expired)
  4. Request a new token if needed

Sample Code

Access tokens can be set with the following helper:

using NianticSpatial.NSDK.AR.Loader;

NsdkSettingsHelper.ActiveSettings.AccessToken = accessToken

Periodic checks for expired tokens can be done by checking the time left on the access token using the NsdkSession object's getAccessAuthInfo function. This function returns an AuthInfo object, which contains the expiration time of the access token. The expiration time is in seconds since the epoch. You can check if the token has expired by comparing the current time to the expiration time. For example:

using NianticSpatial.NSDK.AR.Auth;

/// <summary>
/// Returns true if access has expired or is about to expire in under a minute.
/// Uses <see cref="AuthClient.GetAccessAuthInfo"/> to read the access token's expiration.
/// </summary>
/// <returns>true if access is expired or expires in under 60 seconds; false otherwise.</returns>
public static bool IsAccessExpiredOrExpiringSoon()
{
var authInfo = AuthClient.GetAccessAuthInfo();
var currentTimeSeconds = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var timeLeft = authInfo.ExpirationTime - currentTimeSeconds;
return timeLeft < ExpiringSoonThresholdSeconds;
}

On logout, the access token should be cleared:

NsdkSettingsHelper.ActiveSettings.AccessToken = string.Empty