fix(integration): store omv session in redis (#2467)
* feat(pihole): add support for v6 * fix: add session-store to keep using same session for pi-hole requests * fix(integration): store omv session in redis * chore: address pull request feedback
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import superjson from "superjson";
|
||||
|
||||
import { decryptSecret, encryptSecret } from "@homarr/common/server";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createGetSetChannel } from "@homarr/redis";
|
||||
|
||||
const localLogger = logger.child({ module: "SessionStore" });
|
||||
|
||||
export const createSessionStore = (integration: { id: string }) => {
|
||||
export const createSessionStore = <TValue>(integration: { id: string }) => {
|
||||
const channelName = `session-store:${integration.id}`;
|
||||
const channel = createGetSetChannel<`${string}.${string}`>(channelName);
|
||||
|
||||
@@ -13,11 +15,20 @@ export const createSessionStore = (integration: { id: string }) => {
|
||||
localLogger.debug("Getting session from store", { store: channelName });
|
||||
const value = await channel.getAsync();
|
||||
if (!value) return null;
|
||||
return decryptSecret(value);
|
||||
try {
|
||||
return superjson.parse<TValue>(decryptSecret(value));
|
||||
} catch (error) {
|
||||
localLogger.warn("Failed to load session", { store: channelName, error });
|
||||
return null;
|
||||
}
|
||||
},
|
||||
async setAsync(value: string) {
|
||||
async setAsync(value: TValue) {
|
||||
localLogger.debug("Updating session in store", { store: channelName });
|
||||
await channel.setAsync(encryptSecret(value));
|
||||
try {
|
||||
await channel.setAsync(encryptSecret(superjson.stringify(value)));
|
||||
} catch (error) {
|
||||
localLogger.error("Failed to save session", { store: channelName, error });
|
||||
}
|
||||
},
|
||||
async clearAsync() {
|
||||
localLogger.debug("Cleared session in store", { store: channelName });
|
||||
@@ -26,4 +37,4 @@ export const createSessionStore = (integration: { id: string }) => {
|
||||
};
|
||||
};
|
||||
|
||||
export type SessionStore = ReturnType<typeof createSessionStore>;
|
||||
export type SessionStore<TValue> = ReturnType<typeof createSessionStore<TValue>>;
|
||||
|
||||
Reference in New Issue
Block a user