Skip to main content

Short Lived Token Authentication

Nudge supports two types of authentication methods for better security: short-lived token authentication and static public key authentication. This document will explain how to use the short-lived token authentication method.

note

This feature is not enabled by default. Please reach out to the Nudge team to enable it.

Implementation flow overview

  1. The user is authenticated by the frontend application.
  2. The frontend will expose an API that returns a short-lived token to the client application based on the user’s authorization.
    • Your backend will call Nudge's getToken API to fetch this token.
  3. Nudge SDKs provide a function that allows you to register your token API. The following sections explain how to use this function.

Get Token API

This API helps retrieve a short-lived token, which is valid for 60 minutes by default. It should be called from your backend, not directly from the frontend.

Endpoint URL: https://main-api.nudgenow.com/api/clients/jwt/token

Authorization

KeyValue
apiKeyYOUR_PRIVATE_API_KEY
note

You'll need to generate a PRIVATE_API_KEY from the Settings section in your Nudge dashboard.

Query Parameters

expiryMins (integer): Token expiry in minutes (default: 60 mins).

Response Format

{
"token": "eyJhbGciOiJFZERTQSIsImtpZCI6IjBjNDk0NDRmLWRjMTAtNDMxYS04NDc0LWIyZTMwMjAyOTNlNiJ9.eyJpYXQiOjE3MzIwMjAyNDMsImV4cCI6MTczMjAyMjA0M30.gB6rKYtkwMe2itS5HWq4lnJyiDA55BIMkqx0cdvv5Z0FQAiYS1kcVkn_i_jgUL8r4WW-4185_ZkEbhK_aALMAQ"
}

Response Parameters

token (string): The short-lived token, which you will use in the refreshToken method on the frontend.


Frontend Setup

To handle token expiration and refreshing, you need to register a refreshToken method in your frontend. This method will be responsible for fetching a new token when needed.

Make sure this method is fully functional immediately after the user authenticates, as the token is necessary for all Nudge API calls.

note

Ensure that your refreshToken method:

  1. Returns a valid short-lived token or null.
  2. Retrieves the token from your backend when the SDK calls it.

This ensures that the SDK can call your refreshToken method whenever the token expires, maintaining a valid token for all Nudge API calls.

Example Implementations:

import axios from "axios";

async function refreshToken(): Promise<string | null> {
const res = await axios.get("your_backend_api_end_point", {
headers: {
Authorization: your_token,
},
});

if (res.statusCode === 200) return res.data.token;
return null;
}

Registering your refreshToken method in Nudge SDKs

Once you've set up the refreshToken function, you need to register it with the Nudge SDK to handle token expiration.

Nudge.registerRefreshToken(refreshToken);

Key Points

  1. Use the getToken API from your backend to retrieve short-lived tokens.
  2. Register the refreshToken method in the Nudge SDK to manage token expiration.
  3. Ensure your refreshToken method is operational immediately after user authentication.