Skip to main content

Session management

There is a comprehensive approach to managing user session data, using browser storage mechanisms to persist user authentication states, preferences, and other session-specific information. It provides a unified interface to interact with both local storage and session storage, making it easy to store and retrieve session data.

Key components​

  • Session Interface (Session): Defines the contract for session management operations, including setting, getting, and removing items from storage, as well as managing user authentication results and session return URLs.
  • Default Implementation (DefaultSession): Offers a concrete implementation of the Session interface, encapsulating logic for prefixing storage keys, handling authentication results, and managing return URLs.

Usage examples​

Setting the storage key prefix​

To avoid key collisions in storage, it's recommended to set a unique prefix for your application's storage keys.

import { Sesssion } from '@genesislcap/foundation-comms';

export class MyExampleClass {
@Session session: Session;

constructor() {
this.session.setStorageKeyPrefix('myApp_');
}
}

Managing authentication data​

Store and manage authentication results to maintain user session state.

import { Session } from '@genesislcap/foundation-comms';

export class MyExampleClass {
@Session session: Session;

// Store authentication result
storeAuthResult() {
this.session.setAuthResult({
SESSION_AUTH_TOKEN: 'token123',
REFRESH_AUTH_TOKEN: 'refreshToken456',
USER_NAME: 'john.doe@example.com',
SESSION_ID: 'session789',
DETAILS: { MFA_CODE: 'mfa123' }
});
}

// Delete user session data
deleteUserSessionData() {
this.session.deleteUserFromSessionStorage();
}
}

This uses setSessionStorageItem to store each piece of data and removeSessionStorageItem for clean-up.

Capturing and managing return URLs​

Automatically capture and store the return URL for post-authentication redirection.

import { Session } from '@genesislcap/foundation-comms';

export class MyExampleClass {
@Session session: Session;

// Capture and store the return URL
captureAndStoreReturnUrl() {
this.session.captureReturnUrl();
}

// Redirect to the captured return URL
redirectToReturnUrl() {
window.location.href = this.session.returnUrl;
}
}

This internally uses setSessionStorageItem to store the return URL and initializes this.returnUrl by retrieving it with getSessionStorageItem.

Direct Storage Access: Setting, Getting, and Removing Items​

Setting items​

Store data in local or session storage directly. This demonstrates the versatility in storage options.

import { Session } from '@genesislcap/foundation-comms';

export class MyExampleClass {
@Session session: Session;

// Set in local storage
setTheme() {
this.session.setLocalStorageItem('theme', 'dark');
}

// Set in session storage
setSessionActive() {
this.session.setSessionStorageItem('sessionActive', true);
}

These methods internally call setItem, specifying the storage type.

Getting items​

Retrieve data from storage, showcasing both local and session storage retrieval.

import { Session } from '@genesislcap/foundation-comms';

export class MyExampleClass {
@Session session: Session;

// Get from local storage
getTheme() {
return this.session.getLocalStorageItem('theme');
}

// Get from session storage
getSessionActive() {
return this.session.getSessionStorageItem('sessionActive');
}
}

These methods internally use getItem, specifying the appropriate storage type.

Removing items​

Remove items from storage, illustrating how to clear specific data.

import { Session } from '@genesislcap/foundation-comms';

export class MyExampleClass {
@Session session: Session;

// Remove from local storage
removeTheme() {
this.session.removeLocalStorageItem('theme');
}

// Remove from session storage
removeSessionActive() {
this.session.removeSessionStorageItem('sessionActive');
}
}

These methods make internal calls to removeItem with the specified storage type.

Generic storage management​

For situations where the storage type might vary, or when creating more generic functions that can work with either storage type, setItem, getItem, and removeItem are used directly.

setItem​

import { Session } from '@genesislcap/foundation-comms';

export class MyExampleClass {
@Session session: Session;

// Set item in local storage
setLocalUserSettings() {
this.session.setItem('userSettings', JSON.stringify({ notifications: true }), 'local');
}

// Set item in session storage
setSessionUserSettings() {
this.session.setItem('userSettings', JSON.stringify({ notifications: true }), 'session');
}
}

getItem​

import { Session } from '@genesislcap/foundation-comms';

export class MyExampleClass {
@Session session: Session;

// Get item from local storage
getLocalUserSettings() {
return JSON.parse(this.session.getItem('userSettings', 'local'));
}

// Get item from session storage
getSessionUserSettings() {
return JSON.parse(this.session.getItem('userSettings', 'session'));
}
}

removeItem​

import { Session } from '@genesislcap/foundation-comms';

export class MyExampleClass {
@Session session: Session;

// Remove item from local storage
removeLocalUserSettings() {
this.session.removeItem('userSettings', 'local');
}

// Remove item from session storage
removeSessionUserSettings() {
this.session.removeItem('userSettings', 'session');
}
}

These examples showcase the flexibility of @Session in managing both session and local storage. Functions like setItem, getItem, and removeItem offer a unified interface for storage operations, internally using the storage-specific methods (setLocalStorageItem, getLocalStorageItem, removeLocalStorageItem, setSessionStorageItem, getSessionStorageItem, and removeSessionStorageItem) to provide a cohesive and comprehensive session management strategy.

Considerations​

  • Sensitive Data Handling: Be mindful of the sensitivity of data being stored in the browser storage. Avoid storing sensitive information like passwords or personal identification numbers (PINs) in local or session storage.
  • Storage Key Management: Use the setStorageKeyPrefix method to namespace your application's storage keys, reducing the likelihood of key collisions with other applications or libraries.
  • Session Clean-up: Ensure to clear session-related data upon user logout or session expiration to prevent unauthorized access or data leakage.