sessionize-auth
Version:
A flexible session management library for React, Next.js, Angular, and React Native
77 lines (70 loc) • 2.53 kB
text/typescript
import { SessionStore } from "../core/types";
interface RedisConfig {
host: string;
port: number;
password?: string;
db?: number;
keyPrefix?: string;
ttl?: number; // Time to live in seconds
}
/**
* Redis session store
*/
export function createRedisStore<T>(config: RedisConfig): SessionStore<T> {
const {
host,
port,
password,
db = 0,
keyPrefix = "sessionize:",
ttl = 7 * 24 * 60 * 60 // 7 days
} = config;
// This would typically use a Redis client like ioredis
// For now, we'll create a mock implementation
const redisClient = {
get: async (key: string): Promise<string | null> => {
// Mock implementation - replace with actual Redis client
console.warn("Redis store not implemented - using mock");
return null;
},
set: async (key: string, value: string, ttlSeconds?: number): Promise<void> => {
// Mock implementation - replace with actual Redis client
console.warn("Redis store not implemented - using mock");
},
del: async (key: string): Promise<void> => {
// Mock implementation - replace with actual Redis client
console.warn("Redis store not implemented - using mock");
}
};
return {
get: (): T | null => {
try {
const key = `${keyPrefix}account`;
const data = redisClient.get(key);
// Mock implementation - in real usage, this would be async
const mockData = data as any;
return mockData ? JSON.parse(mockData) : null;
} catch (error) {
console.error("Failed to get session from Redis:", error);
return null;
}
},
set: (account: T): void => {
try {
const key = `${keyPrefix}account`;
const serialized = JSON.stringify(account);
redisClient.set(key, serialized, ttl);
} catch (error) {
console.error("Failed to save session to Redis:", error);
}
},
remove: (): void => {
try {
const key = `${keyPrefix}account`;
redisClient.del(key);
} catch (error) {
console.error("Failed to remove session from Redis:", error);
}
}
};
}