Skip to content

Authentication

Templatical Cloud uses JWT-based authentication. The SDK manages tokens automatically — you provide a server-side endpoint that issues tokens, and the AuthManager handles refresh, retries, and authenticated requests.

Auth Flow

  1. Your frontend calls initCloud() with an auth.url
  2. The SDK requests a token from your server endpoint
  3. Your server authenticates the user and calls the Templatical Cloud API to get a JWT
  4. The SDK uses the JWT for all subsequent API and WebSocket requests
  5. When the token expires, the SDK refreshes it automatically

Configuration

Basic Setup

js
const editor = await initCloud({
  container: '#editor',
  auth: {
    url: '/api/templatical/token',
  },
});

Full Options

js
const editor = await initCloud({
  container: '#editor',
  auth: {
    url: '/api/templatical/token',
    baseUrl: 'https://templatical.com',        // Default
    requestOptions: {
      method: 'POST',                         // Default: POST
      headers: {
        'X-Custom-Header': 'value',
      },
      body: {
        tenant_id: 'tenant-123',
      },
      credentials: 'same-origin',
    },
  },
});

Auth Options Reference

OptionTypeDefaultDescription
urlstringRequired. Your token endpoint URL
baseUrlstringhttps://templatical.comTemplatical Cloud API base URL
requestOptions.method'GET' | 'POST''POST'HTTP method for token requests
requestOptions.headersRecord<string, string>{}Additional headers sent with token requests
requestOptions.bodyRecord<string, unknown>{}Body payload for POST token requests
requestOptions.credentialsRequestCredentials'include'Fetch credentials mode (same-origin, include)

Token Response Format

Your token endpoint must return a JSON response with this structure:

json
{
  "token": "eyJhbGciOiJSUzI1NiIs...",
  "expiresAt": 1720000000,
  "projectId": "proj_abc123",
  "tenant": "acme-corp",
  "testEmail": {
    "allowedEmails": ["team@example.com"],
    "signature": "hmac-signature-here"
  },
  "user": {
    "id": "user_123",
    "name": "Jane Smith",
    "signature": "hmac-signature-here"
  }
}
FieldTypeDescription
tokenstringRequired. The JWT access token
expiresAtnumberRequired. Unix timestamp when the token expires
projectIdstringRequired. The project ID
tenantstringRequired. The tenant slug
userobjectOptional. Used for collaboration presence and comment attribution
user.idstringUser identifier
user.namestringDisplay name shown in collaboration UI
user.signaturestringHMAC signature for user verification
testEmailobjectOptional. Configuration for the test email feature
testEmail.allowedEmailsstring[]Email addresses allowed to receive test emails
testEmail.signaturestringHMAC signature for test email verification

Direct Authentication

For server-side or testing scenarios, you can authenticate directly with API credentials instead of going through a token endpoint:

js
import { createSdkAuthManager } from '@templatical/core/cloud';

const auth = createSdkAuthManager(
  {
    mode: 'direct',
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    tenant: 'tenant-slug',
  },
  (error) => console.error('Auth error:', error), // optional onError callback
);

WARNING

Never expose clientSecret in client-side code. Direct authentication is intended for server-side usage, scripts, and testing only.

Error Handling

Authentication errors are surfaced through the onError callback:

js
const editor = await initCloud({
  container: '#editor',
  auth: { url: '/api/templatical/token' },
  onError: (error) => {
    if (error.message.includes('401') || error.message.includes('auth')) {
      // Redirect to login or show re-auth prompt
      window.location.href = '/login';
    }
  },
});

Security Best Practices

  • Never expose API credentials in client-side code. Always proxy through your server.
  • Validate the user session in your token endpoint before issuing a Templatical token.
  • Scope tokens to tenants. Each token should be associated with a specific tenant to enforce data isolation.
  • Use credentials: 'same-origin' to ensure cookies are sent with token requests when your auth relies on session cookies.