@nhost/nhost-js
Version:
Nhost JavaScript SDK
91 lines • 2.83 kB
JavaScript
/**
* Storage implementations for session persistence in different environments.
*
* This module provides different storage adapters for persisting authentication sessions
* across page reloads and browser sessions.
*/
import { decodeUserSession } from './session';
import { LocalStorage, MemoryStorage, } from './storageBackend';
/**
* A wrapper around any SessionStorageInterface implementation that adds
* the ability to subscribe to session changes.
*/
export class SessionStorage {
storage;
subscribers = new Set();
/**
* Creates a new SessionStorage instance
* @param storage - The underlying storage implementation to use
*/
constructor(storage) {
this.storage = storage;
}
/**
* Gets the session from the underlying storage
* @returns The stored session or null if not found
*/
get() {
return this.storage.get();
}
/**
* Sets the session in the underlying storage and notifies subscribers
* @param value - The session to store
*/
set(value) {
const decodedToken = decodeUserSession(value.accessToken);
const decodedSession = {
...value,
decodedToken: decodedToken,
};
this.storage.set(decodedSession);
this.notifySubscribers(decodedSession);
}
/**
* Removes the session from the underlying storage and notifies subscribers
*/
remove() {
this.storage.remove();
this.notifySubscribers(null);
}
/**
* Subscribe to session changes
* @param callback - Function that will be called when the session changes
* @returns An unsubscribe function to remove this subscription
*/
onChange(callback) {
this.subscribers.add(callback);
return () => {
this.subscribers.delete(callback);
};
}
/**
* Notify all subscribers of a session change
* @param session - The new session value or null if removed
*/
notifySubscribers(session) {
for (const subscriber of this.subscribers) {
try {
subscriber(session);
}
catch (error) {
console.error('Error notifying subscriber:', error);
}
}
}
}
/**
* Detects the best available storage implementation for the current environment.
*
* The detection process follows this order:
* 1. Try to use localStorage if we're in a browser environment
* 2. Fall back to in-memory storage if localStorage isn't available
*
* @returns The best available storage implementation as a SessionStorageBackend
*/
export const detectStorage = () => {
if (typeof window !== 'undefined') {
return new LocalStorage();
}
return new MemoryStorage();
};
//# sourceMappingURL=storage.js.map