JavaScript library for managing WPGraphQL and WooGraphQL authentication and session token lifecycles.
Managing session state in headless WooCommerce applications involves juggling multiple tokens with different purposes and expiration times. @woographql/session-utils simplifies this complexity by providing a unified interface for token lifecycle management.
WooGraphQL applications need to manage several tokens:
| Token | Purpose | Default Expiration |
|---|---|---|
| Session Token | WooCommerce cart session | 14 days |
authToken | WordPress user authentication | 15 minutes |
refreshToken | Renew expired authTokens | ~1 year |
clientSessionId | Device identification for session handoff | Custom |
Each token has different:
WPGraphQL for WooCommerce supports two different session token systems:
| Token Type | Created By | GraphQL Field | HTTP Header |
|---|---|---|---|
| Legacy | QL_Session_Handler | Customer.sessionToken | woocommerce-session |
| Store-API | CartTokenUtils | Customer.cartToken | Cart-Token |
Which should you use?
cartToken) - recommended for all projects, especially if using NextPress to render WordPress/Gutenberg content containing WooCommerce Blocks. The legacy token is not fully supported by WooCommerce Blocks.sessionToken) only if you already have an existing application using it without NextPress, and you don't have the time or resources to migrate.See Understanding Session Tokens for detailed information about each token type.
The TokenManager class handles all of this automatically:
import { TokenManager, SessionBehavior } from '@woographql/session-utils'; const tokenManager = new TokenManager({ ID: 'my-store', behavior: [SessionBehavior.withAuth, SessionBehavior.withClientSession], startSession: async () => { /* fetch initial session */ }, updateSession: async () => { /* update session */ }, refreshAuthToken: async () => { /* refresh auth token */ }, });
Configure how your application handles different user types:
| Behavior | Description |
|---|---|
withAuth | Support authenticated WordPress users |
withClientSession | Enable device-specific client session IDs |
guestOnly | Only support guest shopping sessions |
authOnly | Require authentication for all sessions |
@woographql/session-utils is designed to work with:
Get started by following the Getting Started guide.