Using Authentication Tokens
Authentication tokens are essential when your custom widgets need to make API requests to secure endpoints or services that require user authentication. The Widget Builder provides access to Staffbase authentication tokens that can be used in your API requests.
Available Authentication Tokens
The Widget Builder provides two types of Staffbase authentication tokens that you can use in your API requests:
Staffbase ID Token
The ID Token is a JSON Web Token (JWT) that contains information about the authenticated user. It provides identity information that can be used to verify who the user is.
This is an example payload of the staffbase id token:
{
"at_hash": "SfaFUUXInY8qOcHj-iMrtg",
"aud": ["a5ba7f74-bedf-4d09-a4b0-ab84faf3ca7b"],
"auth_time": 1758618864,
"display_name": "Max Mustermann",
"exp": 1758723348,
"iat": 1758719748,
"iss": "https://id-us1.staffbase.com/",
"jti": "9df571d8-b9ab-4c90-9e7b-12f439139d88",
"public_email": "max.mustermann@jasp.eu",
"rat": 1758618861,
"sid": "058855b2-ef8d-4eb4-9ccd-bb3f022e4a9c",
"sub": "640b43f6fd99249009a41ba4"
}
How to access the ID Token
In your Handlebars templates, you can access the ID Token using the staffbaseIdToken helper:
When to use the ID Token
Use the ID Token when:
- You need to authenticate with services that validate Staffbase users
- You need to access user identity information
- You're making API calls to services that accept Staffbase ID tokens for authentication
- You need to verify the user's identity on the server side
Example: Using the ID Token in an API request
Staffbase Access Token
The Access Token is used to grant access to specific resources or services. Unlike the ID Token, which is primarily for identification, the Access Token is for authorization purposes.
How to access the Access Token
In your Handlebars templates, you can access the Access Token using the staffbaseAccessToken helper:
This helper returns the token as a string that typically looks like:
ory_ae4b742ea3...
When to use the Access Token
Use the Access Token when:
- You need to make requests to Staffbase APIs or services
- You need to authenticate with third-party services that have been integrated with Staffbase
- You need to authorize specific actions rather than just identify the user
- You're making API calls that require permissions to access specific resources
Example: Using the Access Token in an API request
Third Party Connections Tokens
You can add the access token from the Microsoft, Google Workspace, ServiceNow, Atlassian or Box connections to your request. Please contact our support for instructions.
Adding Authentication to API Requests in Widget Builder
When creating API requests in the Widget Builder, you can easily add authentication using the Staffbase tokens:
- In the Widget Builder, navigate to the "API" tab
- Create or edit an API request
- In the Headers section, add an Authorization header:
- For ID Token:
Authorization: Bearer {{staffbaseIdToken}} - For Access Token:
Authorization: Bearer {{staffbaseAccessToken}}
- For ID Token:
Security Considerations
When working with authentication tokens, keep these security best practices in mind:
- Don't store tokens in localStorage or sessionStorage for extended periods
- Use HTTPS for all API requests to ensure tokens are encrypted during transmission
Troubleshooting
If you encounter issues with authentication tokens:
- Verify token format: Ensure you're using the correct format for the Authorization header
- Check CORS settings: Cross-Origin Resource Sharing restrictions might block your requests
- Inspect network requests: Use browser developer tools to see the actual request and response
- Verify API permissions: Ensure the API endpoint accepts the type of token you're using
Example Implementation
Here's a complete example of a widget that uses both types of tokens:
By understanding the differences between ID and Access tokens and when to use each, you can create secure and powerful custom widgets that integrate with various services while maintaining proper authentication.