Token Setup
Configure authentication tokens for NativeFrame frontend components
Authentication tokens are required for all NativeFrame operations. Tokens identify users, define permissions, and secure your video streams. This guide explains how to obtain tokens from your backend and use them in your frontend application.
How Authentication Works
NativeFrame uses a token-based authentication system where:
- Your backend server requests tokens from the NativeFrame Auth Service
- Your frontend application receives these tokens from your backend
- The frontend passes tokens to NativeFrame components for authentication
Security
Never hardcode tokens in client-side code or expose your backend credentials to the browser. Always fetch tokens from your backend server.
Prerequisites
What you need from NativeFrame
Before implementing token authentication, ensure you have:
- Backend endpoint - Your NativeFrame API URL (e.g.,
https://your-subdomain.nativeframe.com) - Token endpoint on your backend that fetches tokens from NativeFrame
- Stream key for identifying your streams
Your backend team should have access to credentials for creating tokens via the NativeFrame Auth Service.
Token Types and Scopes
Tokens define what actions a user can perform. Choose the appropriate scope based on your use case.
Available Scopes
| Scope | Description | Permissions |
|---|---|---|
broadcaster | Create public broadcasts | Create calls, produce/consume video and audio |
viewer | Watch public streams | Consume video and audio only |
private-broadcaster | Create private broadcasts (1-to-many) | Create calls, produce/consume (max 1 producer) |
private-viewer | Participate in private calls | Produce and consume in private calls |
conference-owner | Host group video calls | Create/manage conferences (max 10 producers) |
conference-participant | Join group video calls | Participate in conferences |
admin | Moderate calls | Consume and manage peer permissions |
Choosing the Right Scope
| Use Case | Recommended Scope |
|---|---|
| Live streaming to many viewers | broadcaster (host) + viewer (audience) |
| One-on-one video calls | private-broadcaster + private-viewer |
| Group video meetings | conference-owner (host) + conference-participant (attendees) |
| Private streaming with interaction | private-broadcaster + private-viewer |
Permission Details
| Permission | broadcaster | viewer | private-viewer | conference-owner | conference-participant |
|---|---|---|---|---|---|
| Create calls | Yes | No | No | Yes | No |
| Produce video | Yes | No | Yes | Yes | Yes |
| Produce audio | Yes | No | Yes | Yes | Yes |
| Consume video | Yes | Yes | Yes | Yes | Yes |
| Consume audio | Yes | Yes | Yes | Yes | Yes |
Scope Mismatch
Using a viewer token for broadcasting will fail with a permission error. Ensure your token scope matches the intended operation.
Using Tokens in Your Application
- React
- Vanilla JavaScript
Creating an AuthClient (React)
The useAuthClient hook creates an authentication client that manages your token:
- useAuthClient - Creates an
AuthClientinstance from your token - usePreviewPlayer - Sets up camera/microphone and preview
- context providers - Share auth and media state with child components
Basic Usage
Passing AuthClient to Components
The authClient is passed via the auth property in call options:
Viewer Example
For viewers joining a call:
Token Refresh
Tokens expire after a set period (typically 5-15 minutes). To maintain uninterrupted sessions, implement token refresh.
Token Expiration
Tokens typically expire after 5-15 minutes. Implement token refresh to maintain uninterrupted broadcasting or viewing sessions.
- React
- Vanilla JavaScript
Security Best Practices
Token Storage
| Method | Security Level | Recommended For |
|---|---|---|
| Memory only | High | Short-lived sessions, highest security |
sessionStorage | Medium | Single-tab applications |
HttpOnly cookies | High | Server-rendered applications |
Avoid localStorage
Storing tokens in localStorage exposes them to XSS attacks. Prefer memory-only storage or sessionStorage for client-side applications.
Backend Token Endpoint
Your backend should:
- Authenticate users before issuing tokens
- Request appropriate scopes based on user role
- Set reasonable TTLs (5-15 minutes)
- Never expose credentials to the frontend
What NOT to Do
Troubleshooting
Common Errors
"Authentication failed" / 401 Error
Cause: Token is invalid, expired, or malformed.
Solutions:
- Verify your backend is correctly fetching tokens from the Auth Service
- Check that the token hasn't expired
- Ensure the token is being passed correctly to the AuthClient
"User does not have permission" / 403 Error
Cause: Token scope doesn't match the operation.
Solutions:
- Verify the token has the correct scope for the operation
- Broadcaster operations require
broadcasterorprivate-broadcasterscope - Viewer operations require
viewerorprivate-viewerscope
Token expires during session
Cause: Token TTL exceeded without refresh.
Solutions:
- Implement token refresh callback
- Increase TTL when creating tokens (up to your security requirements)
- Handle the error gracefully and prompt user to reconnect
Debugging Tips
- Inspect token contents: Use jwt.io to decode and inspect JWT tokens
- Check network requests: Verify tokens are being sent in API calls
- Verify scope: Ensure the token scope matches your intended operation
- Check expiration: Verify the token hasn't expired (
expclaim in JWT)
API Reference
useAuthClient (React)
| Parameter | Type | Description |
|---|---|---|
token | string | null | JWT or AccessToken string |
refresh | () => Promise<string> | Optional callback to fetch new token |
Returns: AuthClient instance for use in call options.
BaseAuthClient (Vanilla JS)
| Parameter | Type | Description |
|---|---|---|
token | string | null | JWT or AccessToken string |
refresh | () => Promise<string> | Optional callback to fetch new token |
Next Steps
Now that you understand token authentication:
- Broadcasting Guide - Set up your first broadcast
- Viewing Guide - Build a video player for viewers
- Group Calls - Implement multi-participant video calls
- Private Calls - Build one-on-one video experiences
- Troubleshooting - Solutions to common issues