Events
このコンテンツはまだ日本語訳がありません。
Overview
Section titled “Overview”The @authrim/core SDK provides a rich event system for observing authentication lifecycle events. Use events for logging, UI updates, analytics, and error monitoring.
Subscribing to Events
Section titled “Subscribing to Events”on() — Subscribe to an Event
Section titled “on() — Subscribe to an Event”const unsubscribe = client.on('token:refreshed', (event) => { console.log('Token refreshed at:', event.timestamp); console.log('Expires at:', event.expiresAt);});
// Later, unsubscribeunsubscribe();once() — Subscribe to a Single Occurrence
Section titled “once() — Subscribe to a Single Occurrence”client.once('auth:login:complete', (event) => { console.log('First login:', event.method);});off() — Unsubscribe
Section titled “off() — Unsubscribe”function handler(event) { console.log('Session started');}
client.on('session:started', handler);
// Laterclient.off('session:started', handler);Event Payload
Section titled “Event Payload”All events extend BaseEventPayload:
| Property | Type | Description |
|---|---|---|
timestamp | number | Event timestamp (epoch milliseconds) |
source | 'core' | 'web' | Which SDK layer emitted the event |
operationId | string | undefined | Correlation ID for related events |
Event Reference
Section titled “Event Reference”Authentication Events (auth:*)
Section titled “Authentication Events (auth:*)”| Event | Payload | Description |
|---|---|---|
auth:init | BaseEventPayload | Authentication flow initialized |
auth:redirecting | { url: string } | Redirecting to authorization server |
auth:callback | { code: string, state: string } | Callback received with authorization code |
auth:callback:processing | BaseEventPayload | Processing the callback (code exchange) |
auth:callback:complete | { success: boolean } | Callback processing completed |
auth:login:complete | { method: string, user?: UserInfo } | Login completed successfully |
auth:logout:complete | { method: string } | Logout completed |
auth:required | { reason: string } | Authentication is required |
auth:popup_blocked | BaseEventPayload | Popup was blocked by the browser |
auth:fallback | { from: string, to: 'redirect', reason: string } | Falling back to a different auth method |
Token Events (token:*)
Section titled “Token Events (token:*)”| Event | Payload | Description |
|---|---|---|
token:refreshing | { reason: string } | Token refresh started |
token:refreshed | { hasAccessToken, hasRefreshToken, hasIdToken, expiresAt } | Token refresh succeeded |
token:refresh:failed | { error: AuthrimError, willRetry: boolean, attempt: number } | Token refresh failed |
token:expiring | { expiresAt: number, expiresIn: number } | Token is about to expire |
token:expired | { expiredAt: number, hasRefreshToken: boolean } | Token has expired |
token:error | { error: AuthrimError, context: string } | Token operation error |
token:exchanged | { hasAccessToken, hasRefreshToken, issuedTokenType } | Token exchange completed |
Session Events (session:*)
Section titled “Session Events (session:*)”| Event | Payload | Description |
|---|---|---|
session:started | BaseEventPayload | Session started (user authenticated) |
session:ended | BaseEventPayload | Session ended (logout or expiration) |
session:changed | BaseEventPayload | Session state changed |
session:sync | BaseEventPayload | Session synchronized across tabs |
session:logout:broadcast | BaseEventPayload | Logout broadcast received from another tab |
Error Events (error:*)
Section titled “Error Events (error:*)”| Event | Payload | Description |
|---|---|---|
error:fatal | { error: AuthrimError, context: string } | Fatal error — recovery not possible |
error:recoverable | { error: AuthrimError, context: string } | Recoverable error — automatic retry may occur |
Warning Events (warning:*)
Section titled “Warning Events (warning:*)”| Event | Payload | Description |
|---|---|---|
warning:itp | { browser, impact, recommendation } | Browser tracking-prevention warning |
warning:storage_fallback | { requestedStorage, actualStorage, reason } | Storage fallback warning |
warning:private_mode | { browser, limitation } | Private browsing limitation warning |
Usage Examples
Section titled “Usage Examples”Logging
Section titled “Logging”// Log all authentication eventsclient.on('auth:login:complete', (e) => { console.log(`User logged in via ${e.method}`);});
client.on('auth:logout:complete', (e) => { console.log(`User logged out via ${e.method}`);});UI Updates
Section titled “UI Updates”// Update UI when session changesclient.on('session:started', () => { showUserDashboard();});
client.on('session:ended', () => { showLoginPage();});
// Show loading state during token refreshclient.on('token:refreshing', () => { showSpinner();});
client.on('token:refreshed', () => { hideSpinner();});Error Monitoring
Section titled “Error Monitoring”// Monitor token refresh failuresclient.on('token:refresh:failed', (event) => { if (!event.willRetry) { // Final failure — no more retries reportToErrorTracking({ code: event.error.code, message: event.error.message, attempt: event.attempt, }); redirectToLogin(); }});
// Monitor fatal errorsclient.on('error:fatal', (event) => { reportToErrorTracking({ code: event.error.code, message: event.error.message, context: event.context, });});Cross-Tab Logout
Section titled “Cross-Tab Logout”// Detect when another tab logs outclient.on('session:logout:broadcast', () => { // Clear UI state and redirect clearAppState(); window.location.href = '/login';});Token Expiration Warning
Section titled “Token Expiration Warning”client.on('token:expiring', (event) => { const minutesLeft = Math.floor(event.expiresIn / 60); showNotification(`Session expires in ${minutesLeft} minutes`);});Next Steps
Section titled “Next Steps”- Error Handling — Error classification and recovery
- Token Management — Token lifecycle management
- Session & Logout — Session state management