Skip to main content
The CommerceEngine SDK provides two sophisticated approaches to token management, designed to handle everything from simple prototyping to production-scale applications with automatic token refresh and persistence.

Overview

Manual Token Management

Simple approach where you handle token storage and refresh logic yourself

Automatic Token Management

Production-ready approach with automatic token refresh, persistence, and cleanup

Manual Token Management

Perfect for prototyping, testing, or when you need full control over token handling.

Basic Setup

Authentication Flow

1

Get anonymous token

2

Login with credentials

3

Handle token refresh manually

Manual Mode Methods

Automatic Token Management

Recommended for production applications. Handles token refresh, storage, and cleanup automatically.

Token Storage Options

Browser localStorage Storage

Features:
  • ✅ Persists across browser sessions
  • ✅ Automatic token refresh
  • ✅ Cross-tab synchronization
  • ✅ Works with SPA frameworks (React, Vue, Angular)
Features:
  • ✅ SSR/SSG compatible
  • ✅ Server and client access
  • ✅ Cross-subdomain sharing
  • ✅ Automatic expiry handling

Memory Storage (Server-Side)

Features:
  • ✅ Fast in-memory access
  • ✅ No persistence overhead
  • ✅ Perfect for server environments
  • ✅ Automatic cleanup on process end

Custom Token Storage

Implement the TokenStorage interface for custom storage solutions:

Authentication Patterns

Anonymous to Authenticated Flow

OTP Authentication Flow

Multi-Environment Token Management

Token Lifecycle Events

Handling Token Updates

JWT Utilities

The SDK provides utilities to extract information from tokens:

Best Practices

✅ Production Recommendations

Always use automatic token management in production with appropriate storage for your environment.

Security Best Practices

  1. Use secure cookies in production:
  1. Environment-specific configuration:
  1. Handle token events properly:

Framework Integration

For framework-specific token management patterns and implementation details, see our dedicated integration guides:

React Integration

Context providers, hooks, and component patterns for React SPAs

Next.js Integration

SSR, Server Actions, and automatic cookie-backed token management

TanStack Start Integration

Server functions, route loaders, and pre-rendering

Astro Integration

SSR pages, API routes, middleware, and static prerendering

SvelteKit Integration

Server load functions, form actions, hooks, and universal loads

Node.js Integration

Server-side patterns and background job authentication
Token Management Summary: The SDK’s automatic token management handles all the complexity of authentication, token refresh, and storage, allowing you to focus on building your application features while ensuring a seamless user experience.