Client code integration
Client-side usage of tokens
Niantic Spatial Access tokens expire after a set period. On login, the client should verify the token’s validity using the expiration timestamp provided by the backend. If the token is missing or expired, request a new one from your backend. Access tokens come from your backend in production; the sample app handles them automatically during testing or proof of concept. Never refresh tokens directly in the client.
This guide covers client-side token usage only. Backend token issuance is explained in Backend integration. For testing or proof of concept, see Access.
Proof of concept: Try out the NSDK with your own application
You can test token handling by copying the sample login flow into a Swift, Kotlin, or Unity app to try NSDK features without a backend. For detailed instructions, see Access.
Implementation details vary by platform. In Swift and Kotlin, copy the code from the Auth folder in the sample app. Key files include:
| File Name | Description |
|---|---|
| LoginManager | Opens the login page and handles the response. |
| UserSessionManager | Maintains the user session locally on the device while the app is running. |
RequestRuntimeRefreshToken | Exchanges the user session token for a refresh token, which is then used to request a short-lived access token inside the NSDK. |
Working with multiple users
For apps with multiple users, your backend must issue and manage tokens for each logged-in user. See Backend integration for server-side setup.
On the client side:
- Receive a short-lived access token from the backend on login.
- Provide the access token into the NSDK.
- Periodically check to see if the token is about to expire, at least once per minute.
- Request a new token when needed.
Sample code
The following examples show how to:
- Set access tokens in your NSDK session.
- Periodically check if a token has expired or is about to expire.
- Clear tokens on logout.
Set the access token when initializing the NSDK session as follows:
using NianticSpatial.NSDK.AR.Loader;
NsdkSettingsHelper.ActiveSettings.AccessToken = accessToken
Check if the access token has expired or will expire soon. Use AuthClient.GetAccessAuthInfo to obtain an AuthInfo object containing the expiration time in seconds since the Unix epoch. Compare it to the current time to determine if a new token is needed as follows:
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;
}
Clear the access token to prevent further NSDK access until a new token is obtained as follows:
NsdkSettingsHelper.ActiveSettings.AccessToken = string.Empty